-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathcommon.js
69 lines (58 loc) · 1.85 KB
/
common.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
let { createHash } = require('crypto')
let stableStringify = require('json-stable-stringify')
let base58check = require('bs58check')
function hashFunc (algo) {
return (data) => createHash(algo).update(data).digest()
}
let sha256 = hashFunc('sha256')
let ripemd160 = hashFunc('ripemd160')
function addressHash (data) {
let hash = ripemd160(sha256(data))
return base58check.encode(hash)
}
let burnHandler = {
// no-op, the coins just pay out to nowhere (economically,
// this is the same as paying out to all coin holders)
onOutput () {}
}
// to make basic transactions easier to compose, we let users
// specify them in a simpler format and then expand them here
// to the full format
function normalizeTx (tx) {
// user can specify single values for to/from,
// wrap them in arrays
if (!Array.isArray(tx.from)) tx.from = [ tx.from ]
if (!Array.isArray(tx.to)) tx.to = [ tx.to ]
// if no type, infer that type is 'accounts'
// (convenience for built-in coins handlers)
for (let input of tx.from) {
if (input.type != null) continue
input.type = 'accounts'
// infer accountType (default to secp or multisig)
if (input.accountType != null) continue
input.accountType = input.pubkey ? 'secp256k1' : 'multisig'
}
// if output has address and no type, infer that type is 'accounts'
for (let output of tx.to) {
if (output.type != null) continue
if (output.address == null) continue
output.type = 'accounts'
}
// if there is only 1 input and it has no amount, set amount
// to sum of outputs
if (tx.from.length === 1 && tx.from[0].amount == null) {
let amountOut = tx.to.reduce((sum, { amount }) => sum + amount, 0)
tx.from[0].amount = amountOut
}
}
function clone (obj) {
return JSON.parse(JSON.stringify(obj))
}
module.exports = {
sha256,
ripemd160,
addressHash,
burnHandler,
normalizeTx,
clone
}