Skip to content

Commit 959fb86

Browse files
authored
Merge pull request #5936 from BitGo/WIN-5178-add-hash-function-icp
feat(sdk-coin-icp): implement getHashFunction
2 parents 0f3691c + 26a28aa commit 959fb86

File tree

1 file changed

+39
-16
lines changed
  • modules/sdk-coin-icp/src

1 file changed

+39
-16
lines changed

modules/sdk-coin-icp/src/icp.ts

Lines changed: 39 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
import BigNumber from 'bignumber.js';
2-
import * as request from 'superagent';
31
import {
42
BaseBroadcastTransactionOptions,
53
BaseBroadcastTransactionResult,
@@ -13,32 +11,35 @@ import {
1311
MPCAlgorithm,
1412
MultisigType,
1513
multisigTypes,
16-
ParseTransactionOptions,
1714
ParsedTransaction,
18-
SignTransactionOptions,
15+
ParseTransactionOptions,
1916
SignedTransaction,
17+
SigningError,
18+
SignTransactionOptions,
2019
TssVerifyAddressOptions,
2120
VerifyTransactionOptions,
2221
} from '@bitgo/sdk-core';
23-
import { BaseCoin as StaticsBaseCoin, coins } from '@bitgo/statics';
22+
import { coins, BaseCoin as StaticsBaseCoin } from '@bitgo/statics';
23+
import { Principal } from '@dfinity/principal';
24+
import axios from 'axios';
25+
import BigNumber from 'bignumber.js';
26+
import { createHash, Hash } from 'crypto';
27+
import * as request from 'superagent';
2428
import {
29+
ACCOUNT_BALANCE_ENDPOINT,
30+
CurveType,
31+
LEDGER_CANISTER_ID,
2532
Network,
2633
PayloadsData,
34+
PUBLIC_NODE_REQUEST_ENDPOINT,
35+
PublicNodeSubmitResponse,
2736
RecoveryOptions,
37+
ROOT_PATH,
2838
Signatures,
2939
SigningPayload,
30-
ACCOUNT_BALANCE_ENDPOINT,
31-
ROOT_PATH,
32-
LEDGER_CANISTER_ID,
33-
PublicNodeSubmitResponse,
34-
CurveType,
35-
PUBLIC_NODE_REQUEST_ENDPOINT,
3640
} from './lib/iface';
3741
import { TransactionBuilderFactory } from './lib/transactionBuilderFactory';
3842
import utils from './lib/utils';
39-
import { createHash } from 'crypto';
40-
import { Principal } from '@dfinity/principal';
41-
import axios from 'axios';
4243

4344
/**
4445
* Class representing the Internet Computer (ICP) coin.
@@ -107,8 +108,25 @@ export class Icp extends BaseCoin {
107108
return utils.isValidAddress(address);
108109
}
109110

110-
signTransaction(_: SignTransactionOptions): Promise<SignedTransaction> {
111-
throw new MethodNotImplementedError();
111+
async signTransaction(
112+
params: SignTransactionOptions & { txPrebuild: { txHex: string }; prv: string }
113+
): Promise<SignedTransaction> {
114+
const txHex = params?.txPrebuild?.txHex;
115+
const privateKey = params?.prv;
116+
if (!txHex) {
117+
throw new SigningError('missing required txPrebuild parameter: params.txPrebuild.txHex');
118+
}
119+
if (!privateKey) {
120+
throw new SigningError('missing required prv parameter: params.prv');
121+
}
122+
const factory = this.getBuilderFactory();
123+
const txBuilder = await factory.from(params.txPrebuild.txHex);
124+
txBuilder.sign({ key: params.prv });
125+
txBuilder.combine();
126+
const serializedTx = txBuilder.transaction.toBroadcastFormat();
127+
return {
128+
txHex: serializedTx,
129+
};
112130
}
113131

114132
isValidPub(key: string): boolean {
@@ -134,6 +152,11 @@ export class Icp extends BaseCoin {
134152
return 'ecdsa';
135153
}
136154

155+
/** @inheritDoc **/
156+
getHashFunction(): Hash {
157+
return createHash('sha256');
158+
}
159+
137160
private async getAddressFromPublicKey(hexEncodedPublicKey: string) {
138161
return utils.getAddressFromPublicKey(hexEncodedPublicKey);
139162
}

0 commit comments

Comments
 (0)