@exodus/ethereum-lib 0.1.0 → 0.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.
@@ -0,0 +1,19 @@
1
+ /* @flow */
2
+ import EthereumTx from 'ethereumjs-tx'
3
+
4
+ import type { UnsignedTransaction, SignedTransaction } from '@exodus/models/lib/types'
5
+
6
+ export default function signUnsignedTx(
7
+ unsignedTx: UnsignedTransaction,
8
+ privateKey: Buffer
9
+ ): SignedTransaction {
10
+ const { txData } = unsignedTx
11
+
12
+ const tx = new EthereumTx(txData)
13
+ tx.sign(privateKey)
14
+
15
+ // serialize and get txId
16
+ const rawTx = tx.serialize()
17
+ const txId = tx.hash().toString('hex')
18
+ return { rawTx, txId }
19
+ }
package/src/utils.js ADDED
@@ -0,0 +1,28 @@
1
+ import baseX from 'base-x'
2
+ import * as ethUtil from 'ethereumjs-util'
3
+
4
+ const base10 = baseX('0123456789')
5
+ const base16 = baseX('0123456789abcdef')
6
+
7
+ export const _isEthereumToken = (asset) => asset.assetType === 'ETHEREUM_ERC20'
8
+
9
+ export function buffer2currency({ asset, value }) {
10
+ return asset.currency.baseUnit(base10.encode(value)).toDefault()
11
+ }
12
+
13
+ export function currency2buffer(value) {
14
+ const b10Value = value
15
+ .toBase()
16
+ .floor() // probably not necessary, but to be safe
17
+ .toString({ unit: false })
18
+
19
+ // Desktop: const hexValue = value.toBase()._number.toString(16)
20
+ const hexValue = base16.encode(base10.decode(b10Value))
21
+
22
+ if (hexValue.includes('.')) {
23
+ // <-- probably not necessary anymore
24
+ throw new RangeError(`${value.toBase().toString()} can not be converted to Buffer`)
25
+ }
26
+
27
+ return Buffer.from(ethUtil.padToEven(hexValue), 'hex')
28
+ }