bitcoinjs-lib/src/types.ts

38 lines
1.1 KiB
TypeScript
Raw Normal View History

2018-06-25 16:25:12 +10:00
const typeforce = require('typeforce')
2015-08-11 17:01:47 +10:00
2018-12-26 18:37:09 +09:00
const UINT31_MAX: number = Math.pow(2, 31) - 1
export function UInt31 (value: number): boolean {
2016-09-30 16:25:13 +10:00
return typeforce.UInt32(value) && value <= UINT31_MAX
2015-08-11 17:01:47 +10:00
}
2018-12-26 18:37:09 +09:00
export function BIP32Path (value: string): boolean {
return typeforce.String(value) && !!value.match(/^(m\/)?(\d+'?\/)*\d+'?$/)
}
BIP32Path.toJSON = function () { return 'BIP32 derivation path' }
2018-12-26 18:37:09 +09:00
const SATOSHI_MAX: number = 21 * 1e14
export function Satoshi (value: number): boolean {
2016-09-27 13:54:03 +10:00
return typeforce.UInt53(value) && value <= SATOSHI_MAX
}
2015-08-11 17:01:47 +10:00
// external dependent types
2018-12-26 18:37:09 +09:00
export const ECPoint = typeforce.quacksLike('Point')
2015-08-11 17:01:47 +10:00
// exposed, external API
2018-12-26 18:37:09 +09:00
export const Network = typeforce.compile({
2015-08-11 17:01:47 +10:00
messagePrefix: typeforce.oneOf(typeforce.Buffer, typeforce.String),
bip32: {
2016-09-30 16:25:13 +10:00
public: typeforce.UInt32,
private: typeforce.UInt32
2015-08-11 17:01:47 +10:00
},
2016-09-30 16:25:13 +10:00
pubKeyHash: typeforce.UInt8,
scriptHash: typeforce.UInt8,
wif: typeforce.UInt8
2015-08-11 17:01:47 +10:00
})
2018-12-26 18:37:09 +09:00
export const Buffer256bit = typeforce.BufferN(32)
export const Hash160bit = typeforce.BufferN(20)
export const Hash256bit = typeforce.BufferN(32)
export * from 'typeforce'
2018-12-28 13:18:42 +09:00
export { Number } from 'typeforce'