bitcoinjs-lib/src/types.js

54 lines
1.3 KiB
JavaScript
Raw Normal View History

2015-08-11 09:01:47 +02:00
var typeforce = require('typeforce')
var UINT31_MAX = Math.pow(2, 31) - 1
function UInt31 (value) {
2016-09-30 08:25:13 +02:00
return typeforce.UInt32(value) && value <= UINT31_MAX
2015-08-11 09:01:47 +02:00
}
function BIP32Path (value) {
return typeforce.String(value) && value.match(/^(m\/)?(\d+'?\/)*\d+'?$/)
}
BIP32Path.toJSON = function () { return 'BIP32 derivation path' }
2016-09-27 05:54:03 +02:00
var SATOSHI_MAX = 2.1 * 1e15
function Satoshi (value) {
return typeforce.UInt53(value) && value <= SATOSHI_MAX
}
2015-08-11 09:01:47 +02:00
// external dependent types
2015-08-25 05:47:11 +02:00
var BigInt = typeforce.quacksLike('BigInteger')
var ECPoint = typeforce.quacksLike('Point')
2015-08-11 09:01:47 +02:00
// exposed, external API
var ECSignature = typeforce.compile({ r: BigInt, s: BigInt })
var Network = typeforce.compile({
messagePrefix: typeforce.oneOf(typeforce.Buffer, typeforce.String),
bip32: {
2016-09-30 08:25:13 +02:00
public: typeforce.UInt32,
private: typeforce.UInt32
2015-08-11 09:01:47 +02:00
},
2016-09-30 08:25:13 +02:00
pubKeyHash: typeforce.UInt8,
scriptHash: typeforce.UInt8,
wif: typeforce.UInt8
2015-08-11 09:01:47 +02:00
})
// extend typeforce types with ours
var types = {
BigInt: BigInt,
BIP32Path: BIP32Path,
2016-09-30 08:25:13 +02:00
Buffer256bit: typeforce.BufferN(32),
2015-08-11 09:01:47 +02:00
ECPoint: ECPoint,
ECSignature: ECSignature,
2016-09-30 08:25:13 +02:00
Hash160bit: typeforce.BufferN(20),
Hash256bit: typeforce.BufferN(32),
2015-08-11 09:01:47 +02:00
Network: Network,
2016-09-27 05:54:03 +02:00
Satoshi: Satoshi,
UInt31: UInt31
2015-08-11 09:01:47 +02:00
}
for (var typeName in typeforce) {
types[typeName] = typeforce[typeName]
}
module.exports = types