@aptos-labs/ts-sdk 0.0.0
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/LICENSE +201 -0
- package/README.md +144 -0
- package/dist/browser/index.global.js +410 -0
- package/dist/browser/index.global.js.map +1 -0
- package/dist/cjs/index.d.ts +4965 -0
- package/dist/cjs/index.js +4762 -0
- package/dist/cjs/index.js.map +1 -0
- package/dist/esm/index.d.ts +4965 -0
- package/dist/esm/index.mjs +4645 -0
- package/dist/esm/index.mjs.map +1 -0
- package/dist/types/index.d.ts +1247 -0
- package/dist/types/index.js +151 -0
- package/dist/types/index.js.map +1 -0
- package/package.json +79 -0
- package/src/api/account.ts +360 -0
- package/src/api/aptos.ts +103 -0
- package/src/api/aptosConfig.ts +77 -0
- package/src/api/coin.ts +39 -0
- package/src/api/digitalAsset.ts +192 -0
- package/src/api/event.ts +78 -0
- package/src/api/faucet.ts +30 -0
- package/src/api/fungibleAsset.ts +82 -0
- package/src/api/general.ts +188 -0
- package/src/api/index.ts +5 -0
- package/src/api/staking.ts +58 -0
- package/src/api/transaction.ts +135 -0
- package/src/api/transactionSubmission.ts +168 -0
- package/src/bcs/consts.ts +12 -0
- package/src/bcs/deserializer.ts +248 -0
- package/src/bcs/index.ts +9 -0
- package/src/bcs/serializable/entryFunctionBytes.ts +61 -0
- package/src/bcs/serializable/fixedBytes.ts +65 -0
- package/src/bcs/serializable/movePrimitives.ts +211 -0
- package/src/bcs/serializable/moveStructs.ts +462 -0
- package/src/bcs/serializer.ts +353 -0
- package/src/client/core.ts +106 -0
- package/src/client/get.ts +109 -0
- package/src/client/index.ts +7 -0
- package/src/client/post.ts +90 -0
- package/src/client/types.ts +58 -0
- package/src/core/account.ts +180 -0
- package/src/core/accountAddress.ts +407 -0
- package/src/core/authenticationKey.ts +102 -0
- package/src/core/common.ts +40 -0
- package/src/core/crypto/asymmetricCrypto.ts +77 -0
- package/src/core/crypto/ed25519.ts +224 -0
- package/src/core/crypto/index.ts +7 -0
- package/src/core/crypto/multiEd25519.ts +251 -0
- package/src/core/crypto/secp256k1.ts +227 -0
- package/src/core/hex.ts +177 -0
- package/src/core/index.ts +9 -0
- package/src/index.ts +12 -0
- package/src/internal/account.ts +484 -0
- package/src/internal/coin.ts +32 -0
- package/src/internal/digitalAsset.ts +302 -0
- package/src/internal/event.ts +88 -0
- package/src/internal/faucet.ts +41 -0
- package/src/internal/fungibleAsset.ts +114 -0
- package/src/internal/general.ts +160 -0
- package/src/internal/queries/TokenActivitiesFieldsFragment.graphql +17 -0
- package/src/internal/queries/currentTokenOwnershipFieldsFragment.graphql +45 -0
- package/src/internal/queries/getAccountCoinCount.graphql +7 -0
- package/src/internal/queries/getAccountCoinsData.graphql +32 -0
- package/src/internal/queries/getAccountCollectionsWithOwnedTokens.graphql +33 -0
- package/src/internal/queries/getAccountOwnedObjects.graphql +16 -0
- package/src/internal/queries/getAccountOwnedTokens.graphql +11 -0
- package/src/internal/queries/getAccountOwnedTokensByTokenData.graphql +11 -0
- package/src/internal/queries/getAccountOwnedTokensFromCollectionAddress.graphql +11 -0
- package/src/internal/queries/getAccountTokensCount.graphql +7 -0
- package/src/internal/queries/getAccountTransactionsCount.graphql +7 -0
- package/src/internal/queries/getChainTopUserTransactions.graphql +5 -0
- package/src/internal/queries/getCollectionData.graphql +20 -0
- package/src/internal/queries/getCurrentFungibleAssetBalances.graphql +17 -0
- package/src/internal/queries/getDelegatedStakingActivities.graphql +12 -0
- package/src/internal/queries/getEvents.graphql +12 -0
- package/src/internal/queries/getFungibleAssetActivities.graphql +20 -0
- package/src/internal/queries/getFungibleAssetMetadata.graphql +16 -0
- package/src/internal/queries/getNumberOfDelegatorsQuery.graphql +9 -0
- package/src/internal/queries/getProcessorStatus.graphql +7 -0
- package/src/internal/queries/getTokenActivity.graphql +11 -0
- package/src/internal/queries/getTokenCurrentOwner.graphql +11 -0
- package/src/internal/queries/getTokenData.graphql +38 -0
- package/src/internal/staking.ts +68 -0
- package/src/internal/transaction.ts +245 -0
- package/src/internal/transactionSubmission.ts +162 -0
- package/src/transactions/authenticator/account.ts +121 -0
- package/src/transactions/authenticator/transaction.ts +222 -0
- package/src/transactions/instances/chainId.ts +26 -0
- package/src/transactions/instances/identifier.ts +28 -0
- package/src/transactions/instances/index.ts +9 -0
- package/src/transactions/instances/moduleId.ts +53 -0
- package/src/transactions/instances/rawTransaction.ts +199 -0
- package/src/transactions/instances/signedTransaction.ts +43 -0
- package/src/transactions/instances/transactionArgument.ts +37 -0
- package/src/transactions/instances/transactionPayload.ts +407 -0
- package/src/transactions/transaction_builder/transaction_builder.ts +541 -0
- package/src/transactions/typeTag/typeTag.ts +487 -0
- package/src/transactions/types.ts +262 -0
- package/src/types/codegen.yaml +33 -0
- package/src/types/generated/operations.ts +623 -0
- package/src/types/generated/queries.ts +737 -0
- package/src/types/generated/types.ts +10387 -0
- package/src/types/index.ts +944 -0
- package/src/types/indexer.ts +93 -0
- package/src/utils/apiEndpoints.ts +36 -0
- package/src/utils/const.ts +51 -0
- package/src/utils/hdKey.ts +113 -0
- package/src/utils/helpers.ts +12 -0
- package/src/utils/memoize.ts +68 -0
- package/src/version.ts +9 -0
|
@@ -0,0 +1,248 @@
|
|
|
1
|
+
// Copyright © Aptos Foundation
|
|
2
|
+
// SPDX-License-Identifier: Apache-2.0
|
|
3
|
+
|
|
4
|
+
/* eslint-disable no-bitwise */
|
|
5
|
+
import { MAX_U32_NUMBER } from "./consts";
|
|
6
|
+
import { Uint8, Uint16, Uint32, Uint64, Uint128, Uint256 } from "../types";
|
|
7
|
+
|
|
8
|
+
/**
|
|
9
|
+
* This interface exists to define Deserializable<T> inputs for functions that
|
|
10
|
+
* deserialize a byte buffer into a type T.
|
|
11
|
+
* It is not intended to be implemented or extended, because Typescript has no support
|
|
12
|
+
* for static methods in interfaces.
|
|
13
|
+
*/
|
|
14
|
+
export interface Deserializable<T> {
|
|
15
|
+
deserialize(deserializer: Deserializer): T;
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
export class Deserializer {
|
|
19
|
+
private buffer: ArrayBuffer;
|
|
20
|
+
|
|
21
|
+
private offset: number;
|
|
22
|
+
|
|
23
|
+
constructor(data: Uint8Array) {
|
|
24
|
+
// copies data to prevent outside mutation of buffer.
|
|
25
|
+
this.buffer = new ArrayBuffer(data.length);
|
|
26
|
+
new Uint8Array(this.buffer).set(data, 0);
|
|
27
|
+
this.offset = 0;
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
private read(length: number): ArrayBuffer {
|
|
31
|
+
if (this.offset + length > this.buffer.byteLength) {
|
|
32
|
+
throw new Error("Reached to the end of buffer");
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
const bytes = this.buffer.slice(this.offset, this.offset + length);
|
|
36
|
+
this.offset += length;
|
|
37
|
+
return bytes;
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
/**
|
|
41
|
+
* Deserializes a string. UTF8 string is supported. Reads the string's bytes length "l" first,
|
|
42
|
+
* and then reads "l" bytes of content. Decodes the byte array into a string.
|
|
43
|
+
*
|
|
44
|
+
* BCS layout for "string": string_length | string_content
|
|
45
|
+
* where string_length is a u32 integer encoded as a uleb128 integer, equal to the number of bytes in string_content.
|
|
46
|
+
*
|
|
47
|
+
* @example
|
|
48
|
+
* ```ts
|
|
49
|
+
* const deserializer = new Deserializer(new Uint8Array([8, 49, 50, 51, 52, 97, 98, 99, 100]));
|
|
50
|
+
* assert(deserializer.deserializeStr() === "1234abcd");
|
|
51
|
+
* ```
|
|
52
|
+
*/
|
|
53
|
+
deserializeStr(): string {
|
|
54
|
+
const value = this.deserializeBytes();
|
|
55
|
+
const textDecoder = new TextDecoder();
|
|
56
|
+
return textDecoder.decode(value);
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
/**
|
|
60
|
+
* Deserializes an array of bytes.
|
|
61
|
+
*
|
|
62
|
+
* BCS layout for "bytes": bytes_length | bytes
|
|
63
|
+
* where bytes_length is a u32 integer encoded as a uleb128 integer, equal to the length of the bytes array.
|
|
64
|
+
*/
|
|
65
|
+
deserializeBytes(): Uint8Array {
|
|
66
|
+
const len = this.deserializeUleb128AsU32();
|
|
67
|
+
return new Uint8Array(this.read(len));
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
/**
|
|
71
|
+
* Deserializes an array of bytes. The number of bytes to read is already known.
|
|
72
|
+
*
|
|
73
|
+
*/
|
|
74
|
+
deserializeFixedBytes(len: number): Uint8Array {
|
|
75
|
+
return new Uint8Array(this.read(len));
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
/**
|
|
79
|
+
* Deserializes a boolean value.
|
|
80
|
+
*
|
|
81
|
+
* BCS layout for "boolean": One byte. "0x01" for true and "0x00" for false.
|
|
82
|
+
*/
|
|
83
|
+
deserializeBool(): boolean {
|
|
84
|
+
const bool = new Uint8Array(this.read(1))[0];
|
|
85
|
+
if (bool !== 1 && bool !== 0) {
|
|
86
|
+
throw new Error("Invalid boolean value");
|
|
87
|
+
}
|
|
88
|
+
return bool === 1;
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
/**
|
|
92
|
+
* Deserializes a uint8 number.
|
|
93
|
+
*
|
|
94
|
+
* BCS layout for "uint8": One byte. Binary format in little-endian representation.
|
|
95
|
+
*/
|
|
96
|
+
deserializeU8(): Uint8 {
|
|
97
|
+
return new DataView(this.read(1)).getUint8(0);
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
/**
|
|
101
|
+
* Deserializes a uint16 number.
|
|
102
|
+
*
|
|
103
|
+
* BCS layout for "uint16": Two bytes. Binary format in little-endian representation.
|
|
104
|
+
* @example
|
|
105
|
+
* ```ts
|
|
106
|
+
* const deserializer = new Deserializer(new Uint8Array([0x34, 0x12]));
|
|
107
|
+
* assert(deserializer.deserializeU16() === 4660);
|
|
108
|
+
* ```
|
|
109
|
+
*/
|
|
110
|
+
deserializeU16(): Uint16 {
|
|
111
|
+
return new DataView(this.read(2)).getUint16(0, true);
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
/**
|
|
115
|
+
* Deserializes a uint32 number.
|
|
116
|
+
*
|
|
117
|
+
* BCS layout for "uint32": Four bytes. Binary format in little-endian representation.
|
|
118
|
+
* @example
|
|
119
|
+
* ```ts
|
|
120
|
+
* const deserializer = new Deserializer(new Uint8Array([0x78, 0x56, 0x34, 0x12]));
|
|
121
|
+
* assert(deserializer.deserializeU32() === 305419896);
|
|
122
|
+
* ```
|
|
123
|
+
*/
|
|
124
|
+
deserializeU32(): Uint32 {
|
|
125
|
+
return new DataView(this.read(4)).getUint32(0, true);
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
/**
|
|
129
|
+
* Deserializes a uint64 number.
|
|
130
|
+
*
|
|
131
|
+
* BCS layout for "uint64": Eight bytes. Binary format in little-endian representation.
|
|
132
|
+
* @example
|
|
133
|
+
* ```ts
|
|
134
|
+
* const deserializer = new Deserializer(new Uint8Array([0x00, 0xEF, 0xCD, 0xAB, 0x78, 0x56, 0x34, 0x12]));
|
|
135
|
+
* assert(deserializer.deserializeU64() === 1311768467750121216);
|
|
136
|
+
* ```
|
|
137
|
+
*/
|
|
138
|
+
deserializeU64(): Uint64 {
|
|
139
|
+
const low = this.deserializeU32();
|
|
140
|
+
const high = this.deserializeU32();
|
|
141
|
+
|
|
142
|
+
// combine the two 32-bit values and return (little endian)
|
|
143
|
+
return BigInt((BigInt(high) << BigInt(32)) | BigInt(low));
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
/**
|
|
147
|
+
* Deserializes a uint128 number.
|
|
148
|
+
*
|
|
149
|
+
* BCS layout for "uint128": Sixteen bytes. Binary format in little-endian representation.
|
|
150
|
+
*/
|
|
151
|
+
deserializeU128(): Uint128 {
|
|
152
|
+
const low = this.deserializeU64();
|
|
153
|
+
const high = this.deserializeU64();
|
|
154
|
+
|
|
155
|
+
// combine the two 64-bit values and return (little endian)
|
|
156
|
+
return BigInt((high << BigInt(64)) | low);
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
/**
|
|
160
|
+
* Deserializes a uint256 number.
|
|
161
|
+
*
|
|
162
|
+
* BCS layout for "uint256": Thirty-two bytes. Binary format in little-endian representation.
|
|
163
|
+
*/
|
|
164
|
+
deserializeU256(): Uint256 {
|
|
165
|
+
const low = this.deserializeU128();
|
|
166
|
+
const high = this.deserializeU128();
|
|
167
|
+
|
|
168
|
+
// combine the two 128-bit values and return (little endian)
|
|
169
|
+
return BigInt((high << BigInt(128)) | low);
|
|
170
|
+
}
|
|
171
|
+
|
|
172
|
+
/**
|
|
173
|
+
* Deserializes a uleb128 encoded uint32 number.
|
|
174
|
+
*
|
|
175
|
+
* BCS use uleb128 encoding in two cases: (1) lengths of variable-length sequences and (2) tags of enum values
|
|
176
|
+
*/
|
|
177
|
+
deserializeUleb128AsU32(): Uint32 {
|
|
178
|
+
let value: bigint = BigInt(0);
|
|
179
|
+
let shift = 0;
|
|
180
|
+
|
|
181
|
+
while (value < MAX_U32_NUMBER) {
|
|
182
|
+
const byte = this.deserializeU8();
|
|
183
|
+
value |= BigInt(byte & 0x7f) << BigInt(shift);
|
|
184
|
+
|
|
185
|
+
if ((byte & 0x80) === 0) {
|
|
186
|
+
break;
|
|
187
|
+
}
|
|
188
|
+
shift += 7;
|
|
189
|
+
}
|
|
190
|
+
|
|
191
|
+
if (value > MAX_U32_NUMBER) {
|
|
192
|
+
throw new Error("Overflow while parsing uleb128-encoded uint32 value");
|
|
193
|
+
}
|
|
194
|
+
|
|
195
|
+
return Number(value);
|
|
196
|
+
}
|
|
197
|
+
|
|
198
|
+
/**
|
|
199
|
+
* Helper function that primarily exists to support alternative syntax for deserialization.
|
|
200
|
+
* That is, if we have a `const deserializer: new Deserializer(...)`, instead of having to use
|
|
201
|
+
* `MyClass.deserialize(deserializer)`, we can call `deserializer.deserialize(MyClass)`.
|
|
202
|
+
*
|
|
203
|
+
* @example const deserializer = new Deserializer(new Uint8Array([1, 2, 3]));
|
|
204
|
+
* const value = deserializer.deserialize(MyClass); // where MyClass has a `deserialize` function
|
|
205
|
+
* // value is now an instance of MyClass
|
|
206
|
+
* // equivalent to `const value = MyClass.deserialize(deserializer)`
|
|
207
|
+
* @param cls The BCS-deserializable class to deserialize the buffered bytes into.
|
|
208
|
+
*
|
|
209
|
+
* @returns the deserialized value of class type T
|
|
210
|
+
*/
|
|
211
|
+
deserialize<T>(cls: Deserializable<T>): T {
|
|
212
|
+
// NOTE: `deserialize` in `cls.deserialize(this)` here is a static method defined in `cls`,
|
|
213
|
+
// It is separate from the `deserialize` instance method defined here in Deserializer.
|
|
214
|
+
return cls.deserialize(this);
|
|
215
|
+
}
|
|
216
|
+
|
|
217
|
+
/**
|
|
218
|
+
* Deserializes an array of BCS Deserializable values given an existing Deserializer
|
|
219
|
+
* instance with a loaded byte buffer.
|
|
220
|
+
*
|
|
221
|
+
* @param cls The BCS-deserializable class to deserialize the buffered bytes into.
|
|
222
|
+
* @example
|
|
223
|
+
* // serialize a vector of addresses
|
|
224
|
+
* const addresses = new Array<AccountAddress>(
|
|
225
|
+
* AccountAddress.fromHexInputRelaxed("0x1"),
|
|
226
|
+
* AccountAddress.fromHexInputRelaxed("0x2"),
|
|
227
|
+
* AccountAddress.fromHexInputRelaxed("0xa"),
|
|
228
|
+
* AccountAddress.fromHexInputRelaxed("0xb"),
|
|
229
|
+
* );
|
|
230
|
+
* const serializer = new Serializer();
|
|
231
|
+
* serializer.serializeVector(addresses);
|
|
232
|
+
* const serializedBytes = serializer.toUint8Array();
|
|
233
|
+
*
|
|
234
|
+
* // deserialize the bytes into an array of addresses
|
|
235
|
+
* const deserializer = new Deserializer(serializedBytes);
|
|
236
|
+
* const deserializedAddresses = deserializer.deserializeVector(AccountAddress);
|
|
237
|
+
* // deserializedAddresses is now an array of AccountAddress instances
|
|
238
|
+
* @returns an array of deserialized values of type T
|
|
239
|
+
*/
|
|
240
|
+
deserializeVector<T>(cls: Deserializable<T>): Array<T> {
|
|
241
|
+
const length = this.deserializeUleb128AsU32();
|
|
242
|
+
const vector = new Array<T>();
|
|
243
|
+
for (let i = 0; i < length; i += 1) {
|
|
244
|
+
vector.push(this.deserialize(cls));
|
|
245
|
+
}
|
|
246
|
+
return vector;
|
|
247
|
+
}
|
|
248
|
+
}
|
package/src/bcs/index.ts
ADDED
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
// Copyright © Aptos Foundation
|
|
2
|
+
// SPDX-License-Identifier: Apache-2.0
|
|
3
|
+
|
|
4
|
+
export * from "./deserializer";
|
|
5
|
+
export * from "./serializer";
|
|
6
|
+
export * from "./serializable/entryFunctionBytes";
|
|
7
|
+
export * from "./serializable/fixedBytes";
|
|
8
|
+
export * from "./serializable/movePrimitives";
|
|
9
|
+
export * from "./serializable/moveStructs";
|
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
// Copyright © Aptos Foundation
|
|
2
|
+
// SPDX-License-Identifier: Apache-2.0
|
|
3
|
+
|
|
4
|
+
import { Serializer, Serializable } from "../serializer";
|
|
5
|
+
import { Deserializer } from "../deserializer";
|
|
6
|
+
import { FixedBytes } from "./fixedBytes";
|
|
7
|
+
import { EntryFunctionArgument } from "../../transactions/instances/transactionArgument";
|
|
8
|
+
import { HexInput } from "../../types";
|
|
9
|
+
|
|
10
|
+
/**
|
|
11
|
+
* This class exists solely to represent a sequence of fixed bytes as a serialized entry function, because
|
|
12
|
+
* serializing an entry function appends a prefix that's *only* used for entry function arguments.
|
|
13
|
+
*
|
|
14
|
+
* NOTE: Attempting to use this class for a serialized script function will result in erroneous
|
|
15
|
+
* and unexpected behavior.
|
|
16
|
+
*
|
|
17
|
+
* If you wish to convert this class back to a TransactionArgument, you must know the type
|
|
18
|
+
* of the argument beforehand, and use the appropriate class to deserialize the bytes within
|
|
19
|
+
* an instance of this class.
|
|
20
|
+
*/
|
|
21
|
+
export class EntryFunctionBytes extends Serializable implements EntryFunctionArgument {
|
|
22
|
+
public readonly value: FixedBytes;
|
|
23
|
+
|
|
24
|
+
private constructor(value: HexInput) {
|
|
25
|
+
super();
|
|
26
|
+
this.value = new FixedBytes(value);
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
// Note that to see the Move, BCS-serialized representation of the underlying fixed byte vector,
|
|
30
|
+
// we must not serialize the length prefix.
|
|
31
|
+
//
|
|
32
|
+
// In other words, this class is only used to represent a sequence of bytes that are already
|
|
33
|
+
// BCS-serialized as a type. To represent those bytes accurately, the BCS-serialized form is the same exact
|
|
34
|
+
// representation.
|
|
35
|
+
serialize(serializer: Serializer): void {
|
|
36
|
+
serializer.serialize(this.value);
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
// When we serialize these bytes as an entry function argument, we need to
|
|
40
|
+
// serialize the length prefix. This essentially converts the underlying fixed byte vector to a type-agnostic
|
|
41
|
+
// byte vector to an `any` type.
|
|
42
|
+
// NOTE: This, and the lack of a `serializeForScriptFunction`, is the only meaningful difference between this
|
|
43
|
+
// class and FixedBytes.
|
|
44
|
+
serializeForEntryFunction(serializer: Serializer): void {
|
|
45
|
+
serializer.serializeU32AsUleb128(this.value.value.length);
|
|
46
|
+
serializer.serialize(this);
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
/**
|
|
50
|
+
* The only way to create an instance of this class is to use this static method.
|
|
51
|
+
*
|
|
52
|
+
* This function should only be used when deserializing a sequence of EntryFunctionPayload arguments.
|
|
53
|
+
* @param deserializer the deserializer instance with the buffered bytes
|
|
54
|
+
* @param length the length of the bytes to deserialize
|
|
55
|
+
* @returns an instance of this class, which will now only be usable as an EntryFunctionArgument
|
|
56
|
+
*/
|
|
57
|
+
static deserialize(deserializer: Deserializer, length: number): EntryFunctionBytes {
|
|
58
|
+
const fixedBytes = FixedBytes.deserialize(deserializer, length);
|
|
59
|
+
return new EntryFunctionBytes(fixedBytes.value);
|
|
60
|
+
}
|
|
61
|
+
}
|
|
@@ -0,0 +1,65 @@
|
|
|
1
|
+
// Copyright © Aptos Foundation
|
|
2
|
+
// SPDX-License-Identifier: Apache-2.0
|
|
3
|
+
|
|
4
|
+
import { Serializer, Serializable } from "../serializer";
|
|
5
|
+
import { Deserializer } from "../deserializer";
|
|
6
|
+
import { HexInput } from "../../types";
|
|
7
|
+
import { Hex } from "../../core/hex";
|
|
8
|
+
import { TransactionArgument } from "../../transactions/instances/transactionArgument";
|
|
9
|
+
|
|
10
|
+
/**
|
|
11
|
+
* This class exists to represent a contiguous sequence of already serialized BCS-bytes.
|
|
12
|
+
*
|
|
13
|
+
* It differs from most other Serializable classes in that its internal byte buffer is serialized to BCS
|
|
14
|
+
* bytes exactly as-is, without prepending the length of the bytes.
|
|
15
|
+
*
|
|
16
|
+
* If you want to write your own serialization function and pass the bytes as a transaction argument,
|
|
17
|
+
* you should use this class.
|
|
18
|
+
*
|
|
19
|
+
* This class is also more generally used to represent type-agnostic BCS bytes as a vector<u8>.
|
|
20
|
+
*
|
|
21
|
+
* An example of this is the bytes resulting from entry function arguments that have been serialized
|
|
22
|
+
* for an entry function.
|
|
23
|
+
*
|
|
24
|
+
* @example
|
|
25
|
+
* const yourCustomSerializedBytes = new Uint8Array([1, 2, 3, 4, 5, 6, 7, 8]);
|
|
26
|
+
* const fixedBytes = new FixedBytes(yourCustomSerializedBytes);
|
|
27
|
+
* const payload = generateTransactionPayload({
|
|
28
|
+
* function: "0xbeefcafe::your_module::your_function_that_requires_custom_serialization",
|
|
29
|
+
* arguments: [yourCustomBytes],
|
|
30
|
+
* });
|
|
31
|
+
*
|
|
32
|
+
* For example, if you store each of the 32 bytes for an address as a U8 in a MoveVector<U8>, when you
|
|
33
|
+
* serialize that MoveVector<U8>, it will be serialized to 33 bytes. If you solely want to pass around
|
|
34
|
+
* the 32 bytes as a Serializable class that *does not* prepend the length to the BCS-serialized representation,
|
|
35
|
+
* use this class.
|
|
36
|
+
*
|
|
37
|
+
* @params value: HexInput representing a sequence of Uint8 bytes
|
|
38
|
+
* @returns a Serializable FixedBytes instance, which when serialized, does not prepend the length of the bytes
|
|
39
|
+
* @see EntryFunctionBytes
|
|
40
|
+
*/
|
|
41
|
+
export class FixedBytes extends Serializable implements TransactionArgument {
|
|
42
|
+
public value: Uint8Array;
|
|
43
|
+
|
|
44
|
+
constructor(value: HexInput) {
|
|
45
|
+
super();
|
|
46
|
+
this.value = Hex.fromHexInput(value).toUint8Array();
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
serialize(serializer: Serializer): void {
|
|
50
|
+
serializer.serializeFixedBytes(this.value);
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
serializeForEntryFunction(serializer: Serializer): void {
|
|
54
|
+
serializer.serialize(this);
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
serializeForScriptFunction(serializer: Serializer): void {
|
|
58
|
+
serializer.serialize(this);
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
static deserialize(deserializer: Deserializer, length: number): FixedBytes {
|
|
62
|
+
const bytes = deserializer.deserializeFixedBytes(length);
|
|
63
|
+
return new FixedBytes(bytes);
|
|
64
|
+
}
|
|
65
|
+
}
|
|
@@ -0,0 +1,211 @@
|
|
|
1
|
+
// Copyright © Aptos Foundation
|
|
2
|
+
// SPDX-License-Identifier: Apache-2.0
|
|
3
|
+
|
|
4
|
+
import {
|
|
5
|
+
MAX_U128_BIG_INT,
|
|
6
|
+
MAX_U16_NUMBER,
|
|
7
|
+
MAX_U32_NUMBER,
|
|
8
|
+
MAX_U64_BIG_INT,
|
|
9
|
+
MAX_U8_NUMBER,
|
|
10
|
+
MAX_U256_BIG_INT,
|
|
11
|
+
} from "../consts";
|
|
12
|
+
import { Deserializer } from "../deserializer";
|
|
13
|
+
import { Serializable, Serializer, ensureBoolean, validateNumberInRange } from "../serializer";
|
|
14
|
+
import { TransactionArgument } from "../../transactions/instances/transactionArgument";
|
|
15
|
+
import { AnyNumber, Uint16, Uint32, Uint8, ScriptTransactionArgumentVariants } from "../../types";
|
|
16
|
+
|
|
17
|
+
export class Bool extends Serializable implements TransactionArgument {
|
|
18
|
+
public readonly value: boolean;
|
|
19
|
+
|
|
20
|
+
constructor(value: boolean) {
|
|
21
|
+
super();
|
|
22
|
+
ensureBoolean(value);
|
|
23
|
+
this.value = value;
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
serialize(serializer: Serializer): void {
|
|
27
|
+
serializer.serializeBool(this.value);
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
serializeForEntryFunction(serializer: Serializer): void {
|
|
31
|
+
const bcsBytes = this.bcsToBytes();
|
|
32
|
+
serializer.serializeBytes(bcsBytes);
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
serializeForScriptFunction(serializer: Serializer): void {
|
|
36
|
+
serializer.serializeU32AsUleb128(ScriptTransactionArgumentVariants.Bool);
|
|
37
|
+
serializer.serialize(this);
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
static deserialize(deserializer: Deserializer): Bool {
|
|
41
|
+
return new Bool(deserializer.deserializeBool());
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
export class U8 extends Serializable implements TransactionArgument {
|
|
46
|
+
public readonly value: Uint8;
|
|
47
|
+
|
|
48
|
+
constructor(value: Uint8) {
|
|
49
|
+
super();
|
|
50
|
+
validateNumberInRange(value, 0, MAX_U8_NUMBER);
|
|
51
|
+
this.value = value;
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
serialize(serializer: Serializer): void {
|
|
55
|
+
serializer.serializeU8(this.value);
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
serializeForEntryFunction(serializer: Serializer): void {
|
|
59
|
+
const bcsBytes = this.bcsToBytes();
|
|
60
|
+
serializer.serializeBytes(bcsBytes);
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
serializeForScriptFunction(serializer: Serializer): void {
|
|
64
|
+
serializer.serializeU32AsUleb128(ScriptTransactionArgumentVariants.U8);
|
|
65
|
+
serializer.serialize(this);
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
static deserialize(deserializer: Deserializer): U8 {
|
|
69
|
+
return new U8(deserializer.deserializeU8());
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
export class U16 extends Serializable implements TransactionArgument {
|
|
74
|
+
public readonly value: Uint16;
|
|
75
|
+
|
|
76
|
+
constructor(value: Uint16) {
|
|
77
|
+
super();
|
|
78
|
+
validateNumberInRange(value, 0, MAX_U16_NUMBER);
|
|
79
|
+
this.value = value;
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
serialize(serializer: Serializer): void {
|
|
83
|
+
serializer.serializeU16(this.value);
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
serializeForEntryFunction(serializer: Serializer): void {
|
|
87
|
+
const bcsBytes = this.bcsToBytes();
|
|
88
|
+
serializer.serializeBytes(bcsBytes);
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
serializeForScriptFunction(serializer: Serializer): void {
|
|
92
|
+
serializer.serializeU32AsUleb128(ScriptTransactionArgumentVariants.U16);
|
|
93
|
+
serializer.serialize(this);
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
static deserialize(deserializer: Deserializer): U16 {
|
|
97
|
+
return new U16(deserializer.deserializeU16());
|
|
98
|
+
}
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
export class U32 extends Serializable implements TransactionArgument {
|
|
102
|
+
public readonly value: Uint32;
|
|
103
|
+
|
|
104
|
+
constructor(value: Uint32) {
|
|
105
|
+
super();
|
|
106
|
+
validateNumberInRange(value, 0, MAX_U32_NUMBER);
|
|
107
|
+
this.value = value;
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
serialize(serializer: Serializer): void {
|
|
111
|
+
serializer.serializeU32(this.value);
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
serializeForEntryFunction(serializer: Serializer): void {
|
|
115
|
+
const bcsBytes = this.bcsToBytes();
|
|
116
|
+
serializer.serializeBytes(bcsBytes);
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
serializeForScriptFunction(serializer: Serializer): void {
|
|
120
|
+
serializer.serializeU32AsUleb128(ScriptTransactionArgumentVariants.U32);
|
|
121
|
+
serializer.serialize(this);
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
static deserialize(deserializer: Deserializer): U32 {
|
|
125
|
+
return new U32(deserializer.deserializeU32());
|
|
126
|
+
}
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
export class U64 extends Serializable implements TransactionArgument {
|
|
130
|
+
public readonly value: bigint;
|
|
131
|
+
|
|
132
|
+
constructor(value: AnyNumber) {
|
|
133
|
+
super();
|
|
134
|
+
validateNumberInRange(value, BigInt(0), MAX_U64_BIG_INT);
|
|
135
|
+
this.value = BigInt(value);
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
serialize(serializer: Serializer): void {
|
|
139
|
+
serializer.serializeU64(this.value);
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
serializeForEntryFunction(serializer: Serializer): void {
|
|
143
|
+
const bcsBytes = this.bcsToBytes();
|
|
144
|
+
serializer.serializeBytes(bcsBytes);
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
serializeForScriptFunction(serializer: Serializer): void {
|
|
148
|
+
serializer.serializeU32AsUleb128(ScriptTransactionArgumentVariants.U64);
|
|
149
|
+
serializer.serialize(this);
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
static deserialize(deserializer: Deserializer): U64 {
|
|
153
|
+
return new U64(deserializer.deserializeU64());
|
|
154
|
+
}
|
|
155
|
+
}
|
|
156
|
+
|
|
157
|
+
export class U128 extends Serializable implements TransactionArgument {
|
|
158
|
+
public readonly value: bigint;
|
|
159
|
+
|
|
160
|
+
constructor(value: AnyNumber) {
|
|
161
|
+
super();
|
|
162
|
+
validateNumberInRange(value, BigInt(0), MAX_U128_BIG_INT);
|
|
163
|
+
this.value = BigInt(value);
|
|
164
|
+
}
|
|
165
|
+
|
|
166
|
+
serialize(serializer: Serializer): void {
|
|
167
|
+
serializer.serializeU128(this.value);
|
|
168
|
+
}
|
|
169
|
+
|
|
170
|
+
serializeForEntryFunction(serializer: Serializer): void {
|
|
171
|
+
const bcsBytes = this.bcsToBytes();
|
|
172
|
+
serializer.serializeBytes(bcsBytes);
|
|
173
|
+
}
|
|
174
|
+
|
|
175
|
+
serializeForScriptFunction(serializer: Serializer): void {
|
|
176
|
+
serializer.serializeU32AsUleb128(ScriptTransactionArgumentVariants.U128);
|
|
177
|
+
serializer.serialize(this);
|
|
178
|
+
}
|
|
179
|
+
|
|
180
|
+
static deserialize(deserializer: Deserializer): U128 {
|
|
181
|
+
return new U128(deserializer.deserializeU128());
|
|
182
|
+
}
|
|
183
|
+
}
|
|
184
|
+
|
|
185
|
+
export class U256 extends Serializable implements TransactionArgument {
|
|
186
|
+
public readonly value: bigint;
|
|
187
|
+
|
|
188
|
+
constructor(value: AnyNumber) {
|
|
189
|
+
super();
|
|
190
|
+
validateNumberInRange(value, BigInt(0), MAX_U256_BIG_INT);
|
|
191
|
+
this.value = BigInt(value);
|
|
192
|
+
}
|
|
193
|
+
|
|
194
|
+
serialize(serializer: Serializer): void {
|
|
195
|
+
serializer.serializeU256(this.value);
|
|
196
|
+
}
|
|
197
|
+
|
|
198
|
+
serializeForEntryFunction(serializer: Serializer): void {
|
|
199
|
+
const bcsBytes = this.bcsToBytes();
|
|
200
|
+
serializer.serializeBytes(bcsBytes);
|
|
201
|
+
}
|
|
202
|
+
|
|
203
|
+
serializeForScriptFunction(serializer: Serializer): void {
|
|
204
|
+
serializer.serializeU32AsUleb128(ScriptTransactionArgumentVariants.U256);
|
|
205
|
+
serializer.serialize(this);
|
|
206
|
+
}
|
|
207
|
+
|
|
208
|
+
static deserialize(deserializer: Deserializer): U256 {
|
|
209
|
+
return new U256(deserializer.deserializeU256());
|
|
210
|
+
}
|
|
211
|
+
}
|