|
| 1 | +import assert from 'assert'; |
1 | 2 | import * as ethUtil from 'ethereumjs-util'; |
2 | 3 | import EthereumAbi from 'ethereumjs-abi'; |
3 | 4 | import BN from 'bn.js'; |
4 | 5 | import { coins, BaseCoin, ContractAddressDefinedToken, EthereumNetwork as EthLikeNetwork } from '@bitgo/statics'; |
5 | 6 | import { BuildTransactionError, InvalidParameterValueError } from '@bitgo/sdk-core'; |
6 | 7 | import { decodeTransferData, sendMultiSigData, sendMultiSigTokenData, isValidEthAddress, isValidAmount } from './utils'; |
7 | 8 | import { defaultAbiCoder, keccak256 } from 'ethers/lib/utils'; |
| 9 | +import { sendMultiSigTokenTypes, sendMultiSigTypes } from './walletUtil'; |
8 | 10 |
|
9 | 11 | /** ETH transfer builder */ |
10 | 12 | export class TransferBuilder { |
11 | 13 | private readonly _EMPTY_HEX_VALUE = '0x'; |
12 | 14 | protected _amount: string; |
13 | 15 | protected _toAddress: string; |
14 | 16 | protected _sequenceId: number; |
15 | | - protected _signKey: string; |
| 17 | + protected _signKey: string | null; |
16 | 18 | protected _expirationTime: number; |
17 | 19 | protected _signature: string; |
18 | 20 | private _data: string; |
@@ -111,6 +113,12 @@ export class TransferBuilder { |
111 | 113 | return this; |
112 | 114 | } |
113 | 115 |
|
| 116 | + setSignature(signature: string): TransferBuilder { |
| 117 | + this._signKey = null; |
| 118 | + this._signature = signature; |
| 119 | + return this; |
| 120 | + } |
| 121 | + |
114 | 122 | signAndBuild(chainId: string, coinUsesNonPackedEncodingForTxData?: boolean): string { |
115 | 123 | this._chainId = chainId; |
116 | 124 |
|
@@ -254,6 +262,7 @@ export class TransferBuilder { |
254 | 262 |
|
255 | 263 | protected ethSignMsgHash(): string { |
256 | 264 | const data = this.getOperationHash(); |
| 265 | + assert(this._signKey); |
257 | 266 | const keyBuffer = Buffer.from(ethUtil.padToEven(this._signKey), 'hex'); |
258 | 267 | if (keyBuffer.length !== 32) { |
259 | 268 | throw new Error('private key length is invalid'); |
@@ -289,4 +298,17 @@ export class TransferBuilder { |
289 | 298 | this._tokenContractAddress = transferData.tokenContractAddress; |
290 | 299 | } |
291 | 300 | } |
| 301 | + |
| 302 | + public getSignatureData(): Buffer<ArrayBuffer> { |
| 303 | + const method = this._tokenContractAddress |
| 304 | + ? EthereumAbi.methodID('sendMultiSigToken', sendMultiSigTokenTypes) |
| 305 | + : EthereumAbi.methodID('sendMultiSig', sendMultiSigTypes); |
| 306 | + const operationData = this.getOperationData(); |
| 307 | + const rawEncodedOperationData = EthereumAbi.rawEncode(...operationData); |
| 308 | + return Buffer.concat([ |
| 309 | + method, |
| 310 | + rawEncodedOperationData, |
| 311 | + Buffer.from([this._coinUsesNonPackedEncodingForTxData ? 1 : 0]), |
| 312 | + ]); |
| 313 | + } |
292 | 314 | } |
0 commit comments