2015-08-11 17:01:47 +10:00
|
|
|
var typeforce = require('typeforce')
|
|
|
|
|
|
|
|
function nBuffer (value, n) {
|
2015-08-19 15:27:06 +10:00
|
|
|
typeforce(types.Buffer, value)
|
2016-02-02 14:04:37 +01:00
|
|
|
if (value.length !== n) throw new typeforce.TfTypeError('Expected ' + (n * 8) + '-bit Buffer, got ' + (value.length * 8) + '-bit Buffer')
|
|
|
|
|
2015-08-11 17:01:47 +10:00
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
function Hash160bit (value) { return nBuffer(value, 20) }
|
|
|
|
function Hash256bit (value) { return nBuffer(value, 32) }
|
|
|
|
function Buffer256bit (value) { return nBuffer(value, 32) }
|
|
|
|
|
|
|
|
var UINT53_MAX = Math.pow(2, 53) - 1
|
2016-02-06 17:12:13 +01:00
|
|
|
var UINT31_MAX = Math.pow(2, 31) - 1
|
2015-08-11 17:01:47 +10:00
|
|
|
function UInt2 (value) { return (value & 3) === value }
|
|
|
|
function UInt8 (value) { return (value & 0xff) === value }
|
|
|
|
function UInt32 (value) { return (value >>> 0) === value }
|
2016-02-06 17:12:13 +01:00
|
|
|
function UInt31 (value) {
|
|
|
|
return UInt32(value) && value <= UINT31_MAX
|
|
|
|
}
|
2015-08-11 17:01:47 +10:00
|
|
|
function UInt53 (value) {
|
|
|
|
return typeforce.Number(value) &&
|
|
|
|
value >= 0 &&
|
|
|
|
value <= UINT53_MAX &&
|
|
|
|
Math.floor(value) === value
|
|
|
|
}
|
|
|
|
|
2016-02-14 16:12:16 +01:00
|
|
|
function Bip32Path (value) {
|
2016-02-13 23:25:30 +01:00
|
|
|
return typeforce.String(value) &&
|
2016-02-14 16:25:55 +01:00
|
|
|
value.match(/^(m\/)?(\d+'?\/)*\d+'?$/)
|
2016-02-13 23:25:30 +01:00
|
|
|
}
|
|
|
|
|
2015-08-11 17:01:47 +10:00
|
|
|
// external dependent types
|
2015-08-25 13:47:11 +10:00
|
|
|
var BigInt = typeforce.quacksLike('BigInteger')
|
|
|
|
var ECPoint = typeforce.quacksLike('Point')
|
2015-08-11 17:01:47 +10:00
|
|
|
|
|
|
|
// exposed, external API
|
|
|
|
var ECSignature = typeforce.compile({ r: BigInt, s: BigInt })
|
|
|
|
var Network = typeforce.compile({
|
|
|
|
messagePrefix: typeforce.oneOf(typeforce.Buffer, typeforce.String),
|
|
|
|
bip32: {
|
|
|
|
public: UInt32,
|
|
|
|
private: UInt32
|
|
|
|
},
|
|
|
|
pubKeyHash: UInt8,
|
|
|
|
scriptHash: UInt8,
|
|
|
|
wif: UInt8,
|
2015-08-12 13:09:53 +10:00
|
|
|
dustThreshold: UInt53
|
2015-08-11 17:01:47 +10:00
|
|
|
})
|
|
|
|
|
|
|
|
// extend typeforce types with ours
|
|
|
|
var types = {
|
|
|
|
BigInt: BigInt,
|
|
|
|
Buffer256bit: Buffer256bit,
|
|
|
|
ECPoint: ECPoint,
|
|
|
|
ECSignature: ECSignature,
|
|
|
|
Hash160bit: Hash160bit,
|
|
|
|
Hash256bit: Hash256bit,
|
|
|
|
Network: Network,
|
|
|
|
UInt2: UInt2,
|
|
|
|
UInt8: UInt8,
|
2016-02-06 17:12:13 +01:00
|
|
|
UInt31: UInt31,
|
2015-08-11 17:01:47 +10:00
|
|
|
UInt32: UInt32,
|
2016-02-13 23:25:30 +01:00
|
|
|
UInt53: UInt53,
|
2016-02-14 16:12:16 +01:00
|
|
|
Bip32Path: Bip32Path
|
2015-08-11 17:01:47 +10:00
|
|
|
}
|
|
|
|
|
|
|
|
for (var typeName in typeforce) {
|
|
|
|
types[typeName] = typeforce[typeName]
|
|
|
|
}
|
|
|
|
|
|
|
|
module.exports = types
|