Skip to content

Commit 3391628

Browse files
authored
Merge pull request #5900 from BitGo/COIN-3733-fungible-bug
fix(sdk-coin-apt): fungible fix verify transaction flow
2 parents 60bb013 + 2f4746f commit 3391628

File tree

4 files changed

+41
-31
lines changed

4 files changed

+41
-31
lines changed

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

Lines changed: 19 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ import {
1515
VerifyTransactionOptions,
1616
} from '@bitgo/sdk-core';
1717
import { BaseCoin as StaticsBaseCoin, coins } from '@bitgo/statics';
18-
import { KeyPair as AptKeyPair, Transaction, TransactionBuilderFactory, TransferTransaction } from './lib';
18+
import { KeyPair as AptKeyPair, TransactionBuilderFactory } from './lib';
1919
import utils from './lib/utils';
2020
import * as _ from 'lodash';
2121
import BigNumber from 'bignumber.js';
@@ -79,20 +79,16 @@ export class Apt extends BaseCoin {
7979
return true;
8080
}
8181

82-
getTransaction(coinConfig: Readonly<StaticsBaseCoin>): Transaction {
83-
return new TransferTransaction(coinConfig);
84-
}
85-
8682
async verifyTransaction(params: VerifyTransactionOptions): Promise<boolean> {
87-
const coinConfig = coins.get(this.getChain());
8883
const { txPrebuild: txPrebuild, txParams: txParams } = params;
89-
const transaction = this.getTransaction(coinConfig);
90-
const rawTx = txPrebuild.txHex;
91-
if (!rawTx) {
84+
const txHex = txPrebuild.txHex;
85+
if (!txHex) {
9286
throw new Error('missing required tx prebuild property txHex');
9387
}
94-
transaction.fromRawTransaction(rawTx);
95-
const explainedTx = transaction.explainTransaction();
88+
const explainedTx = await this.explainTransaction({ txHex });
89+
if (!explainedTx) {
90+
throw new Error('failed to explain transaction');
91+
}
9692
if (txParams.recipients !== undefined) {
9793
const filteredRecipients = txParams.recipients?.map((recipient) => {
9894
return {
@@ -155,11 +151,9 @@ export class Apt extends BaseCoin {
155151
* @param params
156152
*/
157153
async explainTransaction(params: ExplainTransactionOptions): Promise<AptTransactionExplanation | undefined> {
158-
const factory = this.getBuilder();
159154
let rebuiltTransaction: BaseTransaction;
160155
try {
161-
const transactionBuilder = factory.from(params.txHex);
162-
rebuiltTransaction = await transactionBuilder.build();
156+
rebuiltTransaction = await this.rebuildTransaction(params.txHex);
163157
} catch {
164158
return undefined;
165159
}
@@ -190,7 +184,17 @@ export class Apt extends BaseCoin {
190184
throw new Error('Method not implemented.');
191185
}
192186

193-
private getBuilder(): TransactionBuilderFactory {
187+
protected getTxBuilderFactory(): TransactionBuilderFactory {
194188
return new TransactionBuilderFactory(coins.get(this.getChain()));
195189
}
190+
191+
protected async rebuildTransaction(txHex: string): Promise<BaseTransaction> {
192+
const txBuilderFactory = this.getTxBuilderFactory();
193+
try {
194+
const txBuilder = txBuilderFactory.from(txHex);
195+
return await txBuilder.build();
196+
} catch {
197+
throw new Error('Failed to rebuild transaction');
198+
}
199+
}
196200
}

modules/sdk-coin-apt/src/aptToken.ts

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
import { Apt } from './apt';
22
import { BitGoBase, CoinConstructor, NamedCoinConstructor } from '@bitgo/sdk-core';
3-
import { AptTokenConfig, BaseCoin as StaticsBaseCoin, coins, tokens } from '@bitgo/statics';
4-
import { FungibleAssetTransfer, Transaction } from './lib';
3+
import { AptTokenConfig, coins, tokens } from '@bitgo/statics';
54

65
export class AptToken extends Apt {
76
public readonly tokenConfig: AptTokenConfig;
@@ -46,10 +45,6 @@ export class AptToken extends Apt {
4645
return this.tokenConfig.decimalPlaces;
4746
}
4847

49-
getTransaction(coinConfig: Readonly<StaticsBaseCoin>): Transaction {
50-
return new FungibleAssetTransfer(coinConfig);
51-
}
52-
5348
getChain(): string {
5449
return this.tokenConfig.type;
5550
}

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

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ export class TransactionBuilderFactory extends BaseTransactionBuilderFactory {
1919
/** @inheritdoc */
2020
from(signedRawTxn: string): TransactionBuilder {
2121
try {
22-
const signedTxn = this.parseTransaction(signedRawTxn);
22+
const signedTxn = Transaction.deserializeSignedTransaction(signedRawTxn);
2323
const txnType = this.getTransactionTypeFromSignedTxn(signedTxn);
2424
switch (txnType) {
2525
case TransactionType.Send:
@@ -80,13 +80,4 @@ export class TransactionBuilderFactory extends BaseTransactionBuilderFactory {
8080
}
8181
return builder;
8282
}
83-
84-
/** Parse the transaction from a signed txn hex string
85-
*
86-
* @param {string} signedRawTransaction - the signed txn hex
87-
* @returns {SignedTransaction} parsedtransaction
88-
*/
89-
private parseTransaction(signedRawTransaction: string): SignedTransaction {
90-
return Transaction.deserializeSignedTransaction(signedRawTransaction);
91-
}
9283
}

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

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,16 @@ describe('APT:', function () {
3535
recipients: testData.recipients,
3636
};
3737

38+
const batchFungibleTxPrebuild = {
39+
txHex: testData.FUNGIBLE_BATCH_RAW_TX_HEX,
40+
txInfo: {},
41+
};
42+
43+
const batchFungibleTxParams = {
44+
recipients: testData.batchFungibleRecipients,
45+
type: 'transfer',
46+
};
47+
3848
before(function () {
3949
bitgo = TestBitGo.decorate(BitGoAPI, { env: 'mock' });
4050
bitgo.safeRegister('apt', Apt.createInstance);
@@ -96,6 +106,16 @@ describe('APT:', function () {
96106
isTransactionVerified.should.equal(true);
97107
});
98108

109+
it('should succeed to verify fungible transaction', async function () {
110+
const verification = {};
111+
const isTransactionVerified = await basecoin.verifyTransaction({
112+
txPrebuild: batchFungibleTxPrebuild,
113+
txParams: batchFungibleTxParams,
114+
verification,
115+
});
116+
isTransactionVerified.should.equal(true);
117+
});
118+
99119
it('should succeed to verify transaction when recipients amount are numbers', async function () {
100120
const txPrebuild = newTxPrebuild();
101121
const txParamsWithNumberAmounts = newTxParams();

0 commit comments

Comments
 (0)