1 | var typeforce = require('typeforce')
|
2 |
|
3 | var UINT31_MAX = Math.pow(2, 31) - 1
|
4 | function UInt31 (value) {
|
5 | return typeforce.UInt32(value) && value <= UINT31_MAX
|
6 | }
|
7 |
|
8 | function BIP32Path (value) {
|
9 | return typeforce.String(value) && value.match(/^(m\/)?(\d+'?\/)*\d+'?$/)
|
10 | }
|
11 | BIP32Path.toJSON = function () { return 'BIP32 derivation path' }
|
12 |
|
13 | var SATOSHI_MAX = 2.1 * 1e15
|
14 | function Satoshi (value) {
|
15 | return typeforce.UInt53(value) && value <= SATOSHI_MAX
|
16 | }
|
17 |
|
18 |
|
19 | var BigInt = typeforce.quacksLike('BigInteger')
|
20 | var ECPoint = typeforce.quacksLike('Point')
|
21 |
|
22 |
|
23 | var ECSignature = typeforce.compile({ r: BigInt, s: BigInt })
|
24 | var Network = typeforce.compile({
|
25 | messagePrefix: typeforce.oneOf(typeforce.Buffer, typeforce.String),
|
26 | bip32: {
|
27 | public: typeforce.UInt32,
|
28 | private: typeforce.UInt32
|
29 | },
|
30 | pubKeyHash: typeforce.UInt16,
|
31 | scriptHash: typeforce.UInt16,
|
32 | wif: typeforce.UInt8,
|
33 | dustThreshold: Satoshi
|
34 | })
|
35 |
|
36 |
|
37 | var types = {
|
38 | BigInt: BigInt,
|
39 | BIP32Path: BIP32Path,
|
40 | Buffer256bit: typeforce.BufferN(32),
|
41 | ECPoint: ECPoint,
|
42 | ECSignature: ECSignature,
|
43 | Hash160bit: typeforce.BufferN(20),
|
44 | Hash256bit: typeforce.BufferN(32),
|
45 | Network: Network,
|
46 | Satoshi: Satoshi,
|
47 | UInt31: UInt31
|
48 | }
|
49 |
|
50 | for (var typeName in typeforce) {
|
51 | types[typeName] = typeforce[typeName]
|
52 | }
|
53 |
|
54 | module.exports = types
|