@exodus/ethereum-lib 4.1.3 → 4.2.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/LICENSE.md ADDED
File without changes
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@exodus/ethereum-lib",
3
- "version": "4.1.3",
3
+ "version": "4.2.1",
4
4
  "description": "Ethereum Library",
5
5
  "main": "src/index.js",
6
6
  "files": [
@@ -40,7 +40,9 @@
40
40
  "@exodus/solidity-contract": "^1.1.3",
41
41
  "base-x": "^3.0.2",
42
42
  "lodash": "^4.17.15",
43
+ "minimalistic-assert": "^1.0.1",
43
44
  "ms": "^2.1.1",
44
45
  "reselect": "~3.0.1"
45
- }
46
+ },
47
+ "gitHead": "419d8b4d0d4f8617b8da97a5dd9d3e328a948ae6"
46
48
  }
@@ -3,3 +3,4 @@ export { default as parseUnsignedTx } from './parse-unsigned-tx'
3
3
  export { default as signUnsignedTx } from './sign-unsigned-tx'
4
4
  export { default as createAndSignTx } from './create-and-sign-tx'
5
5
  export { default as createEthereumJsTx } from './create-ethereumjs-tx'
6
+ export { signHardwareFactory } from './sign-hardware'
@@ -0,0 +1,56 @@
1
+ import assert from 'minimalistic-assert'
2
+ import createEthereumJsTx from './create-ethereumjs-tx'
3
+
4
+ export const signHardwareFactory = ({ baseAssetName }) => async ({
5
+ unsignedTx,
6
+ hardwareDevice,
7
+ accountIndex,
8
+ }) => {
9
+ const tx = createEthereumJsTx(unsignedTx)
10
+
11
+ const signedTx = await signWithHardwareWallet({
12
+ baseAssetName,
13
+ tx,
14
+ hardwareDevice,
15
+ accountIndex,
16
+ })
17
+
18
+ // serialize and get txId
19
+ const rawTx = signedTx.serialize()
20
+ const txId = signedTx.hash().toString('hex')
21
+ return { rawTx, txId }
22
+ }
23
+
24
+ async function signWithHardwareWallet({ baseAssetName, tx, hardwareDevice, accountIndex }) {
25
+ const rlpEncodedTx = tx.serialize()
26
+ const signatures = await hardwareDevice.signTransaction({
27
+ assetName: baseAssetName,
28
+ signableTransaction: rlpEncodedTx,
29
+ derivationPaths: [`m/44'/60'/${accountIndex}'/0/0`],
30
+ })
31
+
32
+ const { signature } = signatures[0]
33
+ assert(signature.length >= 65, `signature length should be 65 or more`)
34
+ const V_SIZE = signature.length - 64
35
+ let v = Number.parseInt(signature.slice(0, V_SIZE).toString('hex'), '16')
36
+ if (v === 0 || v === 1) {
37
+ // _processSignature for EIP1559 expects 27 or 28.
38
+ // hardware wallets on the other hand already precomputed the final v (0, 1).
39
+ const diff = 27
40
+ v += diff
41
+ } else if (tx.common.chainId !== undefined) {
42
+ // _processSignature for legacy transactions expects 27 or 28.
43
+ // hardware wallets on the other hand already precomputed the final v (chainId * 2 + 35 + parity).
44
+ const diff = tx.common
45
+ .chainIdBN()
46
+ .muln(2)
47
+ .addn(8)
48
+ .toNumber()
49
+ v -= diff
50
+ }
51
+ assert([27, 28].includes(v), `unexpected v-value, expected 27 or 28 but got ${v} `)
52
+ const r = signature.slice(V_SIZE, V_SIZE + 32)
53
+ const s = signature.slice(V_SIZE + 32, V_SIZE + 32 + 32)
54
+
55
+ return tx._processSignature(v, r, s)
56
+ }
@@ -1,12 +1,8 @@
1
- /* @flow */
2
- import type { UnsignedTransaction, SignedTransaction } from '@exodus/models/lib/types'
3
1
  import createEthereumJsTx from './create-ethereumjs-tx'
4
2
 
5
- export default function signUnsignedTx(
6
- unsignedTx: UnsignedTransaction,
7
- privateKey: Buffer
8
- ): SignedTransaction {
3
+ export default function signUnsignedTx(unsignedTx, privateKey) {
9
4
  const tx = createEthereumJsTx(unsignedTx)
5
+
10
6
  const signedTx = tx.sign(privateKey)
11
7
 
12
8
  // serialize and get txId