bitcoinjs-lib/ts_src/types.ts

85 lines
2.6 KiB
TypeScript
Raw Normal View History

import { Buffer as NBuffer } from 'buffer';
export const typeforce = require('typeforce');
const ZERO32 = NBuffer.alloc(32, 0);
const EC_P = NBuffer.from(
'fffffffffffffffffffffffffffffffffffffffffffffffffffffffefffffc2f',
'hex',
);
export function isPoint(p: Buffer | number | undefined | null): boolean {
if (!NBuffer.isBuffer(p)) return false;
if (p.length < 33) return false;
const t = p[0];
const x = p.slice(1, 33);
if (x.compare(ZERO32) === 0) return false;
if (x.compare(EC_P) >= 0) return false;
if ((t === 0x02 || t === 0x03) && p.length === 33) {
return true;
}
const y = p.slice(33);
if (y.compare(ZERO32) === 0) return false;
if (y.compare(EC_P) >= 0) return false;
if (t === 0x04 && p.length === 65) return true;
return false;
}
2015-08-11 09:01:47 +02:00
2019-03-03 15:07:49 +01:00
const UINT31_MAX: number = Math.pow(2, 31) - 1;
export function UInt31(value: number): boolean {
return typeforce.UInt32(value) && value <= UINT31_MAX;
2015-08-11 09:01:47 +02:00
}
2019-03-03 15:07:49 +01:00
export function BIP32Path(value: string): boolean {
return typeforce.String(value) && !!value.match(/^(m\/)?(\d+'?\/)*\d+'?$/);
}
BIP32Path.toJSON = (): string => {
2019-03-03 15:07:49 +01:00
return 'BIP32 derivation path';
};
2019-06-13 06:32:21 +02:00
export function Signer(obj: any): boolean {
return (
(typeforce.Buffer(obj.publicKey) ||
typeof obj.getPublicKey === 'function') &&
typeof obj.sign === 'function'
);
}
2019-03-03 15:07:49 +01:00
const SATOSHI_MAX: number = 21 * 1e14;
export function Satoshi(value: number): boolean {
return typeforce.UInt53(value) && value <= SATOSHI_MAX;
2016-09-27 05:54:03 +02:00
}
2015-08-11 09:01:47 +02:00
// external dependent types
2019-03-03 15:07:49 +01:00
export const ECPoint = typeforce.quacksLike('Point');
2015-08-11 09:01:47 +02:00
// exposed, external API
2018-12-26 10:37:09 +01:00
export const Network = typeforce.compile({
2015-08-11 09:01:47 +02:00
messagePrefix: typeforce.oneOf(typeforce.Buffer, typeforce.String),
bip32: {
2016-09-30 08:25:13 +02:00
public: typeforce.UInt32,
2019-03-03 15:07:49 +01:00
private: typeforce.UInt32,
2015-08-11 09:01:47 +02:00
},
2016-09-30 08:25:13 +02:00
pubKeyHash: typeforce.UInt8,
scriptHash: typeforce.UInt8,
2019-03-03 15:07:49 +01:00
wif: typeforce.UInt8,
});
2015-08-11 09:01:47 +02:00
2019-03-03 15:07:49 +01:00
export const Buffer256bit = typeforce.BufferN(32);
export const Hash160bit = typeforce.BufferN(20);
export const Hash256bit = typeforce.BufferN(32);
2019-03-07 06:21:11 +01:00
export const Number = typeforce.Number; // tslint:disable-line variable-name
2019-03-03 15:07:49 +01:00
export const Array = typeforce.Array;
2019-03-07 06:21:11 +01:00
export const Boolean = typeforce.Boolean; // tslint:disable-line variable-name
export const String = typeforce.String; // tslint:disable-line variable-name
2019-03-03 15:07:49 +01:00
export const Buffer = typeforce.Buffer;
export const Hex = typeforce.Hex;
export const maybe = typeforce.maybe;
export const tuple = typeforce.tuple;
export const UInt8 = typeforce.UInt8;
export const UInt32 = typeforce.UInt32;
export const Function = typeforce.Function;
export const BufferN = typeforce.BufferN;
export const Null = typeforce.Null;
export const oneOf = typeforce.oneOf;