Skip to content

Commit 54a1869

Browse files
committed
types: add Satoshi
1 parent 42c7161 commit 54a1869

File tree

2 files changed

+21
-0
lines changed

2 files changed

+21
-0
lines changed

src/types.js

+6
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,11 @@ function UInt31 (value) {
66
return typeforce.UInt32(value) && value <= UINT31_MAX
77
}
88

9+
var SATOSHI_MAX = 2.1 * 1e15
10+
function Satoshi (value) {
11+
return typeforce.UInt53(value) && value <= SATOSHI_MAX
12+
}
13+
914
function Bip32Path (value) {
1015
return typeforce.String(value) &&
1116
value.match(/^(m\/)?(\d+'?\/)*\d+'?$/)
@@ -38,6 +43,7 @@ var types = {
3843
Hash160bit: typeforce.BufferN(20),
3944
Hash256bit: typeforce.BufferN(32),
4045
Network: Network,
46+
Satoshi: Satoshi,
4147
UInt2: UInt2,
4248
UInt31: UInt31,
4349
Bip32Path: Bip32Path

test/types.js

+15
Original file line numberDiff line numberDiff line change
@@ -46,4 +46,19 @@ describe('types', function () {
4646
}, /Expected Buffer\(Length: 32\), got Buffer\(Length: 20\)/)
4747
})
4848
})
49+
50+
describe('Satoshi', function () {
51+
[
52+
{ value: -1, result: false },
53+
{ value: 0, result: true },
54+
{ value: 1, result: true },
55+
{ value: 20999999 * 1e8, result: true },
56+
{ value: 21000000 * 1e8, result: true },
57+
{ value: 21000001 * 1e8, result: false }
58+
].forEach(function (f) {
59+
it('returns ' + f.result + ' for valid for ' + f.value, function () {
60+
assert.strictEqual(types.Satoshi(f.value), f.result)
61+
})
62+
})
63+
})
4964
})

0 commit comments

Comments
 (0)