New Case Study:See how Anthropic automated 95% of dependency reviews with Socket.Learn More
Socket
Sign inDemoInstall
Socket

aptos

Package Overview
Dependencies
Maintainers
1
Versions
70
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

aptos - npm Package Compare versions

Comparing version 1.2.1 to 1.2.2

src/transaction_builder/aptos_types/abi.test.ts

25

CHANGELOG.md

@@ -5,2 +5,27 @@ # Changelog

### 1.2.2 (2022-07-27)
### Features
* add ABI interfaces to aptos client ([1d0fe29](https://github.com/aptos-labs/aptos-core/commit/1d0fe29cc4d2c7b9bd1b19f95059e532ef4a4936))
* add abi support to TS SDK ([f0214b4](https://github.com/aptos-labs/aptos-core/commit/f0214b4deec4f3ab401782c7f4d793431f1f3e9c))
* add UserTransaction and hashing capability ([9168969](https://github.com/aptos-labs/aptos-core/commit/916896947c629fe02588d5c5977bff2fecf587ac))
* allow TransactionBuilder to build raw transactions with binary ABI ([fe0c325](https://github.com/aptos-labs/aptos-core/commit/fe0c325a3e853ba5bf809a3878a8edaa350a2068))
* deprecate getTokenBalance api in SDK ([2ec554e](https://github.com/aptos-labs/aptos-core/commit/2ec554e6e40a81cee4e760f6f84ef7362c570240))
* memoize chain id in aptos client ([#1589](https://github.com/aptos-labs/aptos-core/issues/1589)) ([4a6453b](https://github.com/aptos-labs/aptos-core/commit/4a6453bf0e620247557854053b661446bff807a7))
* **mutiagent:** support multiagent transaction submission ([#1543](https://github.com/aptos-labs/aptos-core/issues/1543)) ([0f0c70e](https://github.com/aptos-labs/aptos-core/commit/0f0c70e8ed2fefa952f0c89b7edb78edc174cb49))
* reimplement type tag parser ([67473a1](https://github.com/aptos-labs/aptos-core/commit/67473a1c35616733442a38055aa2c0440aa1315f))
* support retrieving token balance for any account ([7f93c21](https://github.com/aptos-labs/aptos-core/commit/7f93c2100f8b8e848461a0b5a395bfb76ade8667))
* **ts-sdk:** accepts string parameter as vec<u8> ([0daade4](https://github.com/aptos-labs/aptos-core/commit/0daade4f734d1ba29a896b00d7ddde2249e87970))
* **ts-sdk:** add a transaction builder that supports ABI ([95296a7](https://github.com/aptos-labs/aptos-core/commit/95296a7b75c5765214005054975b71d5d2215263))
* **ts-sdk:** e2e test for ABI interface ([edad199](https://github.com/aptos-labs/aptos-core/commit/edad1994b2e7501733256d20251e45b0d46646da))
* update move to latest version ([23a953b](https://github.com/aptos-labs/aptos-core/commit/23a953b3f1a222a71d496222f0dcd8ce17fc8cac))
### Bug Fixes
* get rid of "natual" calls ([#1678](https://github.com/aptos-labs/aptos-core/issues/1678)) ([54601f7](https://github.com/aptos-labs/aptos-core/commit/54601f79206ea0f8b8b1b0d6599d31832fc4d195))
* **ts-sdk:** fix a typo, natual now becomes natural ([1b7d295](https://github.com/aptos-labs/aptos-core/commit/1b7d2957b79a5d2821ada0c5096cf43c412e0c2d)), closes [#1526](https://github.com/aptos-labs/aptos-core/issues/1526)
### 1.2.1 (2022-07-23)

@@ -7,0 +32,0 @@

8

examples/javascript/index.js

@@ -13,3 +13,3 @@ const aptos = require("aptos");

let resources = await client.getAccountResources(account1.address());
let accountResource = resources.find((r) => r.type === "0x1::coin::CoinStore<0x1::test_coin::TestCoin>");
let accountResource = resources.find((r) => r.type === "0x1::coin::CoinStore<0x1::aptos_coin::AptosCoin>");
console.log(`account2 coins: ${accountResource.data.coin.value}. Should be 5000!`);

@@ -20,3 +20,3 @@

resources = await client.getAccountResources(account2.address());
accountResource = resources.find((r) => r.type === "0x1::coin::CoinStore<0x1::test_coin::TestCoin>");
accountResource = resources.find((r) => r.type === "0x1::coin::CoinStore<0x1::aptos_coin::AptosCoin>");
console.log(`account2 coins: ${accountResource.data.coin.value}. Should be 0!`);

@@ -27,3 +27,3 @@

function: "0x1::coin::transfer",
type_arguments: ["0x1::test_coin::TestCoin"],
type_arguments: ["0x1::aptos_coin::AptosCoin"],
arguments: [account2.address().hex(), "717"],

@@ -37,4 +37,4 @@ };

resources = await client.getAccountResources(account2.address());
accountResource = resources.find((r) => r.type === "0x1::coin::CoinStore<0x1::test_coin::TestCoin>");
accountResource = resources.find((r) => r.type === "0x1::coin::CoinStore<0x1::aptos_coin::AptosCoin>");
console.log(`account2 coins: ${accountResource.data.coin.value}. Should be 717!`);
})();

@@ -27,6 +27,6 @@ /* eslint-disable no-console */

const account1 = new AptosAccount();
// Creates the account on Aptos chain and fund the account with 5000 TestCoin
// Creates the account on Aptos chain and fund the account with 5000 AptosCoin
await faucetClient.fundAccount(account1.address(), 5000);
let resources = await client.getAccountResources(account1.address());
let accountResource = resources.find((r) => r.type === "0x1::coin::CoinStore<0x1::test_coin::TestCoin>");
let accountResource = resources.find((r) => r.type === "0x1::coin::CoinStore<0x1::aptos_coin::AptosCoin>");
let balance = parseInt((accountResource?.data as any).coin.value);

@@ -37,6 +37,6 @@ assert(balance === 5000);

const account2 = new AptosAccount();
// Creates the second account and fund the account with 0 TestCoin
// Creates the second account and fund the account with 0 AptosCoin
await faucetClient.fundAccount(account2.address(), 0);
resources = await client.getAccountResources(account2.address());
accountResource = resources.find((r) => r.type === "0x1::coin::CoinStore<0x1::test_coin::TestCoin>");
accountResource = resources.find((r) => r.type === "0x1::coin::CoinStore<0x1::aptos_coin::AptosCoin>");
balance = parseInt((accountResource?.data as any).coin.value);

@@ -46,3 +46,3 @@ assert(balance === 0);

const token = new TypeTagStruct(StructTag.fromString("0x1::test_coin::TestCoin"));
const token = new TypeTagStruct(StructTag.fromString("0x1::aptos_coin::AptosCoin"));

@@ -64,3 +64,3 @@ // TS SDK support 3 types of transaction payloads: `ScriptFunction`, `Script` and `Module`.

const [{ sequence_number: sequnceNumber }, chainId] = await Promise.all([
const [{ sequence_number: sequenceNumber }, chainId] = await Promise.all([
client.getAccount(account1.address()),

@@ -75,3 +75,3 @@ client.getChainId(),

AccountAddress.fromHex(account1.address()),
BigInt(sequnceNumber),
BigInt(sequenceNumber),
scriptFunctionPayload,

@@ -95,3 +95,3 @@ // Max gas unit to spend

resources = await client.getAccountResources(account2.address());
accountResource = resources.find((r) => r.type === "0x1::coin::CoinStore<0x1::test_coin::TestCoin>");
accountResource = resources.find((r) => r.type === "0x1::coin::CoinStore<0x1::aptos_coin::AptosCoin>");
balance = parseInt((accountResource?.data as any).coin.value);

@@ -98,0 +98,0 @@ assert(balance === 717);

@@ -14,3 +14,3 @@ /* eslint-disable no-console */

let resources = await client.getAccountResources(account1.address());
let accountResource = resources.find((r) => r.type === "0x1::coin::CoinStore<0x1::test_coin::TestCoin>");
let accountResource = resources.find((r) => r.type === "0x1::coin::CoinStore<0x1::aptos_coin::AptosCoin>");
let balance = (accountResource.data as { coin: { value: string } }).coin.value;

@@ -22,3 +22,3 @@ console.log(`account1 coins: ${balance}. Should be 5000!`);

resources = await client.getAccountResources(account2.address());
accountResource = resources.find((r) => r.type === "0x1::coin::CoinStore<0x1::test_coin::TestCoin>");
accountResource = resources.find((r) => r.type === "0x1::coin::CoinStore<0x1::aptos_coin::AptosCoin>");
balance = (accountResource.data as { coin: { value: string } }).coin.value;

@@ -30,3 +30,3 @@ console.log(`account2 coins: ${balance}. Should be 0!`);

function: "0x1::coin::transfer",
type_arguments: ["0x1::test_coin::TestCoin"],
type_arguments: ["0x1::aptos_coin::AptosCoin"],
arguments: [account2.address().hex(), "717"],

@@ -40,5 +40,5 @@ };

resources = await client.getAccountResources(account2.address());
accountResource = resources.find((r) => r.type === "0x1::coin::CoinStore<0x1::test_coin::TestCoin>");
accountResource = resources.find((r) => r.type === "0x1::coin::CoinStore<0x1::aptos_coin::AptosCoin>");
balance = (accountResource.data as { coin: { value: string } }).coin.value;
console.log(`account2 coins: ${balance}. Should be 717!`);
})();

@@ -40,3 +40,3 @@ /* eslint-disable no-console */

// Derive the multisig account address and fund the address with 5000 TestCoin.
// Derive the multisig account address and fund the address with 5000 AptosCoin.
const mutisigAccountAddress = authKey.derivedAddress();

@@ -46,3 +46,3 @@ await faucetClient.fundAccount(mutisigAccountAddress, 5000);

let resources = await client.getAccountResources(mutisigAccountAddress);
let accountResource = resources.find((r) => r.type === "0x1::coin::CoinStore<0x1::test_coin::TestCoin>");
let accountResource = resources.find((r) => r.type === "0x1::coin::CoinStore<0x1::aptos_coin::AptosCoin>");
let balance = parseInt((accountResource?.data as any).coin.value);

@@ -53,6 +53,6 @@ assert(balance === 5000);

const account4 = new AptosAccount();
// Creates a receiver account and fund the account with 0 TestCoin
// Creates a receiver account and fund the account with 0 AptosCoin
await faucetClient.fundAccount(account4.address(), 0);
resources = await client.getAccountResources(account4.address());
accountResource = resources.find((r) => r.type === "0x1::coin::CoinStore<0x1::test_coin::TestCoin>");
accountResource = resources.find((r) => r.type === "0x1::coin::CoinStore<0x1::aptos_coin::AptosCoin>");
balance = parseInt((accountResource?.data as any).coin.value);

@@ -62,3 +62,3 @@ assert(balance === 0);

const token = new TxnBuilderTypes.TypeTagStruct(TxnBuilderTypes.StructTag.fromString("0x1::test_coin::TestCoin"));
const token = new TxnBuilderTypes.TypeTagStruct(TxnBuilderTypes.StructTag.fromString("0x1::aptos_coin::AptosCoin"));

@@ -80,3 +80,3 @@ // TS SDK support 3 types of transaction payloads: `ScriptFunction`, `Script` and `Module`.

const [{ sequence_number: sequnceNumber }, chainId] = await Promise.all([
const [{ sequence_number: sequenceNumber }, chainId] = await Promise.all([
client.getAccount(mutisigAccountAddress),

@@ -91,3 +91,3 @@ client.getChainId(),

TxnBuilderTypes.AccountAddress.fromHex(mutisigAccountAddress),
BigInt(sequnceNumber),
BigInt(sequenceNumber),
scriptFunctionPayload,

@@ -130,3 +130,3 @@ // Max gas unit to spend

resources = await client.getAccountResources(account4.address());
accountResource = resources.find((r) => r.type === "0x1::coin::CoinStore<0x1::test_coin::TestCoin>");
accountResource = resources.find((r) => r.type === "0x1::coin::CoinStore<0x1::aptos_coin::AptosCoin>");
balance = parseInt((accountResource?.data as any).coin.value);

@@ -133,0 +133,0 @@ assert(balance === 123);

@@ -61,3 +61,3 @@ {

},
"version": "1.2.1"
"version": "1.2.2"
}

@@ -75,1 +75,14 @@ # Aptos TS/JS SDK

[api-doc]: https://aptos-labs.github.io/ts-sdk-doc/
## Release process
1. Regenerate generated types `npx swagger-typescript-api@latest -p ../../../api/doc/openapi.yaml -o ./src/api --modular --axios --single-http-client`
2. Update your commit message to follow the Angular Conventional Commits
3. Test lint and format `yarn test` `yarn lint` `yarn fmt`
4. Update the version in the `package.json` file and run `yarn changelog` to generate the changelog
5. Once it's all committed you can run npm release
Generate the change log. yarn changelog and bump the version in package.json. Only bump the minor version.
Commit the changes.
yarn build to build the package. Ideally, you should test the new package in browser to make sure it works in browser as well.
Follow https://aptos-org.slack.com/archives/C034HFWPJ05/p1651687417201819, to publish the package

@@ -136,3 +136,3 @@ /* eslint-disable */

* Account resource is a Move struct value belongs to an account.
* @example {"type":"0x1::Aptos0x1::reconfigurationBalance<0x1::XDX::XDX>","data":{"coin":{"value":"8000000000"}}}
* @example {"type":"0x1::coin::CoinStore<0x1::aptos_coin::AptosCoin>","data":{"coin":{"value":"8000000000"}}}
*/

@@ -147,5 +147,4 @@ export interface AccountResource {

* Examples:
* * `0x1::Aptos::Aptos<0x1::XDX::XDX>`
* * `0x1::Abc::Abc<vector<u8>, vector<u64>>`
* * `0x1::AptosAccount::AccountOperationsCapability`
* * `0x1::coin::CoinStore<0x1::aptos_coin::AptosCoin>`
* * `0x1::account::Account`
* Note:

@@ -182,8 +181,7 @@ * 1. Empty chars should be ignored when comparing 2 struct tag ids.

* `vector<vector<u64>>`
* `vector<0x1::AptosAccount::Balance<0x1::XDX::XDX>>`
* `vector<0x1::coin::CoinStore<0x1::aptos_coin::AptosCoin>>`
Struct type value examples:
* `0x1::Aptos::Aptos<0x1::XDX::XDX>`
* `0x1::Abc::Abc<vector<u8>, vector<u64>>`
* `0x1::AptosAccount::AccountOperationsCapability`
* `0x1::coin::CoinStore<0x1::aptos_coin::AptosCoin>
* `0x1::account::Account`

@@ -194,3 +192,3 @@ Note:

* @pattern ^(bool|u8|u64|u128|address|signer|vector<.+>|0x[0-9a-zA-Z:_<, >]+)$
* @example 0x1::XUS::XUS
* @example 0x1::aptos_coin::AptosCoin
*/

@@ -219,8 +217,7 @@ export type MoveTypeTagId = string;

* `vector<vector<u64>>`
* `vector<0x1::AptosAccount::Balance<0x1::XDX::XDX>>`
* `vector<0x1::coin::CoinStore<0x1::aptos_coin::AptosCoin>>`
Struct type value examples:
* `0x1::Aptos::Aptos<0x1::XDX::XDX>`
* `0x1::Abc::Abc<vector<u8>, vector<u64>>`
* `0x1::AptosAccount::AccountOperationsCapability`
* `0x1::coin::CoinStore<0x1::aptos_coin::AptosCoin>`
* `0x1::account::Account`

@@ -231,35 +228,4 @@ Reference type value examples:

* `&mut vector<u8>`
Generic type parameter value example, the following is `0x1::TransactionFee::TransactionFee` JSON representation:
{
"name": "TransactionFee",
"is_native": false,
"abilities": ["key"],
"generic_type_params": [
{"constraints": [], "is_phantom": true}
],
"fields": [
{ "name": "balance", "type": "0x1::Aptos::Aptos<T0>" },
{ "name": "preburn", "type": "0x1::Aptos::Preburn<T0>" }
]
}
It's Move source code:
module AptosFramework::TransactionFee {
struct TransactionFee<phantom CoinType> has key {
balance: Aptos<CoinType>,
preburn: Preburn<CoinType>,
}
}
The `T0` in the above JSON representation is the generic type place holder for
the `CoinType` in the Move source code.
Note:
1. Empty chars should be ignored when comparing 2 struct tag ids.
2. When used in an URL path, should be encoded by url-encoding (AKA percent-encoding).
* @pattern ^(bool|u8|u64|u128|address|signer|vector<.+>|0x[0-9a-zA-Z:_<, >]+|^&(mut )?.+$|T\d+)$
* @example 0x1::AptosAccount::Balance<0x1::XUS::XUS>
* @example 0x1::coin::CoinStore<0x1::aptos_coin::AptosCoin>
*/

@@ -276,5 +242,4 @@ export type MoveTypeId = string;

Examples:
* `0x1::Aptos::Aptos<0x1::XDX::XDX>`
* `0x1::Abc::Abc<vector<u8>, vector<u64>>`
* `0x1::AptosAccount::AccountOperationsCapability`
* `0x1::coin::CoinStore<0x1::aptos_coin::AptosCoin>`
* `0x1::account::Account`

@@ -288,3 +253,3 @@ Note:

* @pattern ^0x[0-9a-zA-Z:_<>]+$
* @example 0x1::AptosAccount::Balance<0x1::XUS::XUS>
* @example 0x1::coin::CoinStore<0x1::aptos_coin::AptosCoin>
*/

@@ -329,3 +294,3 @@ export type MoveStructTagId = string;

/**
* @example {"name":"Balance","is_native":false,"abilities":["key"],"generic_type_params":[{"constraints":[],"is_phantom":true}],"fields":[{"name":"coin","type":"0x1::Aptos::Aptos<T0>"}]}
* @example {"name":"Balance","is_native":false,"abilities":["key"],"generic_type_params":[{"constraints":[],"is_phantom":true}],"fields":[{"name":"coin","type":"0x1::aptos_coin::AptosCoin"}]}
*/

@@ -365,7 +330,6 @@ export interface MoveStruct {

* * `vector<vector<u64>>`
* * `vector<0x1::AptosAccount::Balance<0x1::XDX::XDX>>`
* * `vector<0x1::coin::CoinStore<0x1::aptos_coin::AptosCoin>>`
* Struct type value examples:
* * `0x1::Aptos::Aptos<0x1::XDX::XDX>`
* * `0x1::Abc::Abc<vector<u8>, vector<u64>>`
* * `0x1::AptosAccount::AccountOperationsCapability`
* * `0x1::coin::CoinStore<0x1::aptos_coin::AptosCoin>`
* * `0x1::account::Account`
* Reference type value examples:

@@ -375,26 +339,2 @@ * * `&signer`

* * `&mut vector<u8>`
* Generic type parameter value example, the following is `0x1::TransactionFee::TransactionFee` JSON representation:
* {
* "name": "TransactionFee",
* "is_native": false,
* "abilities": ["key"],
* "generic_type_params": [
* {"constraints": [], "is_phantom": true}
* ],
* "fields": [
* { "name": "balance", "type": "0x1::Aptos::Aptos<T0>" },
* { "name": "preburn", "type": "0x1::Aptos::Preburn<T0>" }
* ]
* }
* It's Move source code:
* module AptosFramework::TransactionFee {
* struct TransactionFee<phantom CoinType> has key {
* balance: Aptos<CoinType>,
* preburn: Preburn<CoinType>,
* }
* The `T0` in the above JSON representation is the generic type place holder for
* the `CoinType` in the Move source code.
* Note:
* 1. Empty chars should be ignored when comparing 2 struct tag ids.
* 2. When used in an URL path, should be encoded by url-encoding (AKA percent-encoding).
*/

@@ -410,3 +350,3 @@ type: MoveTypeId;

name: string;
visibility: 'public' | 'script' | 'friend';
visibility: "public" | "script" | "friend";
generic_type_params: { constraints: MoveAbility[] }[];

@@ -424,6 +364,6 @@ params: MoveTypeId[];

export enum MoveAbility {
Copy = 'copy',
Drop = 'drop',
Store = 'store',
Key = 'key',
Copy = "copy",
Drop = "drop",
Store = "store",
Key = "key",
}

@@ -442,3 +382,3 @@

See [doc](https://diem.github.io/move/modules-and-scripts.html#modules) for more details.
* @example 0x1::Aptos
* @example 0x1::aptos
*/

@@ -577,3 +517,3 @@ export type MoveModuleId = string;

/**
* @example {"type":"script_function_payload","function":"0x1::PaymentScripts::peer_to_peer_with_metadata","type_arguments":["0x1::XDX::XDX"],"arguments":["0x1668f6be25668c1a17cd8caf6b8d2f25","2021000000","0x","0x"]}
* @example {"type":"script_function_payload","function":"0x1::payment_scripts::peer_to_peer_with_metadata","type_arguments":["0x1::xdx::XDX"],"arguments":["0x1668f6be25668c1a17cd8caf6b8d2f25","2021000000","0x","0x"]}
*/

@@ -604,3 +544,3 @@ export interface ScriptFunctionPayload {

Both `module name` and `function name` are case-sensitive.
* @example 0x1::PaymentScripts::peer_to_peer_with_metadata
* @example 0x1::payment_scripts::peer_to_peer_with_metadata
*/

@@ -722,5 +662,4 @@ export type ScriptFunctionId = string;

* Examples:
* * `0x1::Aptos::Aptos<0x1::XDX::XDX>`
* * `0x1::Abc::Abc<vector<u8>, vector<u64>>`
* * `0x1::AptosAccount::AccountOperationsCapability`
* * `0x1::coin::CoinStore<0x1::aptos_coin::AptosCoin>`
* * `0x1::account::Account`
* Note:

@@ -910,3 +849,3 @@ * 1. Empty chars should be ignored when comparing 2 struct tag ids.

to decode the `data` JSON.
* @example {"key":"0x00000000000000000000000000000000000000000a550c18","sequence_number":"23","type":"0x1::AptosAccount::CreateAccountEvent","data":{"created":"0xa550c18","role_id":"0"}}
* @example {"key":"0x00000000000000000000000000000000000000000a550c18","sequence_number":"23","type":"0x1::account::CreateAccountEvent","data":{"created":"0xa550c18","role_id":"0"}}
*/

@@ -949,7 +888,6 @@ export interface Event {

* * `vector<vector<u64>>`
* * `vector<0x1::AptosAccount::Balance<0x1::XDX::XDX>>`
* * `vector<0x1::coin::CoinStore<0x1::aptos_coin::AptosCoin>>`
* Struct type value examples:
* * `0x1::Aptos::Aptos<0x1::XDX::XDX>`
* * `0x1::Abc::Abc<vector<u8>, vector<u64>>`
* * `0x1::AptosAccount::AccountOperationsCapability`
* * `0x1::coin::CoinStore<0x1::aptos_coin::AptosCoin>
* * `0x1::account::Account`
* Note:

@@ -1075,7 +1013,6 @@ * 1. Empty chars should be ignored when comparing 2 struct tag ids.

* * `vector<vector<u64>>`
* * `vector<0x1::AptosAccount::Balance<0x1::XDX::XDX>>`
* * `vector<0x1::coin::CoinStore<0x1::aptos_coin::AptosCoin>>`
* Struct type value examples:
* * `0x1::Aptos::Aptos<0x1::XDX::XDX>`
* * `0x1::Abc::Abc<vector<u8>, vector<u64>>`
* * `0x1::AptosAccount::AccountOperationsCapability`
* * `0x1::coin::CoinStore<0x1::aptos_coin::AptosCoin>`
* * `0x1::account::Account`
* Reference type value examples:

@@ -1085,26 +1022,2 @@ * * `&signer`

* * `&mut vector<u8>`
* Generic type parameter value example, the following is `0x1::TransactionFee::TransactionFee` JSON representation:
* {
* "name": "TransactionFee",
* "is_native": false,
* "abilities": ["key"],
* "generic_type_params": [
* {"constraints": [], "is_phantom": true}
* ],
* "fields": [
* { "name": "balance", "type": "0x1::Aptos::Aptos<T0>" },
* { "name": "preburn", "type": "0x1::Aptos::Preburn<T0>" }
* ]
* }
* It's Move source code:
* module AptosFramework::TransactionFee {
* struct TransactionFee<phantom CoinType> has key {
* balance: Aptos<CoinType>,
* preburn: Preburn<CoinType>,
* }
* The `T0` in the above JSON representation is the generic type place holder for
* the `CoinType` in the Move source code.
* Note:
* 1. Empty chars should be ignored when comparing 2 struct tag ids.
* 2. When used in an URL path, should be encoded by url-encoding (AKA percent-encoding).
*/

@@ -1132,7 +1045,6 @@ key_type: MoveTypeId;

* * `vector<vector<u64>>`
* * `vector<0x1::AptosAccount::Balance<0x1::XDX::XDX>>`
* * `vector<0x1::coin::CoinStore<0x1::aptos_coin::AptosCoin>>`
* Struct type value examples:
* * `0x1::Aptos::Aptos<0x1::XDX::XDX>`
* * `0x1::Abc::Abc<vector<u8>, vector<u64>>`
* * `0x1::AptosAccount::AccountOperationsCapability`
* * `0x1::coin::CoinStore<0x1::aptos_coin::AptosCoin>`
* * `0x1::account::Account`
* Reference type value examples:

@@ -1142,26 +1054,2 @@ * * `&signer`

* * `&mut vector<u8>`
* Generic type parameter value example, the following is `0x1::TransactionFee::TransactionFee` JSON representation:
* {
* "name": "TransactionFee",
* "is_native": false,
* "abilities": ["key"],
* "generic_type_params": [
* {"constraints": [], "is_phantom": true}
* ],
* "fields": [
* { "name": "balance", "type": "0x1::Aptos::Aptos<T0>" },
* { "name": "preburn", "type": "0x1::Aptos::Preburn<T0>" }
* ]
* }
* It's Move source code:
* module AptosFramework::TransactionFee {
* struct TransactionFee<phantom CoinType> has key {
* balance: Aptos<CoinType>,
* preburn: Preburn<CoinType>,
* }
* The `T0` in the above JSON representation is the generic type place holder for
* the `CoinType` in the Move source code.
* Note:
* 1. Empty chars should be ignored when comparing 2 struct tag ids.
* 2. When used in an URL path, should be encoded by url-encoding (AKA percent-encoding).
*/

@@ -1168,0 +1056,0 @@ value_type: MoveTypeId;

@@ -81,3 +81,3 @@ /* eslint-disable */

/**
* @description There are two types of transaction identifiers: 1. Transaction hash: included in any transaction JSON respond from server. 2. Transaction version: included in on-chain transaction JSON respond from server. When given transaction hash, server first looks up on-chain transaction by hash; if no on-chain transaction found, then look up transaction by hash in the mempool (pending) transactions. When given a transaction version, server looks up the transaction on-chain by version. To create a transaction hash: 1. Create hash message bytes: "Aptos::Transaction" bytes + BCS bytes of [Transaction](https://aptos-labs.github.io/aptos-core/aptos_types/transaction/enum.Transaction.html). 2. Apply hash algorithm `SHA3-256` to the hash message bytes. 3. Hex-encode the hash bytes with `0x` prefix.
* @description There are two types of transaction identifiers: 1. Transaction hash: included in any transaction JSON respond from server. 2. Transaction version: included in on-chain transaction JSON respond from server. When given transaction hash, server first looks up on-chain transaction by hash; if no on-chain transaction found, then look up transaction by hash in the mempool (pending) transactions. When given a transaction version, server looks up the transaction on-chain by version. To create a transaction hash: 1. Create hash message bytes: "APTOS::RawTransaction" bytes + BCS bytes of [Transaction](https://aptos-labs.github.io/aptos-core/aptos_types/transaction/enum.Transaction.html). 2. Apply hash algorithm `SHA3-256` to the hash message bytes. 3. Hex-encode the hash bytes with `0x` prefix.
*

@@ -84,0 +84,0 @@ * @tags transactions

@@ -10,5 +10,12 @@ /* eslint-disable operator-linebreak */

import { AptosAccount } from "./aptos_account";
import { TxnBuilderTypes, TransactionBuilderMultiEd25519, BCS, TransactionBuilder } from "./transaction_builder";
import {
TxnBuilderTypes,
TransactionBuilderMultiEd25519,
BCS,
TransactionBuilder,
TransactionBuilderEd25519,
} from "./transaction_builder";
import { TransactionPayload, WriteResource } from "./api/data-contracts";
import { TokenClient } from "./token_client";
import { HexString } from "./hex_string";

@@ -51,9 +58,9 @@ test("gets genesis account", async () => {

const modules = await client.getAccountModules("0x1");
const module = modules.find((r) => r.abi.name === "test_coin");
const module = modules.find((r) => r.abi.name === "aptos_coin");
expect(module.abi.address).toBe("0x1");
});
test("gets the TestCoin module", async () => {
test("gets the AptosCoin module", async () => {
const client = new AptosClient(NODE_URL);
const module = await client.getAccountModule("0x1", "test_coin");
const module = await client.getAccountModule("0x1", "aptos_coin");
expect(module.abi.address).toBe("0x1");

@@ -81,5 +88,7 @@ });

expect(() => raiseForStatus(200, fakeResponse, testData)).toThrow(
// eslint-disable-next-line quotes
'Status Text - "some string" @ host/path : {"hello":"wow"}',
);
// eslint-disable-next-line quotes
expect(() => raiseForStatus(200, fakeResponse)).toThrow('Status Text - "some string" @ host/path');

@@ -89,4 +98,6 @@

delete fakeResponse.request;
// eslint-disable-next-line quotes
expect(() => raiseForStatus(200, fakeResponse, testData)).toThrow('Status Text - "some string" : {"hello":"wow"}');
// eslint-disable-next-line quotes
expect(() => raiseForStatus(200, fakeResponse)).toThrow('Status Text - "some string"');

@@ -104,3 +115,3 @@ });

let resources = await client.getAccountResources(account1.address());
let accountResource = resources.find((r) => r.type === "0x1::coin::CoinStore<0x1::test_coin::TestCoin>");
let accountResource = resources.find((r) => r.type === "0x1::coin::CoinStore<0x1::aptos_coin::AptosCoin>");
expect((accountResource.data as any).coin.value).toBe("5000");

@@ -111,6 +122,6 @@

resources = await client.getAccountResources(account2.address());
accountResource = resources.find((r) => r.type === "0x1::coin::CoinStore<0x1::test_coin::TestCoin>");
accountResource = resources.find((r) => r.type === "0x1::coin::CoinStore<0x1::aptos_coin::AptosCoin>");
expect((accountResource.data as any).coin.value).toBe("0");
const token = new TxnBuilderTypes.TypeTagStruct(TxnBuilderTypes.StructTag.fromString("0x1::test_coin::TestCoin"));
const token = new TxnBuilderTypes.TypeTagStruct(TxnBuilderTypes.StructTag.fromString("0x1::aptos_coin::AptosCoin"));

@@ -126,3 +137,3 @@ const scriptFunctionPayload = new TxnBuilderTypes.TransactionPayloadScriptFunction(

const [{ sequence_number: sequnceNumber }, chainId] = await Promise.all([
const [{ sequence_number: sequenceNumber }, chainId] = await Promise.all([
client.getAccount(account1.address()),

@@ -134,3 +145,3 @@ client.getChainId(),

TxnBuilderTypes.AccountAddress.fromHex(account1.address()),
BigInt(sequnceNumber),
BigInt(sequenceNumber),
scriptFunctionPayload,

@@ -149,3 +160,3 @@ 1000n,

resources = await client.getAccountResources(account2.address());
accountResource = resources.find((r) => r.type === "0x1::coin::CoinStore<0x1::test_coin::TestCoin>");
accountResource = resources.find((r) => r.type === "0x1::coin::CoinStore<0x1::aptos_coin::AptosCoin>");
expect((accountResource.data as any).coin.value).toBe("717");

@@ -180,3 +191,3 @@ },

let resources = await client.getAccountResources(mutisigAccountAddress);
let accountResource = resources.find((r) => r.type === "0x1::coin::CoinStore<0x1::test_coin::TestCoin>");
let accountResource = resources.find((r) => r.type === "0x1::coin::CoinStore<0x1::aptos_coin::AptosCoin>");
expect((accountResource.data as any).coin.value).toBe("5000");

@@ -187,6 +198,6 @@

resources = await client.getAccountResources(account4.address());
accountResource = resources.find((r) => r.type === "0x1::coin::CoinStore<0x1::test_coin::TestCoin>");
accountResource = resources.find((r) => r.type === "0x1::coin::CoinStore<0x1::aptos_coin::AptosCoin>");
expect((accountResource.data as any).coin.value).toBe("0");
const token = new TxnBuilderTypes.TypeTagStruct(TxnBuilderTypes.StructTag.fromString("0x1::test_coin::TestCoin"));
const token = new TxnBuilderTypes.TypeTagStruct(TxnBuilderTypes.StructTag.fromString("0x1::aptos_coin::AptosCoin"));

@@ -202,3 +213,3 @@ const scriptFunctionPayload = new TxnBuilderTypes.TransactionPayloadScriptFunction(

const [{ sequence_number: sequnceNumber }, chainId] = await Promise.all([
const [{ sequence_number: sequenceNumber }, chainId] = await Promise.all([
client.getAccount(mutisigAccountAddress),

@@ -210,3 +221,3 @@ client.getChainId(),

TxnBuilderTypes.AccountAddress.fromHex(mutisigAccountAddress),
BigInt(sequnceNumber),
BigInt(sequenceNumber),
scriptFunctionPayload,

@@ -241,3 +252,3 @@ 1000n,

resources = await client.getAccountResources(account4.address());
accountResource = resources.find((r) => r.type === "0x1::coin::CoinStore<0x1::test_coin::TestCoin>");
accountResource = resources.find((r) => r.type === "0x1::coin::CoinStore<0x1::aptos_coin::AptosCoin>");
expect((accountResource.data as any).coin.value).toBe("123");

@@ -262,11 +273,11 @@ },

expect(tx2.type).toBe("user_transaction");
const checkTestCoin = async () => {
const checkAptosCoin = async () => {
const resources1 = await client.getAccountResources(account1.address());
const resources2 = await client.getAccountResources(account2.address());
const account1Resource = resources1.find((r) => r.type === "0x1::coin::CoinStore<0x1::test_coin::TestCoin>");
const account2Resource = resources2.find((r) => r.type === "0x1::coin::CoinStore<0x1::test_coin::TestCoin>");
const account1Resource = resources1.find((r) => r.type === "0x1::coin::CoinStore<0x1::aptos_coin::AptosCoin>");
const account2Resource = resources2.find((r) => r.type === "0x1::coin::CoinStore<0x1::aptos_coin::AptosCoin>");
expect((account1Resource.data as { coin: { value: string } }).coin.value).toBe("5000");
expect((account2Resource.data as { coin: { value: string } }).coin.value).toBe("1000");
};
await checkTestCoin();
await checkAptosCoin();

@@ -276,3 +287,3 @@ const payload: TransactionPayload = {

function: "0x1::coin::transfer",
type_arguments: ["0x1::test_coin::TestCoin"],
type_arguments: ["0x1::aptos_coin::AptosCoin"],
arguments: [account2.address().hex(), "1000"],

@@ -284,3 +295,3 @@ };

expect(transactionRes.success);
const account2TestCoin = transactionRes.changes.filter((change) => {
const account2AptosCoin = transactionRes.changes.filter((change) => {
if (change.type !== "write_resource") {

@@ -293,8 +304,8 @@ return false;

write.address === account2.address().toShortString() &&
write.data.type === "0x1::coin::CoinStore<0x1::test_coin::TestCoin>" &&
write.data.type === "0x1::coin::CoinStore<0x1::aptos_coin::AptosCoin>" &&
(write.data.data as { coin: { value: string } }).coin.value === "2000"
);
});
expect(account2TestCoin).toHaveLength(1);
await checkTestCoin();
expect(account2AptosCoin).toHaveLength(1);
await checkAptosCoin();
},

@@ -318,13 +329,13 @@ 30 * 1000,

expect(tx2.type).toBe("user_transaction");
const checkTestCoin = async () => {
const checkAptosCoin = async () => {
const resources1 = await client.getAccountResources(account1.address());
const resources2 = await client.getAccountResources(account2.address());
const account1Resource = resources1.find((r) => r.type === "0x1::coin::CoinStore<0x1::test_coin::TestCoin>");
const account2Resource = resources2.find((r) => r.type === "0x1::coin::CoinStore<0x1::test_coin::TestCoin>");
const account1Resource = resources1.find((r) => r.type === "0x1::coin::CoinStore<0x1::aptos_coin::AptosCoin>");
const account2Resource = resources2.find((r) => r.type === "0x1::coin::CoinStore<0x1::aptos_coin::AptosCoin>");
expect((account1Resource.data as { coin: { value: string } }).coin.value).toBe("5000");
expect((account2Resource.data as { coin: { value: string } }).coin.value).toBe("1000");
};
await checkTestCoin();
await checkAptosCoin();
const token = new TxnBuilderTypes.TypeTagStruct(TxnBuilderTypes.StructTag.fromString("0x1::test_coin::TestCoin"));
const token = new TxnBuilderTypes.TypeTagStruct(TxnBuilderTypes.StructTag.fromString("0x1::aptos_coin::AptosCoin"));
const scriptFunctionPayload = new TxnBuilderTypes.TransactionPayloadScriptFunction(

@@ -339,3 +350,3 @@ TxnBuilderTypes.ScriptFunction.natural(

const [{ sequence_number: sequnceNumber }, chainId] = await Promise.all([
const [{ sequence_number: sequenceNumber }, chainId] = await Promise.all([
client.getAccount(account1.address()),

@@ -347,3 +358,3 @@ client.getChainId(),

TxnBuilderTypes.AccountAddress.fromHex(account1.address()),
BigInt(sequnceNumber),
BigInt(sequenceNumber),
scriptFunctionPayload,

@@ -360,3 +371,3 @@ 1000n,

expect(transactionRes.success);
const account2TestCoin = transactionRes.changes.filter((change) => {
const account2AptosCoin = transactionRes.changes.filter((change) => {
if (change.type !== "write_resource") {

@@ -369,8 +380,8 @@ return false;

write.address === account2.address().toShortString() &&
write.data.type === "0x1::coin::CoinStore<0x1::test_coin::TestCoin>" &&
write.data.type === "0x1::coin::CoinStore<0x1::aptos_coin::AptosCoin>" &&
(write.data.data as { coin: { value: string } }).coin.value === "2000"
);
});
expect(account2TestCoin).toHaveLength(1);
await checkTestCoin();
expect(account2AptosCoin).toHaveLength(1);
await checkAptosCoin();
},

@@ -395,3 +406,3 @@ 30 * 1000,

let resources = await client.getAccountResources(alice.address());
let accountResource = resources.find((r) => r.type === "0x1::coin::CoinStore<0x1::test_coin::TestCoin>");
let accountResource = resources.find((r) => r.type === "0x1::coin::CoinStore<0x1::aptos_coin::AptosCoin>");
expect((accountResource.data as any).coin.value).toBe("5000");

@@ -401,3 +412,3 @@

resources = await client.getAccountResources(bob.address());
accountResource = resources.find((r) => r.type === "0x1::coin::CoinStore<0x1::test_coin::TestCoin>");
accountResource = resources.find((r) => r.type === "0x1::coin::CoinStore<0x1::aptos_coin::AptosCoin>");
expect((accountResource.data as any).coin.value).toBe("6000");

@@ -440,3 +451,3 @@

const [{ sequence_number: sequnceNumber }, chainId] = await Promise.all([
const [{ sequence_number: sequenceNumber }, chainId] = await Promise.all([
client.getAccount(alice.address()),

@@ -448,3 +459,3 @@ client.getChainId(),

aliceAccountAddress,
BigInt(sequnceNumber),
BigInt(sequenceNumber),
scriptFunctionPayload,

@@ -515,1 +526,98 @@ 1000n,

);
test(
"submits ABI transaction",
async () => {
const coinTransferABI =
// eslint-disable-next-line max-len
"01087472616E73666572000000000000000000000000000000000000000000000000000000000000000104636F696E3C205472616E73666572732060616D6F756E7460206F6620636F696E732060436F696E54797065602066726F6D206066726F6D6020746F2060746F602E0109636F696E5F747970650202746F0406616D6F756E7402";
const client = new AptosClient(NODE_URL, {}, { abis: [new HexString(coinTransferABI).toUint8Array()] });
const faucetClient = new FaucetClient(NODE_URL, FAUCET_URL, null);
const account1 = new AptosAccount();
await faucetClient.fundAccount(account1.address(), 1000);
let resources = await client.getAccountResources(account1.address());
let accountResource = resources.find((r) => r.type === "0x1::coin::CoinStore<0x1::aptos_coin::AptosCoin>");
expect((accountResource.data as any).coin.value).toBe("1000");
const account2 = new AptosAccount();
await faucetClient.fundAccount(account2.address(), 0);
resources = await client.getAccountResources(account2.address());
accountResource = resources.find((r) => r.type === "0x1::coin::CoinStore<0x1::aptos_coin::AptosCoin>");
expect((accountResource.data as any).coin.value).toBe("0");
const transactionRes = await client.sendABITransaction(
account1,
"0x1::coin::transfer",
["0x1::aptos_coin::AptosCoin"],
[account2.address(), 144],
);
await client.waitForTransaction(transactionRes.hash);
resources = await client.getAccountResources(account2.address());
accountResource = resources.find((r) => r.type === "0x1::coin::CoinStore<0x1::aptos_coin::AptosCoin>");
expect((accountResource.data as any).coin.value).toBe("144");
},
30 * 1000,
);
test(
"calculates transaction hash",
async () => {
const client = new AptosClient(NODE_URL);
const faucetClient = new FaucetClient(NODE_URL, FAUCET_URL, null);
const account1 = new AptosAccount();
await faucetClient.fundAccount(account1.address(), 5000);
let resources = await client.getAccountResources(account1.address());
let accountResource = resources.find((r) => r.type === "0x1::coin::CoinStore<0x1::aptos_coin::AptosCoin>");
expect((accountResource.data as any).coin.value).toBe("5000");
const account2 = new AptosAccount();
await faucetClient.fundAccount(account2.address(), 0);
resources = await client.getAccountResources(account2.address());
accountResource = resources.find((r) => r.type === "0x1::coin::CoinStore<0x1::aptos_coin::AptosCoin>");
expect((accountResource.data as any).coin.value).toBe("0");
const token = new TxnBuilderTypes.TypeTagStruct(TxnBuilderTypes.StructTag.fromString("0x1::aptos_coin::AptosCoin"));
const scriptFunctionPayload = new TxnBuilderTypes.TransactionPayloadScriptFunction(
TxnBuilderTypes.ScriptFunction.natural(
"0x1::coin",
"transfer",
[token],
[BCS.bcsToBytes(TxnBuilderTypes.AccountAddress.fromHex(account2.address())), BCS.bcsSerializeUint64(717)],
),
);
const [{ sequence_number: sequenceNumber }, chainId] = await Promise.all([
client.getAccount(account1.address()),
client.getChainId(),
]);
const rawTxn = new TxnBuilderTypes.RawTransaction(
TxnBuilderTypes.AccountAddress.fromHex(account1.address()),
BigInt(sequenceNumber),
scriptFunctionPayload,
1000n,
1n,
BigInt(Math.floor(Date.now() / 1000) + 10),
new TxnBuilderTypes.ChainId(chainId),
);
const txnBuilder = new TransactionBuilderEd25519((signingMessage: TxnBuilderTypes.SigningMessage) => {
// @ts-ignore
const sigHexStr = account1.signBuffer(signingMessage);
return new TxnBuilderTypes.Ed25519Signature(sigHexStr.toUint8Array());
}, account1.pubKey().toUint8Array());
const userTxn = new TxnBuilderTypes.UserTransaction(txnBuilder.rawToSigned(rawTxn));
const bcsTxn = txnBuilder.sign(rawTxn);
const transactionRes = await client.submitSignedBCSTransaction(bcsTxn);
expect(HexString.fromUint8Array(userTxn.hash()).hex()).toBe(transactionRes.hash);
},
30 * 1000,
);
import { AxiosRequestConfig, AxiosResponse } from "axios";
import { MemoizeExpiring } from "typescript-memoize";
import { Memoize, MemoizeExpiring } from "typescript-memoize";
import { Accounts } from "./api/Accounts";

@@ -13,3 +13,4 @@ import { Events } from "./api/Events";

import { AptosError } from "./api/data-contracts";
import { TxnBuilderTypes, TransactionBuilderEd25519 } from "./transaction_builder";
import { TxnBuilderTypes, TransactionBuilderEd25519, TransactionBuilderABI } from "./transaction_builder";
import { Bytes } from "./transaction_builder/bcs";

@@ -33,2 +34,6 @@ export class RequestError extends Error {

type ExtraConfig = {
abis: Bytes[];
};
export function raiseForStatus<T>(

@@ -65,2 +70,4 @@ expectedStatus: number,

abis: Bytes[];
/**

@@ -72,3 +79,3 @@ * Establishes a connection to Aptos node

*/
constructor(nodeUrl: string, config?: AptosClientConfig) {
constructor(nodeUrl: string, config?: AptosClientConfig, extraConfig?: ExtraConfig) {
this.nodeUrl = nodeUrl;

@@ -89,2 +96,4 @@

this.transactions = new Transactions(this.client);
this.abis = extraConfig?.abis;
}

@@ -171,3 +180,3 @@

* {
* type: "0x1::AptosAccount::Coin",
* type: "0x1::account::Coin",
* data: { value: 6 }

@@ -195,3 +204,3 @@ * }

* {
* type: "0x1::AptosAccount::Coin",
* type: "0x1::account::Coin",
* data: { value: 6 }

@@ -331,3 +340,3 @@ * }

* @param eventHandleStruct String representation of an on-chain Move struct type.
* (e.g. `0x1::coin::CoinStore<0x1::test_coin::TestCoin>`)
* (e.g. `0x1::coin::CoinStore<0x1::aptos_coin::AptosCoin>`)
* @param fieldName The field name of the EventHandle in the struct

@@ -463,3 +472,3 @@ * @param query Optional query object

*
* 1. Create hash message bytes: "Aptos::Transaction" bytes + BCS bytes of Transaction.
* 1. Create hash message bytes: "APTOS::RawTransaction" bytes + BCS bytes of Transaction.
* 2. Apply hash algorithm SHA3-256 to the hash message bytes.

@@ -534,3 +543,3 @@ * 3. Hex-encode the hash bytes with 0x prefix.

*/
@MemoizeExpiring(5 * 60 * 1000) // cache result for 5 minutes
@Memoize()
async getChainId(params: RequestParams = {}): Promise<number> {

@@ -556,2 +565,47 @@ const result = await this.getLedgerInfo(params);

}
// Only cache for a short period to avoid excessive amount of memory usage
@MemoizeExpiring(2 * 60 * 1000) // Cache for 2min
async getTxnBuilderWithABI(accountFrom: AptosAccount): Promise<TransactionBuilderEd25519> {
if (this.abis.length === 0) {
throw new Error("ABIs are not provided.");
}
const [{ sequence_number: sequenceNumber }, chainId] = await Promise.all([
this.getAccount(accountFrom.address()),
this.getChainId(),
]);
const rawTxnBuilder = new TransactionBuilderABI(this.abis, {
sender: accountFrom.address(),
sequenceNumber,
chainId,
});
return new TransactionBuilderEd25519(
(signingMessage: TxnBuilderTypes.SigningMessage) => {
const sigHexStr = accountFrom.signBuffer(signingMessage);
return new TxnBuilderTypes.Ed25519Signature(sigHexStr.toUint8Array());
},
accountFrom.pubKey().toUint8Array(),
rawTxnBuilder,
);
}
async sendABITransaction(
accountFrom: AptosAccount,
func: string,
ty_tags: string[],
args: any[],
): Promise<Types.PendingTransaction> {
const [txnBuilder, { sequence_number: sequenceNumber }] = await Promise.all([
this.getTxnBuilderWithABI(accountFrom),
this.getAccount(accountFrom.address()),
]);
txnBuilder.rawTxnBuilder?.setSequenceNumber(sequenceNumber);
const rawTxn = txnBuilder.build(func, ty_tags, args);
return this.submitSignedBCSTransaction(txnBuilder.sign(rawTxn));
}
}

@@ -21,3 +21,3 @@ import { AptosClient } from "./aptos_client";

let resources = await client.getAccountResources(account1.address());
let accountResource = resources.find((r) => r.type === "0x1::coin::CoinStore<0x1::test_coin::TestCoin>");
let accountResource = resources.find((r) => r.type === "0x1::coin::CoinStore<0x1::aptos_coin::AptosCoin>");
expect((accountResource.data as { coin: { value: string } }).coin.value).toBe("5000");

@@ -28,3 +28,3 @@

resources = await client.getAccountResources(account2.address());
accountResource = resources.find((r) => r.type === "0x1::coin::CoinStore<0x1::test_coin::TestCoin>");
accountResource = resources.find((r) => r.type === "0x1::coin::CoinStore<0x1::aptos_coin::AptosCoin>");
expect((accountResource.data as { coin: { value: string } }).coin.value).toBe("0");

@@ -35,3 +35,3 @@

function: "0x1::coin::transfer",
type_arguments: ["0x1::test_coin::TestCoin"],
type_arguments: ["0x1::aptos_coin::AptosCoin"],
arguments: [account2.address().hex(), "717"],

@@ -45,3 +45,3 @@ };

resources = await client.getAccountResources(account2.address());
accountResource = resources.find((r) => r.type === "0x1::coin::CoinStore<0x1::test_coin::TestCoin>");
accountResource = resources.find((r) => r.type === "0x1::coin::CoinStore<0x1::aptos_coin::AptosCoin>");
expect((accountResource.data as { coin: { value: string } }).coin.value).toBe("717");

@@ -55,3 +55,3 @@

tx.sender,
"0x1::coin::CoinStore<0x1::test_coin::TestCoin>",
"0x1::coin::CoinStore<0x1::aptos_coin::AptosCoin>",
"withdraw_events",

@@ -63,3 +63,3 @@ );

tx.sender,
"0x1::coin::CoinStore<0x1::test_coin::TestCoin>",
"0x1::coin::CoinStore<0x1::aptos_coin::AptosCoin>",
"withdraw_events",

@@ -66,0 +66,0 @@ { start: 0, limit: 1 },

@@ -5,2 +5,3 @@ /* eslint-disable @typescript-eslint/naming-convention */

/* eslint-disable max-classes-per-file */
import * as SHA3 from "js-sha3";
import { HexString } from "../../hex_string";

@@ -17,2 +18,3 @@ import {

serializeVector,
bcsToBytes,
} from "../bcs";

@@ -148,3 +150,3 @@ import { AccountAddress } from "./account_address";

*
* @param module Fully qualified module name in format "AccountAddress::ModuleName" e.g. "0x1::coin"
* @param module Fully qualified module name in format "AccountAddress::module_name" e.g. "0x1::coin"
* @param func Function name

@@ -251,4 +253,4 @@ * @param ty_args Type arguments that move function requires.

* Converts a string literal to a ModuleId
* @param moduleId String literal in format "AcountAddress::ModuleName",
* e.g. "0x01::Coin"
* @param moduleId String literal in format "AccountAddress::module_name",
* e.g. "0x1::coin"
* @returns

@@ -569,1 +571,45 @@ */

}
export abstract class Transaction {
abstract serialize(serializer: Serializer): void;
abstract hash(): Bytes;
getHashSalt(): Bytes {
const hash = SHA3.sha3_256.create();
hash.update(Buffer.from("APTOS::Transaction"));
return new Uint8Array(hash.arrayBuffer());
}
static deserialize(deserializer: Deserializer): Transaction {
const index = deserializer.deserializeUleb128AsU32();
switch (index) {
case 0:
return UserTransaction.load(deserializer);
default:
throw new Error(`Unknown variant index for Transaction: ${index}`);
}
}
}
export class UserTransaction extends Transaction {
constructor(public readonly value: SignedTransaction) {
super();
}
hash(): Bytes {
const hash = SHA3.sha3_256.create();
hash.update(this.getHashSalt());
hash.update(bcsToBytes(this));
return new Uint8Array(hash.arrayBuffer());
}
serialize(serializer: Serializer): void {
serializer.serializeU32AsUleb128(0);
this.value.serialize(serializer);
}
static load(deserializer: Deserializer): UserTransaction {
return new UserTransaction(SignedTransaction.deserialize(deserializer));
}
}

@@ -137,4 +137,4 @@ /* eslint-disable @typescript-eslint/no-unused-vars */

* Converts a string literal to a StructTag
* @param structTag String literal in format "AcountAddress::ModuleName::ResourceName",
* e.g. "0x01::test_coin::TestCoin"
* @param structTag String literal in format "AcountAddress::module_name::ResourceName",
* e.g. "0x1::aptos_coin::AptosCoin"
* @returns

@@ -141,0 +141,0 @@ */

@@ -14,4 +14,16 @@ import * as SHA3 from "js-sha3";

MultiAgentRawTransaction,
AccountAddress,
ScriptFunction,
Identifier,
ChainId,
Script,
TransactionPayload,
TransactionArgument,
TransactionPayloadScriptFunction,
TransactionPayloadScript,
} from "./aptos_types";
import { bcsToBytes, Bytes } from "./bcs";
import { bcsToBytes, Bytes, Deserializer, Serializer, Uint64, Uint8 } from "./bcs";
import { ScriptABI, ScriptFunctionABI, TransactionScriptABI } from "./aptos_types/abi";
import { HexString } from "../hex_string";
import { argToTransactionArgument, TypeTagParser, serializeArg } from "./builder_utils";

@@ -32,6 +44,20 @@ const RAW_TRANSACTION_SALT = "APTOS::RawTransaction";

constructor(signingFunction: F) {
constructor(signingFunction: F, public readonly rawTxnBuilder?: TransactionBuilderABI) {
this.signingFunction = signingFunction;
}
/**
* Builds a RawTransaction. Relays the call to TransactionBuilderABI.build
* @param func
* @param ty_tags
* @param args
*/
build(func: string, ty_tags: string[], args: any[]): RawTransaction {
if (!this.rawTxnBuilder) {
throw new Error("this.rawTxnBuilder doesn't exist.");
}
return this.rawTxnBuilder.build(func, ty_tags, args);
}
/** Generates a Signing Message out of a raw transaction. */

@@ -60,8 +86,8 @@ static getSigningMessage(rawTxn: AnyRawTransaction): SigningMessage {

constructor(signingFunction: SigningFn, publicKey: Uint8Array) {
super(signingFunction);
constructor(signingFunction: SigningFn, publicKey: Uint8Array, rawTxnBuilder?: TransactionBuilderABI) {
super(signingFunction, rawTxnBuilder);
this.publicKey = publicKey;
}
private signInternal(rawTxn: RawTransaction): SignedTransaction {
rawToSigned(rawTxn: RawTransaction): SignedTransaction {
const signingMessage = TransactionBuilder.getSigningMessage(rawTxn);

@@ -80,3 +106,3 @@ const signature = this.signingFunction(signingMessage);

sign(rawTxn: RawTransaction): Bytes {
return bcsToBytes(this.signInternal(rawTxn));
return bcsToBytes(this.rawToSigned(rawTxn));
}

@@ -96,3 +122,3 @@ }

private signInternal(rawTxn: RawTransaction): SignedTransaction {
rawToSigned(rawTxn: RawTransaction): SignedTransaction {
const signingMessage = TransactionBuilder.getSigningMessage(rawTxn);

@@ -108,4 +134,151 @@ const signature = this.signingFunction(signingMessage);

sign(rawTxn: RawTransaction): Bytes {
return bcsToBytes(this.signInternal(rawTxn));
return bcsToBytes(this.rawToSigned(rawTxn));
}
}
/**
* Config for creating raw transactions.
*/
interface ABIBuilderConfig {
sender: HexString | AccountAddress;
sequenceNumber: Uint64 | string;
gasUnitPrice?: Uint64 | string;
maxGasAmount?: Uint64 | string;
expSecFromNow?: number | string;
chainId: Uint8 | string;
}
/**
* Builds raw transactions based on ABI
*/
export class TransactionBuilderABI {
private readonly abiMap: Map<string, ScriptABI>;
private readonly builderConfig: ABIBuilderConfig;
/**
* Constructs a TransactionBuilderABI instance
* @param abis List of binary ABIs.
* @param builderConfig Configs for creating a raw transaction.
*/
constructor(abis: Bytes[], builderConfig: ABIBuilderConfig) {
this.abiMap = new Map<string, ScriptABI>();
abis.forEach((abi) => {
const deserializer = new Deserializer(abi);
const scriptABI = ScriptABI.deserialize(deserializer);
let k: string;
if (scriptABI instanceof ScriptFunctionABI) {
const funcABI = scriptABI as ScriptFunctionABI;
const { address: addr, name: moduleName } = funcABI.module_name;
k = `${HexString.fromUint8Array(addr.address).toShortString()}::${moduleName.value}::${funcABI.name}`;
} else {
const funcABI = scriptABI as TransactionScriptABI;
k = funcABI.name;
}
if (this.abiMap.has(k)) {
throw new Error("Found conflicting ABI interfaces");
}
this.abiMap.set(k, scriptABI);
});
this.builderConfig = {
gasUnitPrice: 1n,
maxGasAmount: 1000n,
expSecFromNow: 10,
...builderConfig,
};
}
private static toBCSArgs(abiArgs: any[], args: any[]): Bytes[] {
if (abiArgs.length !== args.length) {
throw new Error("Wrong number of args provided.");
}
return args.map((arg, i) => {
const serializer = new Serializer();
serializeArg(arg, abiArgs[i].type_tag, serializer);
return serializer.getBytes();
});
}
private static toTransactionArguments(abiArgs: any[], args: any[]): TransactionArgument[] {
if (abiArgs.length !== args.length) {
throw new Error("Wrong number of args provided.");
}
return args.map((arg, i) => argToTransactionArgument(arg, abiArgs[i].type_tag));
}
setSequenceNumber(seqNumber: Uint64 | string) {
this.builderConfig.sequenceNumber = BigInt(seqNumber);
}
/**
* Builds a RawTransaction
* @param func Fully qualified func names, e.g. 0x1::Coin::transfer
* @param ty_tags TypeTag strings.
* @example Below are valid value examples
* ```
* // Structs are in format `AccountAddress::ModuleName::StructName`
* 0x1::aptos_coin::AptosCoin
* // Vectors are in format `vector<other_tag_string>`
* vector<0x1::aptos_coin::AptosCoin>
* bool
* u8
* u64
* u128
* address
* ```
* @param args Function arguments
* @returns RawTransaction
*/
build(func: string, ty_tags: string[], args: any[]): RawTransaction {
const { sender, sequenceNumber, gasUnitPrice, maxGasAmount, expSecFromNow, chainId } = this.builderConfig;
const senderAccount = sender instanceof HexString ? AccountAddress.fromHex(sender) : sender;
const typeTags = ty_tags.map((ty_arg) => new TypeTagParser(ty_arg).parseTypeTag());
const expTimetampSec = BigInt(Math.floor(Date.now() / 1000) + Number(expSecFromNow));
let payload: TransactionPayload;
if (!this.abiMap.has(func)) {
throw new Error(`Cannot find function: ${func}`);
}
const scriptABI = this.abiMap.get(func);
if (scriptABI instanceof ScriptFunctionABI) {
const funcABI = scriptABI as ScriptFunctionABI;
const bcsArgs = TransactionBuilderABI.toBCSArgs(funcABI.args, args);
payload = new TransactionPayloadScriptFunction(
new ScriptFunction(funcABI.module_name, new Identifier(funcABI.name), typeTags, bcsArgs),
);
}
if (scriptABI instanceof TransactionScriptABI) {
const funcABI = scriptABI as TransactionScriptABI;
const scriptArgs = TransactionBuilderABI.toTransactionArguments(funcABI.args, args);
payload = new TransactionPayloadScript(new Script(funcABI.code, typeTags, scriptArgs));
}
if (payload) {
return new RawTransaction(
senderAccount,
BigInt(sequenceNumber),
payload,
BigInt(maxGasAmount),
BigInt(gasUnitPrice),
expTimetampSec,
new ChainId(Number(chainId)),
);
}
throw new Error("Invalid ABI.");
}
}

@@ -64,3 +64,3 @@ /* eslint-disable max-len */

ScriptFunction.natural(
`${ADDRESS_1}::test_coin`,
`${ADDRESS_1}::aptos_coin`,
"transfer",

@@ -85,3 +85,3 @@ [],

expect(hexSignedTxn(signedTxn)).toBe(
"000000000000000000000000000000000000000000000000000000000a550c18000000000000000003000000000000000000000000000000000000000000000000000000000000122209746573745f636f696e087472616e7366657200022000000000000000000000000000000000000000000000000000000000000000dd080100000000000000d0070000000000000000000000000000ffffffffffffffff040020b9c6ee1630ef3e711144a648db06bbb2284f7274cfbee53ffcee503cc1a4920040673d2343feed57e2409020d0433fa9ddbe9a790e34456f7f873533ef912b0b4a54f01c7d35f61980933e5c0127bbdf90d6266f86576ef715c9a434215960600d",
"000000000000000000000000000000000000000000000000000000000a550c1800000000000000000300000000000000000000000000000000000000000000000000000000000012220a6170746f735f636f696e087472616e7366657200022000000000000000000000000000000000000000000000000000000000000000dd080100000000000000d0070000000000000000000000000000ffffffffffffffff040020b9c6ee1630ef3e711144a648db06bbb2284f7274cfbee53ffcee503cc1a492004061bb6440bfbdfac3fff8559704303bd72544794b432ab7f9d0f3f779b6cb01aad5c86b6574f04a00698d01f4102015de056a480addd57aab600c3d4d2cba580c",
);

@@ -91,3 +91,3 @@ });

test("serialize script function payload with type args", () => {
const token = new TypeTagStruct(StructTag.fromString(`${ADDRESS_4}::test_coin::TestCoin`));
const token = new TypeTagStruct(StructTag.fromString(`${ADDRESS_4}::aptos_coin::AptosCoin`));

@@ -116,3 +116,3 @@ const scriptFunctionPayload = new TransactionPayloadScriptFunction(

expect(hexSignedTxn(signedTxn)).toBe(
"000000000000000000000000000000000000000000000000000000000a550c18000000000000000003000000000000000000000000000000000000000000000000000000000000122204636f696e087472616e736665720107000000000000000000000000000000000000000000000000000000000000000109746573745f636f696e0854657374436f696e00022000000000000000000000000000000000000000000000000000000000000000dd080100000000000000d0070000000000000000000000000000ffffffffffffffff040020b9c6ee1630ef3e711144a648db06bbb2284f7274cfbee53ffcee503cc1a49200405df30fc905d88ba1af3e35ddd063a06ef629b582aa321b9dc080f1c511f3a0336a4b416e3d2a86445281e8bb1af38b8b7f2904da9a32da2c2f170fcd90f90b0d",
"000000000000000000000000000000000000000000000000000000000a550c18000000000000000003000000000000000000000000000000000000000000000000000000000000122204636f696e087472616e73666572010700000000000000000000000000000000000000000000000000000000000000010a6170746f735f636f696e094170746f73436f696e00022000000000000000000000000000000000000000000000000000000000000000dd080100000000000000d0070000000000000000000000000000ffffffffffffffff040020b9c6ee1630ef3e711144a648db06bbb2284f7274cfbee53ffcee503cc1a4920040d7b32e9efbc640963782b11833159a3d62ba962c3f1e5580a9bab89ab012d99c38ed54ab8c0383a438a9a562b3b4b519bd31265130f2955f744125929ff23307",
);

@@ -122,3 +122,3 @@ });

test("serialize script function payload with type args but no function args", () => {
const token = new TypeTagStruct(StructTag.fromString(`${ADDRESS_4}::test_coin::TestCoin`));
const token = new TypeTagStruct(StructTag.fromString(`${ADDRESS_4}::aptos_coin::AptosCoin`));

@@ -142,3 +142,3 @@ const scriptFunctionPayload = new TransactionPayloadScriptFunction(

expect(hexSignedTxn(signedTxn)).toBe(
"000000000000000000000000000000000000000000000000000000000a550c18000000000000000003000000000000000000000000000000000000000000000000000000000000122204636f696e0966616b655f66756e630107000000000000000000000000000000000000000000000000000000000000000109746573745f636f696e0854657374436f696e0000d0070000000000000000000000000000ffffffffffffffff040020b9c6ee1630ef3e711144a648db06bbb2284f7274cfbee53ffcee503cc1a492004005d5111028f24dcdca30ff5bd61fc2c39765a9c7464098016f6e1e789d0e834e5ba8276e3cf3ec1934d96e10e31abcd439bfd72d14807e7e9af0c8ba2b1bf20d",
"000000000000000000000000000000000000000000000000000000000a550c18000000000000000003000000000000000000000000000000000000000000000000000000000000122204636f696e0966616b655f66756e63010700000000000000000000000000000000000000000000000000000000000000010a6170746f735f636f696e094170746f73436f696e0000d0070000000000000000000000000000ffffffffffffffff040020b9c6ee1630ef3e711144a648db06bbb2284f7274cfbee53ffcee503cc1a49200401609f53eab9a0ba128ab203e7d2a635390b16c16cef55b6d704a7acdb3ecf0d50fe2285eb5c8ebfca0575f6da65b91efc0a44ebd385868f04a21b28028e12101",
);

@@ -170,3 +170,3 @@ });

test("serialize script payload with type args but no function args", () => {
const token = new TypeTagStruct(StructTag.fromString(`${ADDRESS_4}::test_coin::TestCoin`));
const token = new TypeTagStruct(StructTag.fromString(`${ADDRESS_4}::aptos_coin::AptosCoin`));

@@ -190,3 +190,3 @@ const script = hexToBytes("a11ceb0b030000000105000100000000050601000000000000000600000000000000001a0102");

expect(hexSignedTxn(signedTxn)).toBe(
"000000000000000000000000000000000000000000000000000000000a550c1800000000000000000126a11ceb0b030000000105000100000000050601000000000000000600000000000000001a01020107000000000000000000000000000000000000000000000000000000000000000109746573745f636f696e0854657374436f696e0000d0070000000000000000000000000000ffffffffffffffff040020b9c6ee1630ef3e711144a648db06bbb2284f7274cfbee53ffcee503cc1a49200404f9c0b74e5d36be002645c03e8758611992435734fe8f6a29009f59073ca7300c260e9611fcd06a47f304e2557317ffda3094d6576918a342d3e78d96027580d",
"000000000000000000000000000000000000000000000000000000000a550c1800000000000000000126a11ceb0b030000000105000100000000050601000000000000000600000000000000001a0102010700000000000000000000000000000000000000000000000000000000000000010a6170746f735f636f696e094170746f73436f696e0000d0070000000000000000000000000000ffffffffffffffff040020b9c6ee1630ef3e711144a648db06bbb2284f7274cfbee53ffcee503cc1a4920040a8f35028bc99eff3896d076e603f67fb1caad39aaf0c44f535fd46f647980698c934885843ec52abea69d0159a9e8c9f7f379fd42d309a571c6184557238f10a",
);

@@ -196,3 +196,3 @@ });

test("serialize script payload with type arg and function arg", () => {
const token = new TypeTagStruct(StructTag.fromString(`${ADDRESS_4}::test_coin::TestCoin`));
const token = new TypeTagStruct(StructTag.fromString(`${ADDRESS_4}::aptos_coin::AptosCoin`));

@@ -217,3 +217,3 @@ const argU8 = new TransactionArgumentU8(2);

expect(hexSignedTxn(signedTxn)).toBe(
"000000000000000000000000000000000000000000000000000000000a550c1800000000000000000126a11ceb0b030000000105000100000000050601000000000000000600000000000000001a01020107000000000000000000000000000000000000000000000000000000000000000109746573745f636f696e0854657374436f696e00010002d0070000000000000000000000000000ffffffffffffffff040020b9c6ee1630ef3e711144a648db06bbb2284f7274cfbee53ffcee503cc1a4920040d5fe884b74b4d0d6272d6a499af4e7cf8a2932c49e69320cc1f94cf83b1eeea8df6ff63f95e0ec3b8185ce552ddb498d61b7a42d14b4fc69aa52bead92b21c0c",
"000000000000000000000000000000000000000000000000000000000a550c1800000000000000000126a11ceb0b030000000105000100000000050601000000000000000600000000000000001a0102010700000000000000000000000000000000000000000000000000000000000000010a6170746f735f636f696e094170746f73436f696e00010002d0070000000000000000000000000000ffffffffffffffff040020b9c6ee1630ef3e711144a648db06bbb2284f7274cfbee53ffcee503cc1a4920040662b626455b62ca41ef35b34c74ef0b848c5b3679ae3cf32af47d10ef3372ed4060cfaaeee6ab71ab0034951c21e589d70512c8c536625f532ebf9f127867209",
);

@@ -223,3 +223,3 @@ });

test("serialize script payload with one type arg and two function args", () => {
const token = new TypeTagStruct(StructTag.fromString(`${ADDRESS_4}::test_coin::TestCoin`));
const token = new TypeTagStruct(StructTag.fromString(`${ADDRESS_4}::aptos_coin::AptosCoin`));

@@ -246,3 +246,3 @@ const argU8Vec = new TransactionArgumentU8Vector(bcsSerializeUint64(1));

expect(hexSignedTxn(signedTxn)).toBe(
"000000000000000000000000000000000000000000000000000000000a550c1800000000000000000126a11ceb0b030000000105000100000000050601000000000000000600000000000000001a01020107000000000000000000000000000000000000000000000000000000000000000109746573745f636f696e0854657374436f696e000204080100000000000000030000000000000000000000000000000000000000000000000000000000000001d0070000000000000000000000000000ffffffffffffffff040020b9c6ee1630ef3e711144a648db06bbb2284f7274cfbee53ffcee503cc1a4920040480f9557d54671a25ed25a67ffe39de64af0c27286d96d3119609ac62ec67f5fc8316a6126e16b33ce16646865377c0941b80c23c530f8894bb5a551c6477107",
"000000000000000000000000000000000000000000000000000000000a550c1800000000000000000126a11ceb0b030000000105000100000000050601000000000000000600000000000000001a0102010700000000000000000000000000000000000000000000000000000000000000010a6170746f735f636f696e094170746f73436f696e000204080100000000000000030000000000000000000000000000000000000000000000000000000000000001d0070000000000000000000000000000ffffffffffffffff040020b9c6ee1630ef3e711144a648db06bbb2284f7274cfbee53ffcee503cc1a4920040df17e1241a61001ea00141f96bdb0a8584fc68792db6297b1941b244d22accdeb344c2d575aea950046d18aacf056c234c054e32d096f22dd7151ba0e3fdc00e",
);

@@ -249,0 +249,0 @@ });

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc