From ecc6d45a7e61838ca8214108a55141f144c9d971 Mon Sep 17 00:00:00 2001 From: Daniel Cousens Date: Tue, 27 Sep 2016 13:54:03 +1000 Subject: [PATCH] types: add Satoshi --- src/types.js | 6 ++++++ test/types.js | 15 +++++++++++++++ 2 files changed, 21 insertions(+) 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) + }) + }) + }) })