|
1 | 1 | import { BaseUtils, isValidEd25519PublicKey } from '@bitgo/sdk-core';
|
2 |
| -import { TopologyController } from '@canton-network/wallet-sdk'; |
| 2 | +import { RecordField } from '@canton-network/core-ledger-proto'; |
| 3 | +import { decodePreparedTransaction, TopologyController } from '@canton-network/wallet-sdk'; |
| 4 | +import { PreparedTrxParsedInfo } from './iface'; |
3 | 5 |
|
4 | 6 | export class Utils implements BaseUtils {
|
5 | 7 | /** @inheritdoc */
|
@@ -40,6 +42,82 @@ export class Utils implements BaseUtils {
|
40 | 42 | getAddressFromPublicKey(publicKey: string): string {
|
41 | 43 | return TopologyController.createFingerprintFromPublicKey(publicKey);
|
42 | 44 | }
|
| 45 | + |
| 46 | + /** |
| 47 | + * Method to parse raw canton transaction & get required data |
| 48 | + * @param {String} rawData base64 encoded string |
| 49 | + * @returns {PreparedTrxParsedInfo} |
| 50 | + */ |
| 51 | + parseRawCantonTransactionData(rawData: string): PreparedTrxParsedInfo { |
| 52 | + const decodedData = decodePreparedTransaction(rawData); |
| 53 | + let sender = ''; |
| 54 | + let receiver = ''; |
| 55 | + let amount = ''; |
| 56 | + decodedData.transaction?.nodes?.map((node) => { |
| 57 | + const versionedNode = node.versionedNode; |
| 58 | + if (!versionedNode || versionedNode.oneofKind !== 'v1') return; |
| 59 | + |
| 60 | + const v1Node = versionedNode.v1; |
| 61 | + const nodeType = v1Node.nodeType; |
| 62 | + |
| 63 | + if (nodeType.oneofKind !== 'create') return; |
| 64 | + |
| 65 | + const createNode = nodeType.create; |
| 66 | + |
| 67 | + // Check if it's the correct template |
| 68 | + const template = createNode.templateId; |
| 69 | + if (template?.entityName !== 'AmuletTransferInstruction') return; |
| 70 | + |
| 71 | + // Now parse the 'create' argument |
| 72 | + if (createNode.argument?.sum?.oneofKind !== 'record') return; |
| 73 | + const fields = createNode.argument?.sum?.record?.fields; |
| 74 | + if (!fields) return; |
| 75 | + |
| 76 | + // Find the 'transfer' field |
| 77 | + const transferField = fields.find((f) => f.label === 'transfer'); |
| 78 | + if (transferField?.value?.sum?.oneofKind !== 'record') return; |
| 79 | + const transferRecord = transferField?.value?.sum?.record?.fields; |
| 80 | + if (!transferRecord) return; |
| 81 | + |
| 82 | + const getField = (fields: RecordField[], label: string) => fields.find((f) => f.label === label)?.value?.sum; |
| 83 | + |
| 84 | + const senderData = getField(transferRecord, 'sender'); |
| 85 | + if (!senderData || senderData.oneofKind !== 'party') return; |
| 86 | + sender = senderData.party; |
| 87 | + const receiverData = getField(transferRecord, 'receiver'); |
| 88 | + if (!receiverData || receiverData.oneofKind !== 'party') return; |
| 89 | + receiver = receiverData.party; |
| 90 | + const amountData = getField(transferRecord, 'amount'); |
| 91 | + if (!amountData || amountData.oneofKind !== 'numeric') return; |
| 92 | + amount = amountData.numeric; |
| 93 | + }); |
| 94 | + if (!sender || !receiver || !amount) { |
| 95 | + throw new Error('invalid transaction data'); |
| 96 | + } |
| 97 | + return { |
| 98 | + sender, |
| 99 | + receiver, |
| 100 | + amount, |
| 101 | + }; |
| 102 | + } |
| 103 | + |
| 104 | + /** |
| 105 | + * Method to compute the prepared transaction hash offline |
| 106 | + * @param {String} preparedTransaction base64 encoded prepared transaction |
| 107 | + * @returns {Promise<string>} the computed hash |
| 108 | + */ |
| 109 | + async computeHashFromPreparedTransaction(preparedTransaction: string): Promise<string> { |
| 110 | + return await TopologyController.createTransactionHash(preparedTransaction); |
| 111 | + } |
| 112 | + |
| 113 | + /** |
| 114 | + * Method to compute the topology transactions hash offline |
| 115 | + * @param {Uint8Array<ArrayBufferLike>[]} preparedTransactions the prepared topology transactions |
| 116 | + * @returns {Promise<String>} the computed hash |
| 117 | + */ |
| 118 | + async computeHashFromTopologyTransaction(preparedTransactions: Uint8Array<ArrayBufferLike>[]): Promise<string> { |
| 119 | + return await TopologyController.computeTopologyTxHash(preparedTransactions); |
| 120 | + } |
43 | 121 | }
|
44 | 122 |
|
45 | 123 | const utils = new Utils();
|
|
0 commit comments