types: add Satoshi

This commit is contained in:
Daniel Cousens 2016-09-27 13:54:03 +10:00 committed by Daniel Cousens
parent f4ebe20687
commit ecc6d45a7e
2 changed files with 21 additions and 0 deletions

View file

@ -6,6 +6,11 @@ function UInt31 (value) {
return typeforce.UInt32(value) && value <= UINT31_MAX return typeforce.UInt32(value) && value <= UINT31_MAX
} }
var SATOSHI_MAX = 2.1 * 1e15
function Satoshi (value) {
return typeforce.UInt53(value) && value <= SATOSHI_MAX
}
function Bip32Path (value) { function Bip32Path (value) {
return typeforce.String(value) && return typeforce.String(value) &&
value.match(/^(m\/)?(\d+'?\/)*\d+'?$/) value.match(/^(m\/)?(\d+'?\/)*\d+'?$/)
@ -38,6 +43,7 @@ var types = {
Hash160bit: typeforce.BufferN(20), Hash160bit: typeforce.BufferN(20),
Hash256bit: typeforce.BufferN(32), Hash256bit: typeforce.BufferN(32),
Network: Network, Network: Network,
Satoshi: Satoshi,
UInt2: UInt2, UInt2: UInt2,
UInt31: UInt31, UInt31: UInt31,
Bip32Path: Bip32Path Bip32Path: Bip32Path

View file

@ -46,4 +46,19 @@ describe('types', function () {
}, /Expected Buffer\(Length: 32\), got Buffer\(Length: 20\)/) }, /Expected Buffer\(Length: 32\), got Buffer\(Length: 20\)/)
}) })
}) })
describe('Satoshi', function () {
[
{ value: -1, result: false },
{ value: 0, result: true },
{ value: 1, result: true },
{ value: 20999999 * 1e8, result: true },
{ value: 21000000 * 1e8, result: true },
{ value: 21000001 * 1e8, result: false }
].forEach(function (f) {
it('returns ' + f.result + ' for valid for ' + f.value, function () {
assert.strictEqual(types.Satoshi(f.value), f.result)
})
})
})
}) })