Repository navigation
WIN-4641-1 | Transaction builder for ICP #5570
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
Closed
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
ea4cc9f
feat(sdk-coin-icp): implemented transaction builder and validations f…
mohd-kashif 67e8d6f
feat(sdk-coin-icp): enhance transaction handling and add unit tests f…
mohd-kashif 6bc9f6c
feat(sdk-coin-icp): refactor transaction and transfer builders; add i…
mohd-kashif 2cf1add
refactor(sdk-coin-icp): remove TransferTransaction class, update enum…
mohd-kashif b663d2a
feat(sdk-coin-icp): enhance transaction and transfer builders; implem…
mohd-kashif 1808876
fix(sdk-coin-icp): standardize ingress expiry naming and improve tran…
mohd-kashif f7a3101
test(sdk-coin-icp): add unit test for building signed transaction and…
mohd-kashif bd3809b
test(sdk-coin-icp): improve unit tests by stubbing transaction signat…
mohd-kashif dec98b9
feat(sdk-coin-icp): Merge branch 'master' into WIN-4641-1
mohd-kashif e6434a3
fix(sdk-coin-icp): standardize ingress expiry naming and improve tran…
mohd-kashif File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,185 @@ | ||
| import { | ||
| TransactionExplanation as BaseTransactionExplanation, | ||
| TransactionType as BitGoTransactionType, | ||
| } from '@bitgo/sdk-core'; | ||
|
|
||
| export const REQUEST_STATUS = 'request_status'; | ||
|
|
||
| export enum RequestType { | ||
| CALL = 'call', | ||
| READ_STATE = 'read_state', | ||
| } | ||
|
|
||
| export enum SignatureType { | ||
| ECDSA = 'ecdsa', | ||
| } | ||
|
|
||
| export enum CurveType { | ||
| SECP256K1 = 'secp256k1', | ||
| } | ||
|
|
||
| export enum OperationType { | ||
| TRANSACTION = 'TRANSACTION', | ||
| FEE = 'FEE', | ||
| } | ||
|
|
||
| export enum MethodName { | ||
| SEND_PB = 'send_pb', // send_pb is the method name for ICP transfer transaction | ||
| } | ||
|
|
||
| export enum Network { | ||
| ID = '00000000000000020101', // ICP does not have different network IDs for mainnet and testnet | ||
| } | ||
|
|
||
| export interface IcpTransactionData { | ||
| senderAddress: string; | ||
| receiverAddress: string; | ||
| amount: string; | ||
| fee: string; | ||
| senderPublicKeyHex: string; | ||
| memo: number | BigInt; // memo in string is not accepted by ICP chain. | ||
| transactionType: OperationType; | ||
| expiryTime: number | BigInt; | ||
| } | ||
|
|
||
| export interface IcpPublicKey { | ||
| hex_bytes: string; | ||
| curve_type: string; | ||
| } | ||
|
|
||
| export interface IcpAccount { | ||
| address: string; | ||
| } | ||
|
|
||
| export interface IcpCurrency { | ||
| symbol: string; | ||
| decimals: number; | ||
| } | ||
|
|
||
| export interface IcpAmount { | ||
| value: string; | ||
| currency: IcpCurrency; | ||
| } | ||
|
|
||
| export interface IcpOperation { | ||
| type: string; | ||
| account: IcpAccount; | ||
| amount: IcpAmount; | ||
| } | ||
|
|
||
| export interface IcpMetadata { | ||
| created_at_time: number; | ||
| memo: number | BigInt; // memo in string is not accepted by ICP chain. | ||
| ingress_start: number | BigInt; // it should be nano seconds | ||
| ingress_end: number | BigInt; // it should be nano seconds | ||
| } | ||
|
|
||
| export interface IcpTransaction { | ||
| public_keys: IcpPublicKey[]; | ||
| operations: IcpOperation[]; | ||
| metadata: IcpMetadata; | ||
| } | ||
|
|
||
| export interface IcpAccountIdentifier { | ||
| address: string; | ||
| } | ||
|
|
||
| export interface SendArgs { | ||
| memo: { memo: number | BigInt }; | ||
| payment: { receiverGets: { e8s: number } }; | ||
| maxFee: { e8s: number }; | ||
| to: { hash: Buffer }; | ||
| createdAtTime: { timestampNanos: number }; | ||
| } | ||
|
|
||
| export interface HttpCanisterUpdate { | ||
| canister_id: Uint8Array; | ||
| method_name: MethodName; | ||
| arg: Uint8Array; | ||
| sender: Uint8Array; | ||
| ingress_expiry: bigint; | ||
| } | ||
|
DinshawKothari marked this conversation as resolved.
Resolved
|
||
|
|
||
| export interface SigningPayload { | ||
| account_identifier: IcpAccountIdentifier; | ||
| hex_bytes: string; | ||
| signature_type: SignatureType; | ||
| } | ||
|
DinshawKothari marked this conversation as resolved.
Resolved
|
||
|
|
||
| export interface PayloadsData { | ||
| payloads: SigningPayload[]; | ||
| unsigned_transaction: string; | ||
| } | ||
|
|
||
| export interface Signatures { | ||
| signing_payload: SigningPayload; | ||
| signature_type: SignatureType; | ||
| public_key: IcpPublicKey; | ||
| hex_bytes: string; | ||
| } | ||
|
|
||
| export interface cborUnsignedTransaction { | ||
| updates: [string, HttpCanisterUpdate][]; | ||
| ingress_expiries: bigint[]; | ||
| } | ||
|
|
||
| export interface ReadState { | ||
| sender: Uint8Array; | ||
| paths: Array<[Buffer, Buffer]>; | ||
| ingress_expiry: bigint; | ||
| } | ||
|
|
||
| export interface UpdateEnvelope { | ||
| content: { | ||
| request_type: RequestType; | ||
| canister_id: Uint8Array; | ||
| method_name: MethodName; | ||
| arg: Uint8Array; | ||
| sender: Uint8Array; | ||
| ingress_expiry: bigint; | ||
| }; | ||
| sender_pubkey: Uint8Array; | ||
| sender_sig: Uint8Array; | ||
| } | ||
|
|
||
| export interface ReadStateEnvelope { | ||
| content: ReadState & { | ||
| request_type: RequestType; | ||
| }; | ||
| sender_pubkey: Uint8Array; | ||
| sender_sig: Uint8Array; | ||
| } | ||
|
|
||
| export interface RequestEnvelope { | ||
| update: UpdateEnvelope; | ||
| read_state: ReadStateEnvelope; | ||
| } | ||
|
|
||
| /** | ||
| * The transaction data returned from the toJson() function of a transaction | ||
| */ | ||
| export interface TxData { | ||
| id?: string; | ||
| sender: string; | ||
| senderPublicKey: string; | ||
| recipient: string; | ||
| memo: number | BigInt; | ||
| feeAmount: string; | ||
| expirationTime: number | BigInt; | ||
| type?: BitGoTransactionType; | ||
| } | ||
|
|
||
| export interface IcpTransactionExplanation extends BaseTransactionExplanation { | ||
| sender?: string; | ||
| type?: BitGoTransactionType; | ||
| } | ||
|
|
||
| export interface NetworkIdentifier { | ||
| blockchain: string; | ||
| network: string; | ||
| } | ||
|
|
||
| export interface SignedTransactionRequest { | ||
| network_identifier: NetworkIdentifier; | ||
| signed_transaction: string; | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,5 +1,8 @@ | ||
| import * as Utils from './utils'; | ||
|
|
||
| export { KeyPair } from './keyPair'; | ||
| export { TransactionBuilder } from './transactionBuilder'; | ||
| export { TransferBuilder } from './transferBuilder'; | ||
| export { TransactionBuilderFactory } from './transactionBuilderFactory'; | ||
| export { Transaction } from './transaction'; | ||
| export { Utils }; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,39 @@ | ||
| syntax = "proto3"; | ||
|
|
||
| message Memo { | ||
| uint64 memo = 1; | ||
| } | ||
|
|
||
| message Tokens { | ||
| uint64 e8s = 1; | ||
| } | ||
|
|
||
| message Payment { | ||
| Tokens receiver_gets = 1; | ||
| } | ||
|
|
||
| message Subaccount { | ||
| bytes sub_account = 1; | ||
| } | ||
|
|
||
| message AccountIdentifier { | ||
| bytes hash = 1; | ||
| } | ||
|
|
||
| message BlockIndex { | ||
| uint64 height = 1; | ||
| } | ||
|
|
||
| message TimeStamp { | ||
| uint64 timestamp_nanos = 1; | ||
| } | ||
|
|
||
| message SendRequest { | ||
| Memo memo = 1; | ||
| Payment payment = 2; | ||
| Tokens max_fee = 3; | ||
| Subaccount from_subaccount = 4; | ||
| AccountIdentifier to = 5; | ||
| BlockIndex created_at = 6; | ||
| TimeStamp created_at_time = 7; | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,84 @@ | ||
| import { | ||
| cborUnsignedTransaction, | ||
| RequestType, | ||
| Signatures, | ||
| UpdateEnvelope, | ||
| ReadStateEnvelope, | ||
| RequestEnvelope, | ||
| } from './iface'; | ||
| import utils from './utils'; | ||
| import assert from 'assert'; | ||
|
|
||
| export class SignedTransactionBuilder { | ||
| protected _unsigned_transaction: string; | ||
| protected _signaturePayload: Signatures[]; | ||
|
|
||
| constructor(unsigned_transaction: string, signatures: Signatures[]) { | ||
| this._unsigned_transaction = unsigned_transaction; | ||
| this._signaturePayload = signatures; | ||
| } | ||
|
|
||
| getSignTransaction(): string { | ||
| const combineRequest = { | ||
| signatures: this._signaturePayload, | ||
| unsigned_transaction: this._unsigned_transaction, | ||
| }; | ||
| const signatureMap = new Map(); | ||
| for (const sig of combineRequest.signatures) { | ||
| signatureMap.set(sig.signing_payload.hex_bytes, sig); | ||
| } | ||
| const unsignedTransaction = utils.cborDecode( | ||
| utils.blobFromHex(combineRequest.unsigned_transaction) | ||
| ) as cborUnsignedTransaction; | ||
| assert(combineRequest.signatures.length === unsignedTransaction.ingress_expiries.length * 2); | ||
| assert(unsignedTransaction.updates.length === 1); | ||
| const envelopes = this.getEnvelopes(unsignedTransaction, signatureMap); | ||
| const envelopRequests = { requests: envelopes }; | ||
| const signedTransaction = utils.cborEncode(envelopRequests); | ||
| return signedTransaction; | ||
| } | ||
|
|
||
| getEnvelopes( | ||
| unsignedTransaction: cborUnsignedTransaction, | ||
| signatureMap: Map<string, Signatures> | ||
| ): [string, RequestEnvelope[]][] { | ||
| const envelopes: [string, RequestEnvelope[]][] = []; | ||
| for (const [reqType, update] of unsignedTransaction.updates) { | ||
| const requestEnvelopes: RequestEnvelope[] = []; | ||
| for (const ingressExpiry of unsignedTransaction.ingress_expiries) { | ||
| update.ingress_expiry = ingressExpiry; | ||
|
|
||
| const readState = utils.makeReadStateFromUpdate(update); | ||
| const transactionSignature = utils.getTransactionSignature(signatureMap, update); | ||
| if (!transactionSignature) { | ||
| throw new Error('Transaction signature is undefined'); | ||
| } | ||
|
|
||
| const readStateSignature = utils.getReadStateSignature(signatureMap, readState); | ||
| if (!readStateSignature) { | ||
| throw new Error('read state signature is undefined'); | ||
| } | ||
|
|
||
| const pk_der = utils.getPublicKeyInDERFormat(transactionSignature.public_key.hex_bytes); | ||
| const updateEnvelope: UpdateEnvelope = { | ||
| content: { request_type: RequestType.CALL, ...update }, | ||
| sender_pubkey: pk_der, | ||
| sender_sig: utils.blobFromHex(transactionSignature.hex_bytes), | ||
| }; | ||
|
|
||
| const readStateEnvelope: ReadStateEnvelope = { | ||
| content: { request_type: RequestType.READ_STATE, ...readState }, | ||
| sender_pubkey: pk_der, | ||
| sender_sig: utils.blobFromHex(readStateSignature.hex_bytes), | ||
| }; | ||
|
|
||
| requestEnvelopes.push({ | ||
| update: updateEnvelope, | ||
| read_state: readStateEnvelope, | ||
| }); | ||
| } | ||
| envelopes.push([reqType, requestEnvelopes]); | ||
| } | ||
| return envelopes; | ||
| } | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.