@aptos-labs/ts-sdk 0.0.2 → 0.0.4
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +33 -24
- package/dist/browser/index.global.js +26 -25
- package/dist/browser/index.global.js.map +1 -1
- package/dist/cjs/index.d.ts +1036 -342
- package/dist/cjs/index.js +1585 -676
- package/dist/cjs/index.js.map +1 -1
- package/dist/esm/index.d.ts +1036 -342
- package/dist/esm/index.mjs +1507 -671
- package/dist/esm/index.mjs.map +1 -1
- package/dist/types/index.d.ts +29 -34
- package/dist/types/index.js +4 -2
- package/dist/types/index.js.map +1 -1
- package/package.json +3 -2
- package/src/api/account.ts +19 -20
- package/src/api/ans.ts +58 -0
- package/src/api/aptos.ts +7 -1
- package/src/api/coin.ts +7 -7
- package/src/api/digitalAsset.ts +14 -13
- package/src/api/event.ts +5 -4
- package/src/api/faucet.ts +8 -3
- package/src/api/general.ts +2 -2
- package/src/api/staking.ts +5 -4
- package/src/api/transactionSubmission.ts +51 -42
- package/src/bcs/deserializer.ts +4 -4
- package/src/bcs/serializable/fixedBytes.ts +1 -1
- package/src/bcs/serializable/moveStructs.ts +21 -45
- package/src/bcs/serializer.ts +4 -4
- package/src/client/core.ts +14 -6
- package/src/core/account.ts +92 -34
- package/src/core/accountAddress.ts +37 -31
- package/src/core/authenticationKey.ts +35 -11
- package/src/core/crypto/anyPublicKey.ts +14 -18
- package/src/core/crypto/ed25519.ts +54 -1
- package/src/core/crypto/hdKey.ts +105 -0
- package/src/core/crypto/index.ts +2 -0
- package/src/core/crypto/multiKey.ts +122 -0
- package/src/core/crypto/secp256k1.ts +38 -0
- package/src/index.ts +1 -3
- package/src/internal/account.ts +84 -62
- package/src/internal/ans.ts +177 -0
- package/src/internal/coin.ts +12 -12
- package/src/internal/digitalAsset.ts +23 -24
- package/src/internal/event.ts +8 -8
- package/src/internal/faucet.ts +12 -8
- package/src/internal/general.ts +3 -3
- package/src/internal/staking.ts +8 -8
- package/src/internal/transactionSubmission.ts +111 -33
- package/src/transactions/authenticator/account.ts +39 -0
- package/src/transactions/authenticator/index.ts +5 -0
- package/src/transactions/authenticator/transaction.ts +6 -6
- package/src/transactions/index.ts +9 -0
- package/src/transactions/instances/index.ts +2 -0
- package/src/transactions/instances/moduleId.ts +1 -1
- package/src/transactions/instances/rotationProofChallenge.ts +58 -0
- package/src/transactions/instances/transactionPayload.ts +13 -8
- package/src/transactions/transactionBuilder/helpers.ts +103 -0
- package/src/transactions/transactionBuilder/index.ts +6 -0
- package/src/transactions/transactionBuilder/remoteAbi.ts +339 -0
- package/src/transactions/{transaction_builder/transaction_builder.ts → transactionBuilder/transactionBuilder.ts} +175 -108
- package/src/transactions/typeTag/index.ts +385 -0
- package/src/transactions/typeTag/parser.ts +22 -9
- package/src/transactions/types.ts +114 -62
- package/src/types/index.ts +28 -31
- package/src/utils/apiEndpoints.ts +8 -0
- package/src/version.ts +1 -1
- package/src/transactions/typeTag/typeTag.ts +0 -487
- package/src/utils/hdKey.ts +0 -113
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
// Copyright © Aptos Foundation
|
|
2
|
+
// SPDX-License-Identifier: Apache-2.0
|
|
3
|
+
|
|
4
|
+
import { Serializer, Serializable } from "../../bcs/serializer";
|
|
5
|
+
import { AccountAddress } from "../../core/accountAddress";
|
|
6
|
+
import { AnyNumber } from "../../types";
|
|
7
|
+
import { PublicKey } from "../../core/crypto/asymmetricCrypto";
|
|
8
|
+
import { MoveString, MoveVector, U64, U8 } from "../../bcs";
|
|
9
|
+
|
|
10
|
+
/**
|
|
11
|
+
* Representation of the challenge which is needed to sign by owner of the account
|
|
12
|
+
* to rotate the authentication key.
|
|
13
|
+
*/
|
|
14
|
+
export class RotationProofChallenge extends Serializable {
|
|
15
|
+
// Resource account address
|
|
16
|
+
public readonly accountAddress: AccountAddress = AccountAddress.ONE;
|
|
17
|
+
|
|
18
|
+
// Module name, i.e: 0x1::account
|
|
19
|
+
public readonly moduleName: MoveString = new MoveString("account");
|
|
20
|
+
|
|
21
|
+
// The rotation proof challenge struct name that live under the module
|
|
22
|
+
public readonly structName: MoveString = new MoveString("RotationProofChallenge");
|
|
23
|
+
|
|
24
|
+
// Signer's address
|
|
25
|
+
public readonly originator: AccountAddress;
|
|
26
|
+
|
|
27
|
+
// Signer's current authentication key
|
|
28
|
+
public readonly currentAuthKey: AccountAddress;
|
|
29
|
+
|
|
30
|
+
// New public key to rotate to
|
|
31
|
+
public readonly newPublicKey: MoveVector<U8>;
|
|
32
|
+
|
|
33
|
+
// Sequence number of the account
|
|
34
|
+
public readonly sequenceNumber: U64;
|
|
35
|
+
|
|
36
|
+
constructor(args: {
|
|
37
|
+
sequenceNumber: AnyNumber;
|
|
38
|
+
originator: AccountAddress;
|
|
39
|
+
currentAuthKey: AccountAddress;
|
|
40
|
+
newPublicKey: PublicKey;
|
|
41
|
+
}) {
|
|
42
|
+
super();
|
|
43
|
+
this.sequenceNumber = new U64(args.sequenceNumber);
|
|
44
|
+
this.originator = args.originator;
|
|
45
|
+
this.currentAuthKey = args.currentAuthKey;
|
|
46
|
+
this.newPublicKey = MoveVector.U8(args.newPublicKey.toUint8Array());
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
serialize(serializer: Serializer): void {
|
|
50
|
+
serializer.serialize(this.accountAddress);
|
|
51
|
+
serializer.serialize(this.moduleName);
|
|
52
|
+
serializer.serialize(this.structName);
|
|
53
|
+
serializer.serialize(this.sequenceNumber);
|
|
54
|
+
serializer.serialize(this.originator);
|
|
55
|
+
serializer.serialize(this.currentAuthKey);
|
|
56
|
+
serializer.serialize(this.newPublicKey);
|
|
57
|
+
}
|
|
58
|
+
}
|
|
@@ -13,7 +13,7 @@ import { Identifier } from "./identifier";
|
|
|
13
13
|
import { ModuleId } from "./moduleId";
|
|
14
14
|
import type { EntryFunctionArgument, ScriptFunctionArgument, TransactionArgument } from "./transactionArgument";
|
|
15
15
|
import { MoveModuleId, ScriptTransactionArgumentVariants, TransactionPayloadVariants } from "../../types";
|
|
16
|
-
import { TypeTag } from "../typeTag
|
|
16
|
+
import { TypeTag } from "../typeTag";
|
|
17
17
|
|
|
18
18
|
/**
|
|
19
19
|
* Deserialize a Script Transaction Argument
|
|
@@ -334,7 +334,7 @@ export class Script {
|
|
|
334
334
|
export class MultiSig {
|
|
335
335
|
public readonly multisig_address: AccountAddress;
|
|
336
336
|
|
|
337
|
-
public readonly transaction_payload?:
|
|
337
|
+
public readonly transaction_payload?: MultisigTransactionPayload;
|
|
338
338
|
|
|
339
339
|
/**
|
|
340
340
|
* Contains the payload to run a multi-sig account transaction.
|
|
@@ -344,7 +344,7 @@ export class MultiSig {
|
|
|
344
344
|
* @param transaction_payload The payload of the multi-sig transaction. This is optional when executing a multi-sig
|
|
345
345
|
* transaction whose payload is already stored on chain.
|
|
346
346
|
*/
|
|
347
|
-
constructor(multisig_address: AccountAddress, transaction_payload?:
|
|
347
|
+
constructor(multisig_address: AccountAddress, transaction_payload?: MultisigTransactionPayload) {
|
|
348
348
|
this.multisig_address = multisig_address;
|
|
349
349
|
this.transaction_payload = transaction_payload;
|
|
350
350
|
}
|
|
@@ -366,16 +366,21 @@ export class MultiSig {
|
|
|
366
366
|
const payloadPresent = deserializer.deserializeBool();
|
|
367
367
|
let transaction_payload;
|
|
368
368
|
if (payloadPresent) {
|
|
369
|
-
transaction_payload =
|
|
369
|
+
transaction_payload = MultisigTransactionPayload.deserialize(deserializer);
|
|
370
370
|
}
|
|
371
371
|
return new MultiSig(multisig_address, transaction_payload);
|
|
372
372
|
}
|
|
373
373
|
}
|
|
374
374
|
|
|
375
375
|
/**
|
|
376
|
-
* Representation of a MultiSig Transaction Payload
|
|
376
|
+
* Representation of a MultiSig Transaction Payload from `multisig_account.move`
|
|
377
|
+
* that can be serialized and deserialized
|
|
378
|
+
|
|
379
|
+
* This class exists right now to represent an extensible transaction payload class for
|
|
380
|
+
* transactions used in `multisig_account.move`. Eventually, this class will be able to
|
|
381
|
+
* support script payloads when the `multisig_account.move` module supports them.
|
|
377
382
|
*/
|
|
378
|
-
export class
|
|
383
|
+
export class MultisigTransactionPayload {
|
|
379
384
|
public readonly transaction_payload: EntryFunction;
|
|
380
385
|
|
|
381
386
|
/**
|
|
@@ -399,9 +404,9 @@ export class MultiSigTransactionPayload {
|
|
|
399
404
|
this.transaction_payload.serialize(serializer);
|
|
400
405
|
}
|
|
401
406
|
|
|
402
|
-
static deserialize(deserializer: Deserializer):
|
|
407
|
+
static deserialize(deserializer: Deserializer): MultisigTransactionPayload {
|
|
403
408
|
// This is the enum value indicating which type of payload the multi-sig transaction contains.
|
|
404
409
|
deserializer.deserializeUleb128AsU32();
|
|
405
|
-
return new
|
|
410
|
+
return new MultisigTransactionPayload(EntryFunction.deserialize(deserializer));
|
|
406
411
|
}
|
|
407
412
|
}
|
|
@@ -0,0 +1,103 @@
|
|
|
1
|
+
// Copyright © Aptos Foundation
|
|
2
|
+
// SPDX-License-Identifier: Apache-2.0
|
|
3
|
+
|
|
4
|
+
import {
|
|
5
|
+
EntryFunctionArgumentTypes,
|
|
6
|
+
InputGenerateTransactionPayloadData,
|
|
7
|
+
InputGenerateTransactionPayloadDataWithRemoteABI,
|
|
8
|
+
InputScriptData,
|
|
9
|
+
SimpleEntryFunctionArgumentTypes,
|
|
10
|
+
} from "../types";
|
|
11
|
+
import { Bool, FixedBytes, MoveString, U128, U16, U256, U32, U64, U8 } from "../../bcs";
|
|
12
|
+
import { AccountAddress } from "../../core";
|
|
13
|
+
import { MoveFunction, MoveStructType } from "../../types";
|
|
14
|
+
|
|
15
|
+
export function isBool(arg: SimpleEntryFunctionArgumentTypes): arg is boolean {
|
|
16
|
+
return typeof arg === "boolean";
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
export function isString(arg: any): arg is string {
|
|
20
|
+
return typeof arg === "string";
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
export function isNumber(arg: SimpleEntryFunctionArgumentTypes): arg is number {
|
|
24
|
+
return typeof arg === "number";
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
export function isLargeNumber(arg: SimpleEntryFunctionArgumentTypes): arg is number | bigint | string {
|
|
28
|
+
return typeof arg === "number" || typeof arg === "bigint" || typeof arg === "string";
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
export function isNull(arg: SimpleEntryFunctionArgumentTypes): arg is null | undefined {
|
|
32
|
+
return arg === null || arg === undefined;
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
export function isBcsBool(arg: EntryFunctionArgumentTypes | SimpleEntryFunctionArgumentTypes): arg is Bool {
|
|
36
|
+
return arg instanceof Bool;
|
|
37
|
+
}
|
|
38
|
+
export function isBcsAddress(
|
|
39
|
+
arg: EntryFunctionArgumentTypes | SimpleEntryFunctionArgumentTypes,
|
|
40
|
+
): arg is AccountAddress {
|
|
41
|
+
return arg instanceof AccountAddress;
|
|
42
|
+
}
|
|
43
|
+
export function isBcsString(arg: EntryFunctionArgumentTypes | SimpleEntryFunctionArgumentTypes): arg is MoveString {
|
|
44
|
+
return arg instanceof MoveString;
|
|
45
|
+
}
|
|
46
|
+
export function isBcsFixedBytes(arg: EntryFunctionArgumentTypes | SimpleEntryFunctionArgumentTypes): arg is FixedBytes {
|
|
47
|
+
return arg instanceof FixedBytes;
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
export function isBcsU8(arg: EntryFunctionArgumentTypes | SimpleEntryFunctionArgumentTypes): arg is U8 {
|
|
51
|
+
return arg instanceof U8;
|
|
52
|
+
}
|
|
53
|
+
export function isBcsU16(arg: EntryFunctionArgumentTypes | SimpleEntryFunctionArgumentTypes): arg is U16 {
|
|
54
|
+
return arg instanceof U16;
|
|
55
|
+
}
|
|
56
|
+
export function isBcsU32(arg: EntryFunctionArgumentTypes | SimpleEntryFunctionArgumentTypes): arg is U32 {
|
|
57
|
+
return arg instanceof U32;
|
|
58
|
+
}
|
|
59
|
+
export function isBcsU64(arg: EntryFunctionArgumentTypes | SimpleEntryFunctionArgumentTypes): arg is U64 {
|
|
60
|
+
return arg instanceof U64;
|
|
61
|
+
}
|
|
62
|
+
export function isBcsU128(arg: EntryFunctionArgumentTypes | SimpleEntryFunctionArgumentTypes): arg is U128 {
|
|
63
|
+
return arg instanceof U128;
|
|
64
|
+
}
|
|
65
|
+
export function isBcsU256(arg: EntryFunctionArgumentTypes | SimpleEntryFunctionArgumentTypes): arg is U256 {
|
|
66
|
+
return arg instanceof U256;
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
export function isScriptDataInput(
|
|
70
|
+
arg: InputGenerateTransactionPayloadDataWithRemoteABI | InputGenerateTransactionPayloadData,
|
|
71
|
+
): arg is InputScriptData {
|
|
72
|
+
return "bytecode" in arg;
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
export function throwTypeMismatch(expectedType: string, position: number) {
|
|
76
|
+
throw new Error(`Type mismatch for argument ${position}, expected '${expectedType}'`);
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
/**
|
|
80
|
+
* Finds first non-signer arg.
|
|
81
|
+
*
|
|
82
|
+
* A function is often defined with a `signer` or `&signer` arguments at the start, which are filled in
|
|
83
|
+
* by signatures, and not by the caller.
|
|
84
|
+
* @param functionAbi
|
|
85
|
+
*/
|
|
86
|
+
export function findFirstNonSignerArg(functionAbi: MoveFunction): number {
|
|
87
|
+
const index = functionAbi.params.findIndex((param) => param !== "signer" && param !== "&signer");
|
|
88
|
+
if (index < 0) {
|
|
89
|
+
return functionAbi.params.length;
|
|
90
|
+
}
|
|
91
|
+
return index;
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
export function getFunctionParts(functionArg: MoveStructType) {
|
|
95
|
+
const funcNameParts = functionArg.split("::");
|
|
96
|
+
if (funcNameParts.length !== 3) {
|
|
97
|
+
throw new Error(`Invalid function ${functionArg}`);
|
|
98
|
+
}
|
|
99
|
+
const moduleAddress = funcNameParts[0];
|
|
100
|
+
const moduleName = funcNameParts[1];
|
|
101
|
+
const functionName = funcNameParts[2];
|
|
102
|
+
return { moduleAddress, moduleName, functionName };
|
|
103
|
+
}
|
|
@@ -0,0 +1,339 @@
|
|
|
1
|
+
// Copyright © Aptos Foundation
|
|
2
|
+
// SPDX-License-Identifier: Apache-2.0
|
|
3
|
+
|
|
4
|
+
import { parseTypeTag } from "../typeTag/parser";
|
|
5
|
+
import { TypeTag, TypeTagStruct } from "../typeTag";
|
|
6
|
+
import { AptosConfig } from "../../api/aptosConfig";
|
|
7
|
+
import { EntryFunctionArgumentTypes, SimpleEntryFunctionArgumentTypes, EntryFunctionABI } from "../types";
|
|
8
|
+
import { Bool, MoveOption, MoveString, MoveVector, U128, U16, U256, U32, U64, U8 } from "../../bcs";
|
|
9
|
+
import { AccountAddress, Hex } from "../../core";
|
|
10
|
+
import { getModule } from "../../internal/account";
|
|
11
|
+
import {
|
|
12
|
+
findFirstNonSignerArg,
|
|
13
|
+
isBcsAddress,
|
|
14
|
+
isBcsBool,
|
|
15
|
+
isBcsFixedBytes,
|
|
16
|
+
isBcsString,
|
|
17
|
+
isBcsU128,
|
|
18
|
+
isBcsU16,
|
|
19
|
+
isBcsU256,
|
|
20
|
+
isBcsU32,
|
|
21
|
+
isBcsU64,
|
|
22
|
+
isBcsU8,
|
|
23
|
+
isBool,
|
|
24
|
+
isLargeNumber,
|
|
25
|
+
isNull,
|
|
26
|
+
isNumber,
|
|
27
|
+
isString,
|
|
28
|
+
throwTypeMismatch,
|
|
29
|
+
} from "./helpers";
|
|
30
|
+
|
|
31
|
+
/**
|
|
32
|
+
* Convert type arguments to only type tags, allowing for string representations of type tags
|
|
33
|
+
*/
|
|
34
|
+
export function standardizeTypeTags(typeArguments?: Array<TypeTag | string>): Array<TypeTag> {
|
|
35
|
+
return (
|
|
36
|
+
typeArguments?.map((typeArg: string | TypeTag): TypeTag => {
|
|
37
|
+
// Convert to TypeTag if it's a string representation
|
|
38
|
+
if (isString(typeArg)) {
|
|
39
|
+
return parseTypeTag(typeArg);
|
|
40
|
+
}
|
|
41
|
+
return typeArg;
|
|
42
|
+
}) ?? []
|
|
43
|
+
);
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
/**
|
|
47
|
+
* Fetches the ABI for an entry function from the module
|
|
48
|
+
*
|
|
49
|
+
* @param moduleAddress
|
|
50
|
+
* @param moduleName
|
|
51
|
+
* @param functionName
|
|
52
|
+
* @param aptosConfig
|
|
53
|
+
*/
|
|
54
|
+
export async function fetchEntryFunctionAbi(
|
|
55
|
+
moduleAddress: string,
|
|
56
|
+
moduleName: string,
|
|
57
|
+
functionName: string,
|
|
58
|
+
aptosConfig: AptosConfig,
|
|
59
|
+
): Promise<EntryFunctionABI> {
|
|
60
|
+
// This fetch from the API is currently cached
|
|
61
|
+
const module = await getModule({ aptosConfig, accountAddress: moduleAddress, moduleName });
|
|
62
|
+
|
|
63
|
+
const functionAbi = module.abi?.exposed_functions.find((func) => func.name === functionName);
|
|
64
|
+
|
|
65
|
+
// If there's no ABI, then the function is invalid
|
|
66
|
+
if (!functionAbi) {
|
|
67
|
+
throw new Error(`Could not find entry function ABI for '${moduleAddress}::${moduleName}::${functionName}'`);
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
// Non-entry functions also can't be used
|
|
71
|
+
if (!functionAbi.is_entry) {
|
|
72
|
+
throw new Error(`'${moduleAddress}::${moduleName}::${functionName}' is not an entry function`);
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
// Remove the signer arguments
|
|
76
|
+
const first = findFirstNonSignerArg(functionAbi);
|
|
77
|
+
const params = [];
|
|
78
|
+
for (let i = first; i < functionAbi.params.length; i += 1) {
|
|
79
|
+
params.push(parseTypeTag(functionAbi.params[i], { allowGenerics: true }));
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
return {
|
|
83
|
+
typeParameters: functionAbi.generic_type_params,
|
|
84
|
+
parameters: params,
|
|
85
|
+
};
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
/**
|
|
89
|
+
* Converts a non-BCS encoded argument into BCS encoded, if necessary
|
|
90
|
+
* @param functionName
|
|
91
|
+
* @param functionAbi
|
|
92
|
+
* @param arg
|
|
93
|
+
* @param position
|
|
94
|
+
*/
|
|
95
|
+
export function convertArgument(
|
|
96
|
+
functionName: string,
|
|
97
|
+
functionAbi: EntryFunctionABI,
|
|
98
|
+
arg: EntryFunctionArgumentTypes | SimpleEntryFunctionArgumentTypes,
|
|
99
|
+
position: number,
|
|
100
|
+
genericTypeParams: Array<TypeTag>,
|
|
101
|
+
) {
|
|
102
|
+
// Ensure not too many arguments
|
|
103
|
+
if (position >= functionAbi.parameters.length) {
|
|
104
|
+
throw new Error(`Too many arguments for '${functionName}', expected ${functionAbi.parameters.length}`);
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
// If the argument is bcs encoded, we can just use it directly
|
|
108
|
+
if (
|
|
109
|
+
isBcsBool(arg) ||
|
|
110
|
+
isBcsU8(arg) ||
|
|
111
|
+
isBcsU16(arg) ||
|
|
112
|
+
isBcsU32(arg) ||
|
|
113
|
+
isBcsU64(arg) ||
|
|
114
|
+
isBcsU128(arg) ||
|
|
115
|
+
isBcsU256(arg) ||
|
|
116
|
+
isBcsAddress(arg) ||
|
|
117
|
+
isBcsString(arg) ||
|
|
118
|
+
isBcsFixedBytes(arg) ||
|
|
119
|
+
arg instanceof MoveVector ||
|
|
120
|
+
arg instanceof MoveOption
|
|
121
|
+
) {
|
|
122
|
+
// Ensure the type matches the ABI
|
|
123
|
+
checkType(functionAbi, arg, position);
|
|
124
|
+
return arg;
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
// If it is not BCS encoded, we will need to convert it with the ABI
|
|
128
|
+
return parseArg(arg, functionAbi.parameters[position], position, genericTypeParams);
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
/**
|
|
132
|
+
* Parses a non-BCS encoded argument into a BCS encoded argument recursively
|
|
133
|
+
* @param arg
|
|
134
|
+
* @param param
|
|
135
|
+
* @param position
|
|
136
|
+
* @param genericTypeParams
|
|
137
|
+
*/
|
|
138
|
+
function parseArg(
|
|
139
|
+
arg: SimpleEntryFunctionArgumentTypes,
|
|
140
|
+
param: TypeTag,
|
|
141
|
+
position: number,
|
|
142
|
+
genericTypeParams: Array<TypeTag>,
|
|
143
|
+
): EntryFunctionArgumentTypes {
|
|
144
|
+
if (param.isBool()) {
|
|
145
|
+
if (isBool(arg)) {
|
|
146
|
+
return new Bool(arg);
|
|
147
|
+
}
|
|
148
|
+
throwTypeMismatch("boolean", position);
|
|
149
|
+
}
|
|
150
|
+
// TODO: support uint8array?
|
|
151
|
+
if (param.isAddress()) {
|
|
152
|
+
if (isString(arg)) {
|
|
153
|
+
return AccountAddress.fromStringRelaxed(arg);
|
|
154
|
+
}
|
|
155
|
+
throwTypeMismatch("string", position);
|
|
156
|
+
}
|
|
157
|
+
if (param.isU8()) {
|
|
158
|
+
if (isNumber(arg)) {
|
|
159
|
+
return new U8(arg);
|
|
160
|
+
}
|
|
161
|
+
throwTypeMismatch("number", position);
|
|
162
|
+
}
|
|
163
|
+
if (param.isU16()) {
|
|
164
|
+
if (isNumber(arg)) {
|
|
165
|
+
return new U16(arg);
|
|
166
|
+
}
|
|
167
|
+
throwTypeMismatch("number", position);
|
|
168
|
+
}
|
|
169
|
+
if (param.isU32()) {
|
|
170
|
+
if (isNumber(arg)) {
|
|
171
|
+
return new U32(arg);
|
|
172
|
+
}
|
|
173
|
+
throwTypeMismatch("number", position);
|
|
174
|
+
}
|
|
175
|
+
if (param.isU64()) {
|
|
176
|
+
if (isLargeNumber(arg)) {
|
|
177
|
+
return new U64(BigInt(arg));
|
|
178
|
+
}
|
|
179
|
+
throwTypeMismatch("bigint | number | string", position);
|
|
180
|
+
}
|
|
181
|
+
if (param.isU128()) {
|
|
182
|
+
if (isLargeNumber(arg)) {
|
|
183
|
+
return new U128(BigInt(arg));
|
|
184
|
+
}
|
|
185
|
+
throwTypeMismatch("bigint | number | string", position);
|
|
186
|
+
}
|
|
187
|
+
if (param.isU256()) {
|
|
188
|
+
if (isLargeNumber(arg)) {
|
|
189
|
+
return new U256(BigInt(arg));
|
|
190
|
+
}
|
|
191
|
+
throwTypeMismatch("bigint | number | string", position);
|
|
192
|
+
}
|
|
193
|
+
|
|
194
|
+
// Generic needs to use the sub-type
|
|
195
|
+
if (param.isGeneric()) {
|
|
196
|
+
const genericIndex = param.value;
|
|
197
|
+
if (genericIndex < 0 || genericIndex >= genericTypeParams.length) {
|
|
198
|
+
throw new Error(`Generic argument ${param.toString()} is invalid for argument ${position}`);
|
|
199
|
+
}
|
|
200
|
+
|
|
201
|
+
parseArg(arg, genericTypeParams[genericIndex], position, genericTypeParams);
|
|
202
|
+
}
|
|
203
|
+
|
|
204
|
+
// We have to special case some vectors for Vector<u8>
|
|
205
|
+
if (param.isVector()) {
|
|
206
|
+
// Check special case for Vector<u8>
|
|
207
|
+
if (param.value.isU8() && isString(arg)) {
|
|
208
|
+
// TODO: Improve message when hex is invalid
|
|
209
|
+
return MoveVector.U8(Hex.fromHexInput(arg).toUint8Array());
|
|
210
|
+
}
|
|
211
|
+
|
|
212
|
+
if (Array.isArray(arg)) {
|
|
213
|
+
return new MoveVector(arg.map((item) => parseArg(item, param.value, position, genericTypeParams)));
|
|
214
|
+
}
|
|
215
|
+
|
|
216
|
+
throw new Error(`Type mismatch for argument ${position}, type '${param.toString()}'`);
|
|
217
|
+
}
|
|
218
|
+
|
|
219
|
+
// Handle structs as they're more complex
|
|
220
|
+
if (param.isStruct()) {
|
|
221
|
+
if (param.isString()) {
|
|
222
|
+
if (isString(arg)) {
|
|
223
|
+
return new MoveString(arg);
|
|
224
|
+
}
|
|
225
|
+
throwTypeMismatch("string", position);
|
|
226
|
+
}
|
|
227
|
+
if (param.isObject()) {
|
|
228
|
+
// The inner type of Object doesn't matter, since it's just syntactic sugar
|
|
229
|
+
if (isString(arg)) {
|
|
230
|
+
return AccountAddress.fromStringRelaxed(arg);
|
|
231
|
+
}
|
|
232
|
+
throwTypeMismatch("string", position);
|
|
233
|
+
}
|
|
234
|
+
|
|
235
|
+
if (param.isOption()) {
|
|
236
|
+
// Empty option must be handled specially
|
|
237
|
+
if (isNull(arg)) {
|
|
238
|
+
// Note: This is a placeholder U8 type, and does not match the actual type, as that can't be dynamically grabbed
|
|
239
|
+
return new MoveOption<U8>(null);
|
|
240
|
+
}
|
|
241
|
+
|
|
242
|
+
return new MoveOption(parseArg(arg, param.value.typeArgs[0], position, genericTypeParams));
|
|
243
|
+
}
|
|
244
|
+
|
|
245
|
+
throw new Error(`Unsupported struct input type for argument ${position}, type '${param.toString()}'`);
|
|
246
|
+
}
|
|
247
|
+
|
|
248
|
+
throw new Error(`Type mismatch for argument ${position}, type '${param.toString()}'`);
|
|
249
|
+
}
|
|
250
|
+
|
|
251
|
+
/**
|
|
252
|
+
* Checks that the type of an already BCS encoded argument matches the ABI
|
|
253
|
+
* @param functionAbi
|
|
254
|
+
* @param arg
|
|
255
|
+
* @param position
|
|
256
|
+
*/
|
|
257
|
+
function checkType(functionAbi: EntryFunctionABI, arg: EntryFunctionArgumentTypes, position: number) {
|
|
258
|
+
const param = functionAbi.parameters[position];
|
|
259
|
+
if (param.isBool()) {
|
|
260
|
+
if (isBcsBool(arg)) {
|
|
261
|
+
return;
|
|
262
|
+
}
|
|
263
|
+
throwTypeMismatch("Bool", position);
|
|
264
|
+
}
|
|
265
|
+
if (param.isAddress()) {
|
|
266
|
+
if (isBcsAddress(arg)) {
|
|
267
|
+
return;
|
|
268
|
+
}
|
|
269
|
+
throwTypeMismatch("AccountAddress", position);
|
|
270
|
+
}
|
|
271
|
+
if (param.isU8()) {
|
|
272
|
+
if (isBcsU8(arg)) {
|
|
273
|
+
return;
|
|
274
|
+
}
|
|
275
|
+
throwTypeMismatch("U8", position);
|
|
276
|
+
}
|
|
277
|
+
if (param.isU16()) {
|
|
278
|
+
if (isBcsU16(arg)) {
|
|
279
|
+
return;
|
|
280
|
+
}
|
|
281
|
+
throwTypeMismatch("U16", position);
|
|
282
|
+
}
|
|
283
|
+
if (param.isU32()) {
|
|
284
|
+
if (isBcsU32(arg)) {
|
|
285
|
+
return;
|
|
286
|
+
}
|
|
287
|
+
throwTypeMismatch("U32", position);
|
|
288
|
+
}
|
|
289
|
+
if (param.isU64()) {
|
|
290
|
+
if (isBcsU64(arg)) {
|
|
291
|
+
return;
|
|
292
|
+
}
|
|
293
|
+
throwTypeMismatch("U64", position);
|
|
294
|
+
}
|
|
295
|
+
if (param.isU128()) {
|
|
296
|
+
if (isBcsU128(arg)) {
|
|
297
|
+
return;
|
|
298
|
+
}
|
|
299
|
+
throwTypeMismatch("U128", position);
|
|
300
|
+
}
|
|
301
|
+
if (param.isU256()) {
|
|
302
|
+
if (isBcsU256(arg)) {
|
|
303
|
+
return;
|
|
304
|
+
}
|
|
305
|
+
throwTypeMismatch("U256", position);
|
|
306
|
+
}
|
|
307
|
+
if (param.isVector()) {
|
|
308
|
+
if (arg instanceof MoveVector) {
|
|
309
|
+
// TODO: More introspection to verify the type
|
|
310
|
+
return;
|
|
311
|
+
}
|
|
312
|
+
throwTypeMismatch("MoveVector", position);
|
|
313
|
+
}
|
|
314
|
+
|
|
315
|
+
// Handle structs as they're more complex
|
|
316
|
+
if (param instanceof TypeTagStruct) {
|
|
317
|
+
if (param.isString()) {
|
|
318
|
+
if (isBcsString(arg)) {
|
|
319
|
+
return;
|
|
320
|
+
}
|
|
321
|
+
throwTypeMismatch("MoveString", position);
|
|
322
|
+
}
|
|
323
|
+
if (param.isObject()) {
|
|
324
|
+
if (isBcsAddress(arg)) {
|
|
325
|
+
return;
|
|
326
|
+
}
|
|
327
|
+
throwTypeMismatch("AccountAddress", position);
|
|
328
|
+
}
|
|
329
|
+
if (param.isOption()) {
|
|
330
|
+
if (arg instanceof MoveOption) {
|
|
331
|
+
// TODO: more introspection for the type
|
|
332
|
+
return;
|
|
333
|
+
}
|
|
334
|
+
throwTypeMismatch("MoveOption", position);
|
|
335
|
+
}
|
|
336
|
+
}
|
|
337
|
+
|
|
338
|
+
throw new Error(`Type mismatch for argument ${position}, expected '${param.toString()}'`);
|
|
339
|
+
}
|