UNPKG

1.37 kBJavaScriptView Raw
1var typeforce = require('typeforce')
2
3var UINT31_MAX = Math.pow(2, 31) - 1
4function UInt31 (value) {
5 return typeforce.UInt32(value) && value <= UINT31_MAX
6}
7
8function BIP32Path (value) {
9 return typeforce.String(value) && value.match(/^(m\/)?(\d+'?\/)*\d+'?$/)
10}
11BIP32Path.toJSON = function () { return 'BIP32 derivation path' }
12
13var SATOSHI_MAX = 2.1 * 1e15
14function Satoshi (value) {
15 return typeforce.UInt53(value) && value <= SATOSHI_MAX
16}
17
18// external dependent types
19var BigInt = typeforce.quacksLike('BigInteger')
20var ECPoint = typeforce.quacksLike('Point')
21
22// exposed, external API
23var ECSignature = typeforce.compile({ r: BigInt, s: BigInt })
24var 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// extend typeforce types with ours
37var 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
50for (var typeName in typeforce) {
51 types[typeName] = typeforce[typeName]
52}
53
54module.exports = types