Skip to content

Commit b4b794e

Browse files
fix(sdk-coin-apt): use NoAccountAuthenticator for transaction simulation
2 parents 9df7d6b + 7ff087c commit b4b794e

File tree

3 files changed

+36
-13
lines changed

3 files changed

+36
-13
lines changed

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

Lines changed: 28 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,9 @@ import {
1010
import { BaseCoin as CoinConfig } from '@bitgo/statics';
1111
import {
1212
AccountAddress,
13+
AccountAuthenticator,
1314
AccountAuthenticatorEd25519,
15+
AccountAuthenticatorNoAccountAuthenticator,
1416
DEFAULT_MAX_GAS_AMOUNT,
1517
Ed25519PublicKey,
1618
Ed25519Signature,
@@ -44,6 +46,7 @@ export abstract class Transaction extends BaseTransaction {
4446
protected _expirationTime: number;
4547
protected _feePayerAddress: string;
4648
protected _assetId: string;
49+
protected _isSimulateTxn: boolean;
4750

4851
static EMPTY_PUBLIC_KEY = Buffer.alloc(32);
4952
static EMPTY_SIGNATURE = Buffer.alloc(64);
@@ -57,6 +60,7 @@ export abstract class Transaction extends BaseTransaction {
5760
this._sequenceNumber = 0;
5861
this._sender = AccountAddress.ZERO.toString();
5962
this._assetId = AccountAddress.ZERO.toString();
63+
this._isSimulateTxn = false;
6064
this._senderSignature = {
6165
publicKey: {
6266
pub: Hex.fromHexInput(Transaction.EMPTY_PUBLIC_KEY).toString(),
@@ -150,6 +154,14 @@ export abstract class Transaction extends BaseTransaction {
150154
this._assetId = value;
151155
}
152156

157+
get isSimulateTxn(): boolean {
158+
return this._isSimulateTxn;
159+
}
160+
161+
set isSimulateTxn(value: boolean) {
162+
this._isSimulateTxn = value;
163+
}
164+
153165
protected abstract buildRawTransaction(): void;
154166

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

198210
serialize(): string {
199-
const senderPublicKeyBuffer = utils.getBufferFromHexString(this._senderSignature.publicKey.pub);
200-
const senderPublicKey = new Ed25519PublicKey(senderPublicKeyBuffer);
201-
202-
const senderSignature = new Ed25519Signature(this._senderSignature.signature);
203-
const senderAuthenticator = new AccountAuthenticatorEd25519(senderPublicKey, senderSignature);
204-
205-
const feePayerPublicKeyBuffer = utils.getBufferFromHexString(this._feePayerSignature.publicKey.pub);
206-
const feePayerPublicKey = new Ed25519PublicKey(feePayerPublicKeyBuffer);
207-
208-
const feePayerSignature = new Ed25519Signature(this._feePayerSignature.signature);
209-
const feePayerAuthenticator = new AccountAuthenticatorEd25519(feePayerPublicKey, feePayerSignature);
211+
let senderAuthenticator: AccountAuthenticator;
212+
let feePayerAuthenticator: AccountAuthenticator;
213+
if (this.isSimulateTxn) {
214+
senderAuthenticator = new AccountAuthenticatorNoAccountAuthenticator();
215+
feePayerAuthenticator = new AccountAuthenticatorNoAccountAuthenticator();
216+
} else {
217+
const senderPublicKeyBuffer = utils.getBufferFromHexString(this._senderSignature.publicKey.pub);
218+
const senderPublicKey = new Ed25519PublicKey(senderPublicKeyBuffer);
219+
const senderSignature = new Ed25519Signature(this._senderSignature.signature);
220+
senderAuthenticator = new AccountAuthenticatorEd25519(senderPublicKey, senderSignature);
221+
222+
const feePayerPublicKeyBuffer = utils.getBufferFromHexString(this._feePayerSignature.publicKey.pub);
223+
const feePayerPublicKey = new Ed25519PublicKey(feePayerPublicKeyBuffer);
224+
const feePayerSignature = new Ed25519Signature(this._feePayerSignature.signature);
225+
feePayerAuthenticator = new AccountAuthenticatorEd25519(feePayerPublicKey, feePayerSignature);
226+
}
210227

211228
const txnAuthenticator = new TransactionAuthenticatorFeePayer(senderAuthenticator, [], [], {
212229
address: AccountAddress.fromString(this._feePayerAddress),

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,10 @@ export abstract class TransactionBuilder extends BaseTransactionBuilder {
100100
this.transaction.addFeePayerSignature(publicKey, signature);
101101
}
102102

103+
setIsSimulateTxn(value: boolean): void {
104+
this.transaction.isSimulateTxn = value;
105+
}
106+
103107
/** @inheritdoc */
104108
protected fromImplementation(rawTransaction: string): Transaction {
105109
this.transaction.fromRawTransaction(rawTransaction);

modules/sdk-coin-apt/test/unit/transactionBuilder/transferBuilder.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ describe('Apt Transfer Transaction', () => {
2121
txBuilder.sequenceNumber(14);
2222
txBuilder.expirationTime(1736246155);
2323
txBuilder.addFeePayerAddress(testData.feePayer.address);
24+
txBuilder.setIsSimulateTxn(true);
2425
const tx = (await txBuilder.build()) as TransferTransaction;
2526
should.equal(tx.sender, testData.sender2.address);
2627
should.equal(tx.recipient.address, testData.recipients[0].address);
@@ -45,7 +46,7 @@ describe('Apt Transfer Transaction', () => {
4546
const rawTx = tx.toBroadcastFormat();
4647
should.equal(txBuilder.isValidRawTransaction(rawTx), true);
4748
rawTx.should.equal(
48-
'0x1aed808916ab9b1b30b07abb53561afd46847285ce28651221d406173a3724490e000000000000000200000000000000000000000000000000000000000000000000000000000000010d6170746f735f6163636f756e740e7472616e736665725f636f696e73010700000000000000000000000000000000000000000000000000000000000000010a6170746f735f636f696e094170746f73436f696e000220f7405c28a02cf5bab4ea4498240bb3579db45951794eb1c843bef0534c093ad908e803000000000000400d03000000000064000000000000008b037d670000000002030020000000000000000000000000000000000000000000000000000000000000000040000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000dbc87a1c816d9bcd06b683c37e80c7162e4d48da7812198b830e4d5d8e0629f2002000000000000000000000000000000000000000000000000000000000000000004000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000'
49+
'0x1aed808916ab9b1b30b07abb53561afd46847285ce28651221d406173a3724490e000000000000000200000000000000000000000000000000000000000000000000000000000000010d6170746f735f6163636f756e740e7472616e736665725f636f696e73010700000000000000000000000000000000000000000000000000000000000000010a6170746f735f636f696e094170746f73436f696e000220f7405c28a02cf5bab4ea4498240bb3579db45951794eb1c843bef0534c093ad908e803000000000000400d03000000000064000000000000008b037d67000000000203040000dbc87a1c816d9bcd06b683c37e80c7162e4d48da7812198b830e4d5d8e0629f204'
4950
);
5051
});
5152

@@ -178,6 +179,7 @@ describe('Apt Transfer Transaction', () => {
178179
txBuilder.expirationTime(1736246155);
179180
txBuilder.assetId(testData.LEGACY_COIN);
180181
txBuilder.addFeePayerAddress(testData.feePayer.address);
182+
txBuilder.setIsSimulateTxn(true);
181183
const tx = (await txBuilder.build()) as TransferTransaction;
182184
should.equal(tx.sender, testData.sender2.address);
183185
should.equal(tx.recipient.address, testData.recipients[0].address);
@@ -202,7 +204,7 @@ describe('Apt Transfer Transaction', () => {
202204
const rawTx = tx.toBroadcastFormat();
203205
should.equal(txBuilder.isValidRawTransaction(rawTx), true);
204206
rawTx.should.equal(
205-
'0x1aed808916ab9b1b30b07abb53561afd46847285ce28651221d406173a3724490e000000000000000200000000000000000000000000000000000000000000000000000000000000010d6170746f735f6163636f756e740e7472616e736665725f636f696e7301074fb379c10c763a13e724064ecfb7d946690bea519ba982c81b518d1c11dd23fe0766615f7465737405436f696e7a000220f7405c28a02cf5bab4ea4498240bb3579db45951794eb1c843bef0534c093ad908e803000000000000400d03000000000064000000000000008b037d670000000002030020000000000000000000000000000000000000000000000000000000000000000040000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000dbc87a1c816d9bcd06b683c37e80c7162e4d48da7812198b830e4d5d8e0629f2002000000000000000000000000000000000000000000000000000000000000000004000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000'
207+
'0x1aed808916ab9b1b30b07abb53561afd46847285ce28651221d406173a3724490e000000000000000200000000000000000000000000000000000000000000000000000000000000010d6170746f735f6163636f756e740e7472616e736665725f636f696e7301074fb379c10c763a13e724064ecfb7d946690bea519ba982c81b518d1c11dd23fe0766615f7465737405436f696e7a000220f7405c28a02cf5bab4ea4498240bb3579db45951794eb1c843bef0534c093ad908e803000000000000400d03000000000064000000000000008b037d67000000000203040000dbc87a1c816d9bcd06b683c37e80c7162e4d48da7812198b830e4d5d8e0629f204'
206208
);
207209
});
208210

0 commit comments

Comments
 (0)