Browse Source

types: add Satoshi

hk-custom-address
Daniel Cousens 8 years ago
committed by Daniel Cousens
parent
commit
ecc6d45a7e
  1. 6
      src/types.js
  2. 15
      test/types.js

6
src/types.js

@ -6,6 +6,11 @@ function UInt31 (value) {
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) {
return typeforce.String(value) &&
value.match(/^(m\/)?(\d+'?\/)*\d+'?$/)
@ -38,6 +43,7 @@ var types = {
Hash160bit: typeforce.BufferN(20),
Hash256bit: typeforce.BufferN(32),
Network: Network,
Satoshi: Satoshi,
UInt2: UInt2,
UInt31: UInt31,
Bip32Path: Bip32Path

15
test/types.js

@ -46,4 +46,19 @@ describe('types', function () {
}, /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)
})
})
})
})

Loading…
Cancel
Save