Skip to content

Commit 63cac67

Browse files
authored
Merge pull request #7093 from BitGo/apt-deserialize-signable-payload-hex
feat: add utility to deserialize APT signable payload hex
2 parents 50f69f6 + 9f586ce commit 63cac67

File tree

2 files changed

+47
-4
lines changed

2 files changed

+47
-4
lines changed

modules/sdk-coin-apt/src/lib/utils.ts

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import {
44
Deserializer,
55
Ed25519PublicKey,
66
EntryFunctionArgument,
7+
FeePayerRawTransaction,
78
Hex,
89
SignedTransaction,
910
TransactionPayload,
@@ -19,18 +20,18 @@ import {
1920
TransactionType,
2021
} from '@bitgo/sdk-core';
2122
import {
23+
ADDRESS_BYTES_LENGTH,
24+
AMOUNT_BYTES_LENGTH,
2225
APT_ADDRESS_LENGTH,
2326
APT_BLOCK_ID_LENGTH,
2427
APT_SIGNATURE_LENGTH,
2528
APT_TRANSACTION_ID_LENGTH,
2629
COIN_BATCH_TRANSFER_FUNCTION,
2730
COIN_TRANSFER_FUNCTION,
2831
DIGITAL_ASSET_TRANSFER_FUNCTION,
32+
FUNGIBLE_ASSET_BATCH_TRANSFER_FUNCTION,
2933
FUNGIBLE_ASSET_TRANSFER_FUNCTION,
3034
SECONDS_PER_WEEK,
31-
ADDRESS_BYTES_LENGTH,
32-
AMOUNT_BYTES_LENGTH,
33-
FUNGIBLE_ASSET_BATCH_TRANSFER_FUNCTION,
3435
} from './constants';
3536
import BigNumber from 'bignumber.js';
3637
import { RecipientsValidationResult } from './iface';
@@ -148,6 +149,12 @@ export class Utils implements BaseUtils {
148149
return deserializer.deserialize(SignedTransaction);
149150
}
150151

152+
deserializeFeePayerRawTransaction(rawTransaction: string): FeePayerRawTransaction {
153+
const txnBytes = Hex.fromHexString(rawTransaction).toUint8Array();
154+
const deserializer = new Deserializer(txnBytes);
155+
return FeePayerRawTransaction.load(deserializer);
156+
}
157+
151158
deserializeAccountAddressVector(serializedBytes: Uint8Array): string[] {
152159
const deserializer = new Deserializer(serializedBytes);
153160
const deserializedAddresses = deserializer.deserializeVector(AccountAddress);

modules/sdk-coin-apt/test/unit/utils.ts

Lines changed: 37 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,11 @@
11
import * as testData from '../resources/apt';
22
import should from 'should';
33
import utils from '../../src/lib/utils';
4-
import { SignedTransaction, TransactionAuthenticatorFeePayer } from '@aptos-labs/ts-sdk';
4+
import {
5+
SignedTransaction,
6+
TransactionAuthenticatorFeePayer,
7+
TransactionPayloadEntryFunction,
8+
} from '@aptos-labs/ts-sdk';
59

610
describe('Aptos util library', function () {
711
describe('isValidAddress', function () {
@@ -48,4 +52,36 @@ describe('Aptos util library', function () {
4852
// without 0x prefix
4953
should.equal(true, utils.isValidPublicKey('9b4e96086d111500259f9b38680b0509a405c1904da18976455a20c691d3bb07'));
5054
});
55+
56+
it('deserializeFeePayerRawTransaction', function () {
57+
const signablePayloadHex =
58+
'5efa3c4f02f83a0f4b2d69fc95c607cc02825cc4e7be536ef0992df050d9e67c01f22fa009b6473cdc539ecbd570d00d0585682f5939ffe256a90ddee0d616292f000000000000000002000000000000000000000000000000000000000000000000000000000000000104636f696e087472616e73666572010700000000000000000000000000000000000000000000000000000000000000010a6170746f735f636f696e094170746f73436f696e000220da22bdc19f873fd6ce48c32912965c8a9dde578b2a3cf4fae3dd85dfaac784c908e803000000000000a8610000000000006400000000000000901cdd68000000000200adba44b46722b8da1797645f71ddcdab28626c06acd36b88d5198a684966306c';
59+
const feePayerRawTransactionHex = signablePayloadHex.slice(66); // remove the first 33 bytes (66 hex characters)
60+
const feePayerRawTransaction = utils.deserializeFeePayerRawTransaction(feePayerRawTransactionHex);
61+
const rawTxn = feePayerRawTransaction.raw_txn;
62+
63+
const sender = rawTxn.sender.toString();
64+
const sequenceNumber = utils.castToNumber(rawTxn.sequence_number);
65+
const maxGasAmount = utils.castToNumber(rawTxn.max_gas_amount);
66+
const gasUnitPrice = utils.castToNumber(rawTxn.gas_unit_price);
67+
const expirationTime = utils.castToNumber(rawTxn.expiration_timestamp_secs);
68+
should.equal(sender, '0xf22fa009b6473cdc539ecbd570d00d0585682f5939ffe256a90ddee0d616292f');
69+
should.equal(sequenceNumber, 0);
70+
should.equal(maxGasAmount, 25000);
71+
should.equal(gasUnitPrice, 100);
72+
should.equal(expirationTime, 1759321232);
73+
74+
const entryFunction = (rawTxn.payload as TransactionPayloadEntryFunction).entryFunction;
75+
const functionName = `${entryFunction.module_name.address.toString()}::${entryFunction.module_name.name.identifier.toString()}::${entryFunction.function_name.identifier.toString()}`;
76+
should.equal(functionName, '0x1::coin::transfer');
77+
const assetId = entryFunction.type_args[0].toString();
78+
should.equal(assetId, '0x1::aptos_coin::AptosCoin');
79+
80+
const addressArg = entryFunction.args[0];
81+
const amountArg = entryFunction.args[1];
82+
const recipients = utils.parseRecipients(addressArg, amountArg);
83+
should.equal(recipients.length, 1);
84+
should.equal(recipients[0].address, '0xda22bdc19f873fd6ce48c32912965c8a9dde578b2a3cf4fae3dd85dfaac784c9');
85+
should.equal(recipients[0].amount, '1000');
86+
});
5187
});

0 commit comments

Comments
 (0)