diff --git a/src/types.js b/src/types.js index e41207f..007d3b2 100644 --- a/src/types.js +++ b/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 diff --git a/test/types.js b/test/types.js index bd02377..df0550a 100644 --- a/test/types.js +++ b/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) + }) + }) + }) })