Skip to content

Commit

Permalink
fix(sdk-coin-apt): use NoAccountAuthenticator for transaction simulation
Browse files Browse the repository at this point in the history
TICKET: COIN-3065
  • Loading branch information
bhavidhingra committed Feb 8, 2025
1 parent 71faec2 commit 7ff087c
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 13 deletions.
39 changes: 28 additions & 11 deletions modules/sdk-coin-apt/src/lib/transaction/transaction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,9 @@ import {
import { BaseCoin as CoinConfig } from '@bitgo/statics';
import {
AccountAddress,
AccountAuthenticator,
AccountAuthenticatorEd25519,
AccountAuthenticatorNoAccountAuthenticator,
DEFAULT_MAX_GAS_AMOUNT,
Ed25519PublicKey,
Ed25519Signature,
Expand Down Expand Up @@ -44,6 +46,7 @@ export abstract class Transaction extends BaseTransaction {
protected _expirationTime: number;
protected _feePayerAddress: string;
protected _assetId: string;
protected _isSimulateTxn: boolean;

static EMPTY_PUBLIC_KEY = Buffer.alloc(32);
static EMPTY_SIGNATURE = Buffer.alloc(64);
Expand All @@ -57,6 +60,7 @@ export abstract class Transaction extends BaseTransaction {
this._sequenceNumber = 0;
this._sender = AccountAddress.ZERO.toString();
this._assetId = AccountAddress.ZERO.toString();
this._isSimulateTxn = false;
this._senderSignature = {
publicKey: {
pub: Hex.fromHexInput(Transaction.EMPTY_PUBLIC_KEY).toString(),
Expand Down Expand Up @@ -150,6 +154,14 @@ export abstract class Transaction extends BaseTransaction {
this._assetId = value;
}

get isSimulateTxn(): boolean {
return this._isSimulateTxn;
}

set isSimulateTxn(value: boolean) {
this._isSimulateTxn = value;
}

protected abstract buildRawTransaction(): void;

protected abstract parseTransactionPayload(payload: TransactionPayload): void;
Expand Down Expand Up @@ -196,17 +208,22 @@ export abstract class Transaction extends BaseTransaction {
}

serialize(): string {
const senderPublicKeyBuffer = utils.getBufferFromHexString(this._senderSignature.publicKey.pub);
const senderPublicKey = new Ed25519PublicKey(senderPublicKeyBuffer);

const senderSignature = new Ed25519Signature(this._senderSignature.signature);
const senderAuthenticator = new AccountAuthenticatorEd25519(senderPublicKey, senderSignature);

const feePayerPublicKeyBuffer = utils.getBufferFromHexString(this._feePayerSignature.publicKey.pub);
const feePayerPublicKey = new Ed25519PublicKey(feePayerPublicKeyBuffer);

const feePayerSignature = new Ed25519Signature(this._feePayerSignature.signature);
const feePayerAuthenticator = new AccountAuthenticatorEd25519(feePayerPublicKey, feePayerSignature);
let senderAuthenticator: AccountAuthenticator;
let feePayerAuthenticator: AccountAuthenticator;
if (this.isSimulateTxn) {
senderAuthenticator = new AccountAuthenticatorNoAccountAuthenticator();
feePayerAuthenticator = new AccountAuthenticatorNoAccountAuthenticator();
} else {
const senderPublicKeyBuffer = utils.getBufferFromHexString(this._senderSignature.publicKey.pub);
const senderPublicKey = new Ed25519PublicKey(senderPublicKeyBuffer);
const senderSignature = new Ed25519Signature(this._senderSignature.signature);
senderAuthenticator = new AccountAuthenticatorEd25519(senderPublicKey, senderSignature);

const feePayerPublicKeyBuffer = utils.getBufferFromHexString(this._feePayerSignature.publicKey.pub);
const feePayerPublicKey = new Ed25519PublicKey(feePayerPublicKeyBuffer);
const feePayerSignature = new Ed25519Signature(this._feePayerSignature.signature);
feePayerAuthenticator = new AccountAuthenticatorEd25519(feePayerPublicKey, feePayerSignature);
}

const txnAuthenticator = new TransactionAuthenticatorFeePayer(senderAuthenticator, [], [], {
address: AccountAddress.fromString(this._feePayerAddress),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,10 @@ export abstract class TransactionBuilder extends BaseTransactionBuilder {
this.transaction.addFeePayerSignature(publicKey, signature);
}

setIsSimulateTxn(value: boolean): void {
this.transaction.isSimulateTxn = value;
}

/** @inheritdoc */
protected fromImplementation(rawTransaction: string): Transaction {
this.transaction.fromRawTransaction(rawTransaction);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ describe('Apt Transfer Transaction', () => {
txBuilder.sequenceNumber(14);
txBuilder.expirationTime(1736246155);
txBuilder.addFeePayerAddress(testData.feePayer.address);
txBuilder.setIsSimulateTxn(true);
const tx = (await txBuilder.build()) as TransferTransaction;
should.equal(tx.sender, testData.sender2.address);
should.equal(tx.recipient.address, testData.recipients[0].address);
Expand All @@ -45,7 +46,7 @@ describe('Apt Transfer Transaction', () => {
const rawTx = tx.toBroadcastFormat();
should.equal(txBuilder.isValidRawTransaction(rawTx), true);
rawTx.should.equal(
'0x1aed808916ab9b1b30b07abb53561afd46847285ce28651221d406173a3724490e000000000000000200000000000000000000000000000000000000000000000000000000000000010d6170746f735f6163636f756e740e7472616e736665725f636f696e73010700000000000000000000000000000000000000000000000000000000000000010a6170746f735f636f696e094170746f73436f696e000220f7405c28a02cf5bab4ea4498240bb3579db45951794eb1c843bef0534c093ad908e803000000000000400d03000000000064000000000000008b037d670000000002030020000000000000000000000000000000000000000000000000000000000000000040000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000dbc87a1c816d9bcd06b683c37e80c7162e4d48da7812198b830e4d5d8e0629f2002000000000000000000000000000000000000000000000000000000000000000004000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000'
'0x1aed808916ab9b1b30b07abb53561afd46847285ce28651221d406173a3724490e000000000000000200000000000000000000000000000000000000000000000000000000000000010d6170746f735f6163636f756e740e7472616e736665725f636f696e73010700000000000000000000000000000000000000000000000000000000000000010a6170746f735f636f696e094170746f73436f696e000220f7405c28a02cf5bab4ea4498240bb3579db45951794eb1c843bef0534c093ad908e803000000000000400d03000000000064000000000000008b037d67000000000203040000dbc87a1c816d9bcd06b683c37e80c7162e4d48da7812198b830e4d5d8e0629f204'
);
});

Expand Down Expand Up @@ -178,6 +179,7 @@ describe('Apt Transfer Transaction', () => {
txBuilder.expirationTime(1736246155);
txBuilder.assetId(testData.LEGACY_COIN);
txBuilder.addFeePayerAddress(testData.feePayer.address);
txBuilder.setIsSimulateTxn(true);
const tx = (await txBuilder.build()) as TransferTransaction;
should.equal(tx.sender, testData.sender2.address);
should.equal(tx.recipient.address, testData.recipients[0].address);
Expand All @@ -202,7 +204,7 @@ describe('Apt Transfer Transaction', () => {
const rawTx = tx.toBroadcastFormat();
should.equal(txBuilder.isValidRawTransaction(rawTx), true);
rawTx.should.equal(
'0x1aed808916ab9b1b30b07abb53561afd46847285ce28651221d406173a3724490e000000000000000200000000000000000000000000000000000000000000000000000000000000010d6170746f735f6163636f756e740e7472616e736665725f636f696e7301074fb379c10c763a13e724064ecfb7d946690bea519ba982c81b518d1c11dd23fe0766615f7465737405436f696e7a000220f7405c28a02cf5bab4ea4498240bb3579db45951794eb1c843bef0534c093ad908e803000000000000400d03000000000064000000000000008b037d670000000002030020000000000000000000000000000000000000000000000000000000000000000040000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000dbc87a1c816d9bcd06b683c37e80c7162e4d48da7812198b830e4d5d8e0629f2002000000000000000000000000000000000000000000000000000000000000000004000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000'
'0x1aed808916ab9b1b30b07abb53561afd46847285ce28651221d406173a3724490e000000000000000200000000000000000000000000000000000000000000000000000000000000010d6170746f735f6163636f756e740e7472616e736665725f636f696e7301074fb379c10c763a13e724064ecfb7d946690bea519ba982c81b518d1c11dd23fe0766615f7465737405436f696e7a000220f7405c28a02cf5bab4ea4498240bb3579db45951794eb1c843bef0534c093ad908e803000000000000400d03000000000064000000000000008b037d67000000000203040000dbc87a1c816d9bcd06b683c37e80c7162e4d48da7812198b830e4d5d8e0629f204'
);
});

Expand Down

0 comments on commit 7ff087c

Please sign in to comment.