Skip to content

Commit 7d5bc11

Browse files
committed
feat(sdk-coin-canton): added pre-approval builder
Ticket: COIN-5918
1 parent 5a75d7a commit 7d5bc11

File tree

3 files changed

+50
-21
lines changed

3 files changed

+50
-21
lines changed
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
import { TransactionType } from '@bitgo/sdk-core';
2+
import { BaseCoin as CoinConfig } from '@bitgo/statics';
3+
import { TransactionBuilder } from './transactionBuilder';
4+
5+
export class OneStepPreApprovalBuilder extends TransactionBuilder {
6+
private _partyId: string;
7+
constructor(_coinConfig: Readonly<CoinConfig>) {
8+
super(_coinConfig);
9+
}
10+
11+
protected get transactionType(): TransactionType {
12+
return TransactionType.OneStepPreApproval;
13+
}
14+
15+
/**
16+
*
17+
*/
18+
public partyId(partyId: string): this {
19+
this._partyId = partyId;
20+
return this;
21+
}
22+
}

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

Lines changed: 26 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,12 @@ import {
33
BaseKey,
44
BaseTransactionBuilder,
55
BuildTransactionError,
6-
FeeOptions,
76
PublicKey as BasePublicKey,
87
Signature,
98
TransactionType,
109
} from '@bitgo/sdk-core';
1110
import BigNumber from 'bignumber.js';
11+
import { KeyPair } from './keyPair';
1212
import { Transaction } from './transaction/transaction';
1313
import utils from './utils';
1414

@@ -42,21 +42,6 @@ export abstract class TransactionBuilder extends BaseTransactionBuilder {
4242
this._signatures.push({ publicKey, signature });
4343
}
4444

45-
/**
46-
* Sets the sender of this transaction.
47-
* This account will be responsible for paying transaction fees.
48-
*
49-
* @param {string} senderAddress the account that is sending this transaction
50-
* @returns {TransactionBuilder} This transaction builder
51-
*/
52-
sender(senderAddress: string): this {
53-
throw new Error('Method not implemented.');
54-
}
55-
56-
fee(feeOptions: FeeOptions): this {
57-
throw new Error('Method not implemented.');
58-
}
59-
6045
/** @inheritdoc */
6146
protected fromImplementation(rawTransaction: string): Transaction {
6247
throw new Error('Method not implemented.');
@@ -77,17 +62,37 @@ export abstract class TransactionBuilder extends BaseTransactionBuilder {
7762

7863
/** @inheritdoc */
7964
validateKey(key: BaseKey): void {
80-
throw new Error('Method not implemented.');
65+
let keyPair: KeyPair;
66+
try {
67+
keyPair = new KeyPair({ prv: key.key });
68+
} catch {
69+
throw new BuildTransactionError('Invalid key');
70+
}
71+
if (!keyPair.getKeys().prv) {
72+
throw new BuildTransactionError('Invalid key');
73+
}
8174
}
8275

8376
/** @inheritdoc */
84-
validateRawTransaction(rawTransaction: string): void {
85-
throw new Error('Method not implemented.');
77+
async validateRawTransaction(rawTransaction: string): Promise<void> {
78+
if (!rawTransaction || !this._transaction.transaction) {
79+
throw new BuildTransactionError('invalid raw transaction');
80+
}
81+
const localHash = await utils.computeHashFromPreparedTransaction(rawTransaction);
82+
if (localHash !== this._transaction.transaction.preparedTransactionHash) {
83+
throw new BuildTransactionError('invalid raw transaction, hash not matching');
84+
}
8685
}
8786

8887
/** @inheritdoc */
89-
validateTransaction(transaction?: Transaction): void {
90-
throw new Error('Method not implemented.');
88+
async validateTransaction(transaction?: Transaction): Promise<void> {
89+
if (!transaction?.transaction?.preparedTransaction) {
90+
return;
91+
}
92+
const localHash = await utils.computeHashFromPreparedTransaction(transaction.transaction.preparedTransaction);
93+
if (localHash !== transaction.transaction.preparedTransactionHash) {
94+
throw new BuildTransactionError('invalid transaction');
95+
}
9196
}
9297

9398
/** @inheritdoc */

modules/sdk-core/src/account-lib/baseCoin/enum.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,8 @@ export enum TransactionType {
8787
FlushERC721,
8888
// Flush ERC1155 tokens from a forwarder address to base address
8989
FlushERC1155,
90+
// Set up 1-step pre-approval for canton
91+
OneStepPreApproval,
9092

9193
// trx
9294
FREEZE,

0 commit comments

Comments
 (0)