forked from weilu/cb-wallet
-
Notifications
You must be signed in to change notification settings - Fork 0
/
validator.js
41 lines (35 loc) · 1.03 KB
/
validator.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
var assert = require('assert')
var bitcoin = require('bitcoinjs-lib')
function preCreateTx(to, value, network) {
var error
try {
var address = to instanceof bitcoin.Address ? to : bitcoin.Address.fromBase58Check(to)
assert(address.version === network.pubKeyHash || address.version === network.scriptHash,
'Invalid address version prefix')
} catch (e) {
error = new Error('Invalid address')
error.details = e.message
throw error
}
if (value <= network.dustThreshold) {
error = new Error('Invalid value')
error.details = 'Not above dust threshold'
error.dustThreshold = network.dustThreshold
throw error
}
}
function postCreateTx(needed, has, hasIncludingZeroConf) {
if (has < needed) {
var error = new Error('Insufficient funds')
error.has = has
error.needed = needed
if (hasIncludingZeroConf >= needed) {
error.details = 'Additional funds confirmation pending'
}
throw error
}
}
module.exports = {
preCreateTx: preCreateTx,
postCreateTx: postCreateTx
}