Skip to content

Commit

Permalink
Merge pull request #106 from Soneso/p-22
Browse files Browse the repository at this point in the history
Support for protocol 22
  • Loading branch information
christian-rogobete authored Oct 24, 2024
2 parents 5cd0853 + e944c19 commit 1194f81
Show file tree
Hide file tree
Showing 14 changed files with 368 additions and 108 deletions.
61 changes: 61 additions & 0 deletions lib/src/invoke_host_function_operation.dart
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,36 @@ abstract class HostFunction {
}
}
break;
case XdrHostFunctionType.HOST_FUNCTION_TYPE_CREATE_CONTRACT_V2:
if (xdr.createContractV2 != null) {
if (xdr.createContractV2!.contractIDPreimage.type ==
XdrContractIDPreimageType.CONTRACT_ID_PREIMAGE_FROM_ADDRESS) {
if (xdr.createContractV2!.executable.type ==
XdrContractExecutableType.CONTRACT_EXECUTABLE_WASM &&
xdr.createContractV2!.executable.wasmHash != null) {
String wasmId = Util.bytesToHex(
xdr.createContractV2!.executable.wasmHash!.hash);
return CreateContractWithConstructorHostFunction(
Address.fromXdr(
xdr.createContractV2!.contractIDPreimage.address!),
wasmId, xdr.createContractV2!.constructorArgs,
salt: xdr.createContractV2!.contractIDPreimage.salt!);
} else if (xdr.createContractV2!.executable.type ==
XdrContractExecutableType.CONTRACT_EXECUTABLE_STELLAR_ASSET) {
return DeploySACWithSourceAccountHostFunction(
Address.fromXdr(
xdr.createContractV2!.contractIDPreimage.address!),
salt: xdr.createContractV2!.contractIDPreimage.salt!);
}
} else if (xdr.createContractV2!.contractIDPreimage.type ==
XdrContractIDPreimageType.CONTRACT_ID_PREIMAGE_FROM_ASSET &&
xdr.createContractV2!.executable.type ==
XdrContractExecutableType.CONTRACT_EXECUTABLE_STELLAR_ASSET) {
return DeploySACWithAssetHostFunction(Asset.fromXdr(
xdr.createContractV2!.contractIDPreimage.fromAsset!));
}
}
break;
}
throw UnimplementedError();
}
Expand Down Expand Up @@ -117,6 +147,37 @@ class CreateContractHostFunction extends HostFunction {
}
}

class CreateContractWithConstructorHostFunction extends HostFunction {
Address _address;
Address get address => this._address;
set address(Address value) => this._address = value;

String _wasmId;
String get wasmId => this._wasmId;
set wasmId(String value) => this._wasmId = value;

List<XdrSCVal> _constructorArgs;
List<XdrSCVal> get constructorArgs => this._constructorArgs;
set constructorArgs(List<XdrSCVal> value) => this._constructorArgs = value;

late XdrUint256 _salt;
XdrUint256 get salt => this._salt;
set salt(XdrUint256 value) => this._salt = value;

CreateContractWithConstructorHostFunction(this._address, this._wasmId, this._constructorArgs, {XdrUint256? salt}) {
if (salt != null) {
this._salt = salt;
} else {
this._salt = new XdrUint256(TweetNaCl.randombytes(32));
}
}

@override
XdrHostFunction toXdr() {
return XdrHostFunction.forCreatingContractV2(address.toXdr(), salt, wasmId, constructorArgs);
}
}

class DeploySACWithSourceAccountHostFunction extends HostFunction {
Address _address;
Address get address => this._address;
Expand Down
6 changes: 0 additions & 6 deletions lib/src/responses/asset_response.dart
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,6 @@ class AssetResponse extends Response {
AssetBalances balances;
String claimableBalancesAmount;
String pagingToken;
String amount;
int numAccounts;
int numLiquidityPools;
String liquidityPoolsAmount;
Flags flags;
Expand All @@ -39,8 +37,6 @@ class AssetResponse extends Response {
this.balances,
this.claimableBalancesAmount,
this.pagingToken,
this.amount,
this.numAccounts,
this.numLiquidityPools,
this.liquidityPoolsAmount,
this.flags,
Expand All @@ -65,8 +61,6 @@ class AssetResponse extends Response {
AssetBalances.fromJson(json['balances']),
json['claimable_balances_amount'],
json['paging_token'],
json['amount'],
convertInt(json['num_accounts']!)!,
convertInt(json['num_liquidity_pools']!)!,
json['liquidity_pools_amount'],
Flags.fromJson(json['flags']),
Expand Down
2 changes: 1 addition & 1 deletion lib/src/responses/submit_transaction_response.dart
Original file line number Diff line number Diff line change
Expand Up @@ -386,7 +386,7 @@ class SubmitAsyncTransactionResponse {
txStatus: json['tx_status'],
hash: json['hash'],
httpStatusCode: httpResponseStatusCode,
errorResultXdrBase64: json['errorResultXdr'],
errorResultXdrBase64: json['errorResultXdr'] ?? json['error_result_xdr'],
);
}

Expand Down
6 changes: 0 additions & 6 deletions lib/src/responses/transaction_response.dart
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,6 @@ class TransactionResponse extends Response {
Memo _memo;
String? memoBytes;
List<String> signatures;
String? validAfter;
String? validBefore;
FeeBumpTransactionResponse? feeBumpTransaction;
InnerTransaction? innerTransaction;
TransactionResponseLinks links;
Expand Down Expand Up @@ -62,8 +60,6 @@ class TransactionResponse extends Response {
this._memo,
this.memoBytes,
this.signatures,
this.validAfter,
this.validBefore,
this.feeBumpTransaction,
this.innerTransaction,
this.links,
Expand Down Expand Up @@ -99,8 +95,6 @@ class TransactionResponse extends Response {
Memo.fromJson(json),
json['memo_bytes'],
signaturesList,
json['valid_after'],
json['valid_before'],
json['fee_bump_transaction'] == null
? null
: FeeBumpTransactionResponse.fromJson(json['fee_bump_transaction']),
Expand Down
53 changes: 53 additions & 0 deletions lib/src/sep/0011/txrep.dart
Original file line number Diff line number Diff line change
Expand Up @@ -710,6 +710,10 @@ class TxRep {
type) {
var args = _getCreateContractArgs('$prefix.createContractHostFn', map);
return XdrSorobanAuthorizedFunction.forCreateContractArgs(args);
} else if ('SOROBAN_AUTHORIZED_FUNCTION_TYPE_CREATE_CONTRACT_V2_HOST_FN' ==
type) {
var args = _getCreateContractArgsV2('$prefix.createContractV2HostFn', map);
return XdrSorobanAuthorizedFunction.forCreateContractArgsV2(args);
} else {
throw Exception('unknown $prefix.type');
}
Expand Down Expand Up @@ -940,6 +944,26 @@ class TxRep {
return XdrCreateContractArgs(preimage, executable);
}

static XdrCreateContractArgsV2 _getCreateContractArgsV2(
String prefix, Map<String, String> map) {
var preimage = _getContractIDPreimage('$prefix.contractIDPreimage', map);
var executable = _getContractExecutable('$prefix.executable', map);
var argsLenStr = _getString('$prefix.constructorArgs.len', map);
int argsLen = 0;
try {
argsLen = int.parse(argsLenStr);
} catch (e) {
throw Exception('invalid value for $prefix.constructorArgs.len');
}
List<XdrSCVal> args = List<XdrSCVal>.empty(growable: true);
for (int i = 0; i < argsLen; i++) {
XdrSCVal next = _getSCVal('$prefix.constructorArgs[$i]', map);
args.add(next);
}

return XdrCreateContractArgsV2(preimage, executable, args);
}

static XdrHostFunction _getHostFunction(
String prefix, Map<String, String> map) {
String type = _getString('$prefix.type', map);
Expand All @@ -951,6 +975,10 @@ class TxRep {
var createContractArgs =
_getCreateContractArgs('$prefix.createContract', map);
return XdrHostFunction.forCreatingContractWithArgs(createContractArgs);
} else if (type == 'HOST_FUNCTION_TYPE_CREATE_CONTRACT_V2') {
var createContractArgs =
_getCreateContractArgsV2('$prefix.createContractV2', map);
return XdrHostFunction.forCreatingContractV2WithArgs(createContractArgs);
} else if (type == 'HOST_FUNCTION_TYPE_UPLOAD_CONTRACT_WASM') {
var wasmStr = _getString('$prefix.wasm', map);
return XdrHostFunction.forUploadContractWasm(Util.hexToBytes(wasmStr));
Expand Down Expand Up @@ -3137,6 +3165,11 @@ class TxRep {
_addLine('$fnPrefix.type', 'HOST_FUNCTION_TYPE_CREATE_CONTRACT', lines);
_addCreateContractArgs(
hostFunctionXdr.createContract!, lines, '$fnPrefix.createContract');
} else if (hostFunctionXdr.type ==
XdrHostFunctionType.HOST_FUNCTION_TYPE_CREATE_CONTRACT_V2) {
_addLine('$fnPrefix.type', 'HOST_FUNCTION_TYPE_CREATE_CONTRACT_V2', lines);
_addCreateContractArgsV2(
hostFunctionXdr.createContractV2!, lines, '$fnPrefix.createContractV2');
} else if (hostFunctionXdr.type ==
XdrHostFunctionType.HOST_FUNCTION_TYPE_UPLOAD_CONTRACT_WASM) {
_addLine(
Expand Down Expand Up @@ -3195,6 +3228,13 @@ class TxRep {
'SOROBAN_AUTHORIZED_FUNCTION_TYPE_CREATE_CONTRACT_HOST_FN', lines);
_addCreateContractArgs(function.createContractHostFn!, lines,
'$prefix.createContractHostFn');
} else if (function.type ==
XdrSorobanAuthorizedFunctionType
.SOROBAN_AUTHORIZED_FUNCTION_TYPE_CREATE_CONTRACT_V2_HOST_FN) {
_addLine('$prefix.type',
'SOROBAN_AUTHORIZED_FUNCTION_TYPE_CREATE_CONTRACT_V2_HOST_FN', lines);
_addCreateContractArgsV2(function.createContractV2HostFn!, lines,
'$prefix.createContractV2HostFn');
}
}

Expand Down Expand Up @@ -3227,6 +3267,19 @@ class TxRep {
_addContractExecutable(args.executable, lines, '$prefix.executable');
}

static _addCreateContractArgsV2(
XdrCreateContractArgsV2 args, List<String> lines, String prefix) {
_addContractIDPreimage(
args.contractIDPreimage, lines, '$prefix.contractIDPreimage');
_addContractExecutable(args.executable, lines, '$prefix.executable');

int argsLen = args.constructorArgs.length;
_addLine('$prefix.constructorArgs.len', argsLen.toString(), lines);
for (int i = 0; i < argsLen; i++) {
_addSCVal(args.constructorArgs[i], lines, '$prefix.constructorArgs[$i]');
}
}

static _addContractIDPreimage(
XdrContractIDPreimage preimage, List<String> lines, String prefix) {
if (preimage.type ==
Expand Down
35 changes: 27 additions & 8 deletions lib/src/soroban/soroban_auth.dart
Original file line number Diff line number Diff line change
Expand Up @@ -167,18 +167,19 @@ class SorobanCredentials {
class SorobanAuthorizedFunction {
XdrInvokeContractArgs? contractFn;
XdrCreateContractArgs? createContractHostFn;
XdrCreateContractArgsV2? createContractV2HostFn;

SorobanAuthorizedFunction(
{XdrInvokeContractArgs? contractFn,
XdrCreateContractArgs? createContractHostFn}) {
if (contractFn == null && createContractHostFn == null) {
throw ArgumentError("invalid arguments");
}
if (contractFn != null && createContractHostFn != null) {
XdrCreateContractArgs? createContractHostFn,
XdrCreateContractArgsV2? createContractV2HostFn}) {
if (contractFn == null && createContractHostFn == null && createContractV2HostFn == null) {
throw ArgumentError("invalid arguments");
}

this.contractFn = contractFn;
this.createContractHostFn = createContractHostFn;
this.createContractV2HostFn = createContractV2HostFn;
}

static SorobanAuthorizedFunction forContractFunction(
Expand All @@ -194,16 +195,28 @@ class SorobanAuthorizedFunction {
createContractHostFn: createContractHostFn);
}

static SorobanAuthorizedFunction forCreateContractV2HostFunction(
XdrCreateContractArgsV2 createContractV2HostFn) {
return SorobanAuthorizedFunction(
createContractV2HostFn: createContractV2HostFn);
}

static SorobanAuthorizedFunction fromXdr(XdrSorobanAuthorizedFunction xdr) {
if (xdr.type ==
XdrSorobanAuthorizedFunctionType
.SOROBAN_AUTHORIZED_FUNCTION_TYPE_CONTRACT_FN &&
xdr.contractFn != null) {
return SorobanAuthorizedFunction(
contractFn:xdr.contractFn!);
} else {
} else if (xdr.type ==
XdrSorobanAuthorizedFunctionType
.SOROBAN_AUTHORIZED_FUNCTION_TYPE_CREATE_CONTRACT_HOST_FN &&
xdr.createContractHostFn != null) {
return SorobanAuthorizedFunction(
createContractHostFn: xdr.createContractHostFn);
} else {
return SorobanAuthorizedFunction(
createContractV2HostFn: xdr.createContractV2HostFn);
}
}

Expand All @@ -214,11 +227,17 @@ class SorobanAuthorizedFunction {
.SOROBAN_AUTHORIZED_FUNCTION_TYPE_CONTRACT_FN);
cfn.contractFn = contractFn!;
return cfn;
} else if (createContractHostFn != null) {
XdrSorobanAuthorizedFunction cfn = XdrSorobanAuthorizedFunction(
XdrSorobanAuthorizedFunctionType
.SOROBAN_AUTHORIZED_FUNCTION_TYPE_CREATE_CONTRACT_HOST_FN);
cfn.createContractHostFn = createContractHostFn!;
return cfn;
}
XdrSorobanAuthorizedFunction cfn = XdrSorobanAuthorizedFunction(
XdrSorobanAuthorizedFunctionType
.SOROBAN_AUTHORIZED_FUNCTION_TYPE_CREATE_CONTRACT_HOST_FN);
cfn.createContractHostFn = createContractHostFn!;
.SOROBAN_AUTHORIZED_FUNCTION_TYPE_CREATE_CONTRACT_V2_HOST_FN);
cfn.createContractV2HostFn = createContractV2HostFn!;
return cfn;
}
}
Expand Down
Loading

0 comments on commit 1194f81

Please sign in to comment.