Skip to content

Commit 7e87e40

Browse files
committed
feat(sdk-coin-canton): added transaction class
Ticket: COIN-5871
1 parent ef0a892 commit 7e87e40

File tree

4 files changed

+114
-5
lines changed

4 files changed

+114
-5
lines changed
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,32 @@
1+
import { TransactionType } from '@bitgo/sdk-core';
2+
import { PartyId } from '@canton-network/core-types';
3+
14
/**
25
* The transaction data returned from the toJson() function of a transaction
36
*/
47
export interface TxData {
58
id: string;
9+
type: TransactionType;
10+
sender: string;
11+
receiver: string;
12+
}
13+
14+
export interface WalletInitializationDataTxData {
15+
id: string;
16+
type: TransactionType;
17+
}
18+
19+
export interface CantonPrepareCommandResponse {
20+
preparedTransaction?: string;
21+
preparedTransactionHash: string;
22+
hashingSchemeVersion: string;
23+
hashingDetails?: string;
24+
}
25+
26+
export interface PreparedParty {
27+
partyTransactions: Uint8Array<ArrayBufferLike>[];
28+
combinedHash: string;
29+
txHashes: Buffer<ArrayBuffer>[];
30+
namespace: string;
31+
partyId: PartyId;
632
}
Lines changed: 33 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,45 @@
1-
import { BaseKey, BaseTransaction } from '@bitgo/sdk-core';
2-
import { TxData } from './iface';
1+
import { BaseKey, BaseTransaction, InvalidTransactionError, TransactionType } from '@bitgo/sdk-core';
2+
import { BaseCoin as CoinConfig } from '@bitgo/statics';
3+
import { CantonPrepareCommandResponse, TxData } from './iface';
34

45
export class Transaction extends BaseTransaction {
6+
private _transaction: CantonPrepareCommandResponse;
7+
8+
constructor(coinConfig: Readonly<CoinConfig>) {
9+
super(coinConfig);
10+
}
11+
12+
get transaction(): CantonPrepareCommandResponse {
13+
return this._transaction;
14+
}
15+
16+
set transaction(transaction: CantonPrepareCommandResponse) {
17+
this._transaction = transaction;
18+
this._id = transaction.preparedTransactionHash;
19+
}
20+
521
canSign(key: BaseKey): boolean {
622
return false;
723
}
824

925
toBroadcastFormat(): string {
10-
throw new Error('Method not implemented.');
26+
if (!this._transaction) {
27+
throw new InvalidTransactionError('Empty transaction data');
28+
}
29+
return this._transaction.preparedTransactionHash;
1130
}
1231

1332
toJson(): TxData {
14-
throw new Error('Method not implemented.');
33+
if (!this._transaction) {
34+
throw new InvalidTransactionError('Empty transaction data');
35+
}
36+
const result: TxData = {
37+
id: this.id,
38+
type: this._type as TransactionType,
39+
sender: '',
40+
receiver: '',
41+
};
42+
// Add logic to parse the preparedTransaction & extract sender, receiver
43+
return result;
1544
}
1645
}

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

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { BaseUtils, isValidEd25519PublicKey } from '@bitgo/sdk-core';
2-
import { TopologyController } from '@canton-network/wallet-sdk';
2+
import { decodePreparedTransaction, PreparedTransaction, TopologyController } from '@canton-network/wallet-sdk';
33

44
export class Utils implements BaseUtils {
55
/** @inheritdoc */
@@ -40,6 +40,17 @@ export class Utils implements BaseUtils {
4040
getAddressFromPublicKey(publicKey: string): string {
4141
return TopologyController.createFingerprintFromPublicKey(publicKey);
4242
}
43+
44+
/**
45+
* Method to parse raw canton transaction & get required data
46+
* @param {String} rawData base64 encoded string
47+
* @returns
48+
*/
49+
parseRawCantonTransactionData(rawData: string): PreparedTransaction {
50+
const decodedData = decodePreparedTransaction(rawData);
51+
// const sender = decodedData.metadata?.submitterInfo?.actAs?.[0];
52+
return decodedData;
53+
}
4354
}
4455

4556
const utils = new Utils();
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
import { BaseKey, BaseTransaction, InvalidTransactionError, TransactionType } from '@bitgo/sdk-core';
2+
import { BaseCoin as CoinConfig } from '@bitgo/statics';
3+
import { PreparedParty, WalletInitializationDataTxData } from '../iface';
4+
5+
export class Transaction extends BaseTransaction {
6+
private _transaction: PreparedParty;
7+
8+
constructor(coinConfig: Readonly<CoinConfig>) {
9+
super(coinConfig);
10+
}
11+
12+
get transaction(): PreparedParty {
13+
return this._transaction;
14+
}
15+
16+
set transaction(transaction: PreparedParty) {
17+
this._transaction = transaction;
18+
this._id = transaction.combinedHash;
19+
}
20+
21+
canSign(key: BaseKey): boolean {
22+
return false;
23+
}
24+
25+
toBroadcastFormat(): string {
26+
if (!this._transaction) {
27+
throw new InvalidTransactionError('Empty transaction data');
28+
}
29+
return this._transaction.combinedHash;
30+
}
31+
32+
toJson(): WalletInitializationDataTxData {
33+
if (!this._transaction) {
34+
throw new InvalidTransactionError('Empty transaction data');
35+
}
36+
const result: WalletInitializationDataTxData = {
37+
id: this.id,
38+
type: this._type as TransactionType,
39+
};
40+
// Add logic to parse the preparedTransaction & extract sender, receiver
41+
return result;
42+
}
43+
}

0 commit comments

Comments
 (0)