11 changed files with 151 additions and 85 deletions
@ -0,0 +1,71 @@ |
|||||
|
var bigi = require('bigi') |
||||
|
var ecurve = require('ecurve') |
||||
|
var typeforce = require('typeforce') |
||||
|
|
||||
|
function nBuffer (value, n) { |
||||
|
if (!Buffer.isBuffer(value)) return false |
||||
|
if (value.length !== n) throw new Error('Expected ' + (n * 8) + '-bit Buffer, got ' + (value.length * 8) + '-bit') |
||||
|
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 |
||||
|
function UInt2 (value) { return (value & 3) === value } |
||||
|
function UInt8 (value) { return (value & 0xff) === value } |
||||
|
function UInt32 (value) { return (value >>> 0) === value } |
||||
|
function UInt53 (value) { |
||||
|
return typeforce.Number(value) && |
||||
|
value >= 0 && |
||||
|
value <= UINT53_MAX && |
||||
|
Math.floor(value) === value |
||||
|
} |
||||
|
|
||||
|
// external dependent types
|
||||
|
function BigInt (value) { return !typeforce.Null(value) && value.constructor === bigi } |
||||
|
function ECCurve (value) { return !typeforce.Null(value) && value.constructor === ecurve.Curve } |
||||
|
function ECPoint (value) { return !typeforce.Null(value) && value.constructor === ecurve.Point } |
||||
|
|
||||
|
// 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, |
||||
|
dustThreshold: typeforce.Number |
||||
|
}) |
||||
|
|
||||
|
var Script = typeforce.compile({ |
||||
|
buffer: typeforce.Buffer, |
||||
|
chunks: typeforce.arrayOf(typeforce.oneOf(typeforce.Number, typeforce.Buffer)) |
||||
|
}) |
||||
|
|
||||
|
// extend typeforce types with ours
|
||||
|
var types = { |
||||
|
BigInt: BigInt, |
||||
|
Buffer256bit: Buffer256bit, |
||||
|
ECCurve: ECCurve, |
||||
|
ECPoint: ECPoint, |
||||
|
ECSignature: ECSignature, |
||||
|
Hash160bit: Hash160bit, |
||||
|
Hash256bit: Hash256bit, |
||||
|
Network: Network, |
||||
|
Script: Script, |
||||
|
UInt2: UInt2, |
||||
|
UInt8: UInt8, |
||||
|
UInt32: UInt32, |
||||
|
UInt53: UInt53 |
||||
|
} |
||||
|
|
||||
|
for (var typeName in typeforce) { |
||||
|
types[typeName] = typeforce[typeName] |
||||
|
} |
||||
|
|
||||
|
module.exports = types |
Loading…
Reference in new issue