Skip to content

Commit

Permalink
Merge pull request #4048 from BitGo/EA-2171-ton-sdk-fix-add-signature
Browse files Browse the repository at this point in the history
fix(coin-sdk-coin): fix add signature
  • Loading branch information
ewangbitgo authored Nov 8, 2023
2 parents e8f577b + 497d42a commit 95b88f0
Show file tree
Hide file tree
Showing 3 changed files with 7 additions and 13 deletions.
4 changes: 3 additions & 1 deletion modules/sdk-coin-ton/src/lib/transaction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,12 +59,14 @@ export class Transaction extends BaseTransaction {
const signingMessage = this.createSigningMessage(WALLET_ID, this.seqno, this.expireTime);
const sendMode = 3;
signingMessage.bits.writeUint8(sendMode);
signingMessage.refs.push(this.createOutMsg(this.recipient.address, this.recipient.amount, this.message));
const outMsg = this.createOutMsg(this.recipient.address, this.recipient.amount, this.message);
signingMessage.refs.push(outMsg);
this.unsignedMessage = Buffer.from(await signingMessage.hash()).toString('hex');

const signature =
this._signatures.length > 0 ? this._signatures[0] : Buffer.from(new Uint8Array(64)).toString('hex');
const finalMessage = await this.createExternalMessage(signingMessage, this.seqno, signature);

this.finalMessage = TonWeb.utils.bytesToBase64(await finalMessage.toBoc(false));

this._id = TonWeb.utils.bytesToBase64(await finalMessage.hash());
Expand Down
8 changes: 1 addition & 7 deletions modules/sdk-coin-ton/src/lib/transactionBuilder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ export abstract class TransactionBuilder extends BaseTransactionBuilder {

/** @inheritDoc */
addSignature(publicKey: BasePublicKey, signature: Buffer): void {
this.transaction.signature.push(signature.toString('hex'));
this.transaction.signature[0] = signature.toString('hex');
}

/**
Expand Down Expand Up @@ -73,12 +73,6 @@ export abstract class TransactionBuilder extends BaseTransactionBuilder {
/** @inheritdoc */
protected async buildImplementation(): Promise<Transaction> {
await this.transaction.build();
// if (this.si) {
// this.transaction.sign(this._signer);
// }
// if (this._signatures?.length > 0) {
// this.transaction.constructSignedPayload(this._signatures[0].signature);
// }
this.transaction.loadInputsAndOutputs();
return this.transaction;
}
Expand Down
8 changes: 3 additions & 5 deletions modules/sdk-coin-ton/test/unit/transferBuilder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ describe('Ton Transfer Builder', () => {
it('should build a send from rawTx', async function () {
const txBuilder = factory.from(testData.signedTransaction.tx);
const builtTx = await txBuilder.build();
const jsonTx = builtTx.toJson();
should.equal(builtTx.type, TransactionType.Send);
should.equal(builtTx.signablePayload.toString('base64'), testData.signedTransaction.signable);
// should.equal(builtTx.id, testData.signedTransaction.txId);
Expand All @@ -50,7 +51,6 @@ describe('Ton Transfer Builder', () => {
should.equal(builtTx.toBroadcastFormat(), testData.signedTransaction.tx);
builtTx.inputs.length.should.equal(1);
builtTx.outputs.length.should.equal(1);
const jsonTx = builtTx.toJson();
jsonTx.sender.should.equal('EQCSBjR3fUOL98WTw2F_IT4BrcqjZJWVLWUSz5WQDpaL9Jpl');
jsonTx.destination.should.equal('EQA0i8-CdGnF_DhUHHf92R1ONH6sIA9vLZ_WLcCIhfBBXwtG');
jsonTx.amount.should.equal('10000000');
Expand Down Expand Up @@ -82,16 +82,14 @@ describe('Ton Transfer Builder', () => {
should.equal(tx.type, TransactionType.Send);
const signable = tx.signablePayload;
const signature = keyPair.signMessageinUint8Array(signable);
txBuilder.addSignature(keyPair.getKeys(), Buffer.from(signature));
const signedTx = await txBuilder.build();
const builder2 = factory.from(signedTx.toBroadcastFormat());
builder2.addSignature(keyPair.getKeys(), Buffer.from(signature));
const tx2 = await builder2.build();
const signature2 = keyPair.signMessageinUint8Array(tx2.signablePayload);
should.equal(Buffer.from(signature).toString('hex'), Buffer.from(signature2).toString('hex'));
should.equal(tx.toBroadcastFormat(), tx2.toBroadcastFormat());

await new Promise((resolve) => setTimeout(resolve, 2000));
const result = await tonweb.provider.sendBoc(signedTx.toBroadcastFormat());
const result = await tonweb.provider.sendBoc(tx2.toBroadcastFormat());
console.log(JSON.stringify(result));
});

Expand Down

0 comments on commit 95b88f0

Please sign in to comment.