2019-03-03 15:07:49 +01:00
|
|
|
import { Network } from './networks';
|
|
|
|
import * as networks from './networks';
|
|
|
|
import * as payments from './payments';
|
2019-03-07 03:32:06 +01:00
|
|
|
import * as bscript from './script';
|
|
|
|
import * as types from './types';
|
2018-12-29 13:39:19 +01:00
|
|
|
|
2021-02-16 22:48:05 +01:00
|
|
|
const { bech32, bech32m } = require('bech32');
|
2019-03-03 15:07:49 +01:00
|
|
|
const bs58check = require('bs58check');
|
|
|
|
const typeforce = require('typeforce');
|
2014-05-05 07:31:40 +02:00
|
|
|
|
2019-03-07 03:32:06 +01:00
|
|
|
export interface Base58CheckResult {
|
2018-12-26 08:13:43 +01:00
|
|
|
hash: Buffer;
|
|
|
|
version: number;
|
2019-03-07 03:32:06 +01:00
|
|
|
}
|
2018-12-26 08:13:43 +01:00
|
|
|
|
2019-03-07 03:32:06 +01:00
|
|
|
export interface Bech32Result {
|
2018-12-26 08:13:43 +01:00
|
|
|
version: number;
|
|
|
|
prefix: string;
|
|
|
|
data: Buffer;
|
2019-03-07 03:32:06 +01:00
|
|
|
}
|
2018-12-26 08:13:43 +01:00
|
|
|
|
2021-02-16 22:48:05 +01:00
|
|
|
const FUTURE_SEGWIT_MAX_SIZE: number = 40;
|
|
|
|
const FUTURE_SEGWIT_MIN_SIZE: number = 2;
|
|
|
|
const FUTURE_SEGWIT_MAX_VERSION: number = 16;
|
|
|
|
const FUTURE_SEGWIT_MIN_VERSION: number = 1;
|
|
|
|
const FUTURE_SEGWIT_VERSION_DIFF: number = 0x50;
|
|
|
|
|
|
|
|
function _toFutureSegwitAddress(output: Buffer, network: Network): string {
|
|
|
|
const data = output.slice(2);
|
|
|
|
|
|
|
|
if (
|
|
|
|
data.length < FUTURE_SEGWIT_MIN_SIZE ||
|
|
|
|
data.length > FUTURE_SEGWIT_MAX_SIZE
|
|
|
|
)
|
|
|
|
throw new TypeError('Invalid program length for segwit address');
|
|
|
|
|
|
|
|
const version = output[0] - FUTURE_SEGWIT_VERSION_DIFF;
|
|
|
|
|
|
|
|
if (
|
|
|
|
version < FUTURE_SEGWIT_MIN_VERSION ||
|
|
|
|
version > FUTURE_SEGWIT_MAX_VERSION
|
|
|
|
)
|
|
|
|
throw new TypeError('Invalid version for segwit address');
|
|
|
|
|
|
|
|
if (output[1] !== data.length)
|
|
|
|
throw new TypeError('Invalid script for segwit address');
|
|
|
|
|
|
|
|
return toBech32(data, version, network.bech32);
|
|
|
|
}
|
|
|
|
|
2019-03-03 15:07:49 +01:00
|
|
|
export function fromBase58Check(address: string): Base58CheckResult {
|
|
|
|
const payload = bs58check.decode(address);
|
2017-08-17 06:12:43 +02:00
|
|
|
|
|
|
|
// TODO: 4.0.0, move to "toOutputScript"
|
2019-03-03 15:07:49 +01:00
|
|
|
if (payload.length < 21) throw new TypeError(address + ' is too short');
|
|
|
|
if (payload.length > 21) throw new TypeError(address + ' is too long');
|
2015-07-21 04:29:35 +02:00
|
|
|
|
2019-03-03 15:07:49 +01:00
|
|
|
const version = payload.readUInt8(0);
|
|
|
|
const hash = payload.slice(1);
|
2014-04-17 15:31:45 +02:00
|
|
|
|
2019-03-07 03:32:06 +01:00
|
|
|
return { version, hash };
|
2014-03-25 20:57:19 +01:00
|
|
|
}
|
2013-10-07 14:21:00 +02:00
|
|
|
|
2019-03-03 15:07:49 +01:00
|
|
|
export function fromBech32(address: string): Bech32Result {
|
2021-02-16 22:48:05 +01:00
|
|
|
let result;
|
|
|
|
let version;
|
|
|
|
try {
|
|
|
|
result = bech32.decode(address);
|
|
|
|
} catch (e) {}
|
|
|
|
|
|
|
|
if (result) {
|
|
|
|
version = result.words[0];
|
|
|
|
if (version !== 0) throw new TypeError(address + ' uses wrong encoding');
|
|
|
|
} else {
|
|
|
|
result = bech32m.decode(address);
|
|
|
|
version = result.words[0];
|
|
|
|
if (version === 0) throw new TypeError(address + ' uses wrong encoding');
|
|
|
|
}
|
|
|
|
|
2019-03-03 15:07:49 +01:00
|
|
|
const data = bech32.fromWords(result.words.slice(1));
|
2017-08-14 09:15:34 +02:00
|
|
|
|
2017-08-17 06:12:43 +02:00
|
|
|
return {
|
2021-02-16 22:48:05 +01:00
|
|
|
version,
|
2017-08-17 06:12:43 +02:00
|
|
|
prefix: result.prefix,
|
2019-03-03 15:07:49 +01:00
|
|
|
data: Buffer.from(data),
|
|
|
|
};
|
2017-08-14 09:15:34 +02:00
|
|
|
}
|
|
|
|
|
2019-03-03 15:07:49 +01:00
|
|
|
export function toBase58Check(hash: Buffer, version: number): string {
|
|
|
|
typeforce(types.tuple(types.Hash160bit, types.UInt8), arguments);
|
2015-07-21 02:55:47 +02:00
|
|
|
|
2019-03-03 15:07:49 +01:00
|
|
|
const payload = Buffer.allocUnsafe(21);
|
|
|
|
payload.writeUInt8(version, 0);
|
|
|
|
hash.copy(payload, 1);
|
2014-06-03 09:02:59 +02:00
|
|
|
|
2019-03-03 15:07:49 +01:00
|
|
|
return bs58check.encode(payload);
|
2014-03-25 20:57:19 +01:00
|
|
|
}
|
2014-05-05 07:31:40 +02:00
|
|
|
|
2019-03-03 15:07:49 +01:00
|
|
|
export function toBech32(
|
|
|
|
data: Buffer,
|
|
|
|
version: number,
|
|
|
|
prefix: string,
|
|
|
|
): string {
|
|
|
|
const words = bech32.toWords(data);
|
|
|
|
words.unshift(version);
|
2017-08-14 09:15:34 +02:00
|
|
|
|
2021-02-16 22:48:05 +01:00
|
|
|
return version === 0
|
|
|
|
? bech32.encode(prefix, words)
|
|
|
|
: bech32m.encode(prefix, words);
|
2017-08-14 09:15:34 +02:00
|
|
|
}
|
|
|
|
|
2019-04-11 08:55:33 +02:00
|
|
|
export function fromOutputScript(output: Buffer, network?: Network): string {
|
2019-03-07 03:32:06 +01:00
|
|
|
// TODO: Network
|
2019-03-03 15:07:49 +01:00
|
|
|
network = network || networks.bitcoin;
|
2016-09-28 00:44:21 +02:00
|
|
|
|
2019-03-03 15:07:49 +01:00
|
|
|
try {
|
2019-03-07 03:32:06 +01:00
|
|
|
return payments.p2pkh({ output, network }).address as string;
|
2019-03-03 15:07:49 +01:00
|
|
|
} catch (e) {}
|
|
|
|
try {
|
2019-03-07 03:32:06 +01:00
|
|
|
return payments.p2sh({ output, network }).address as string;
|
2019-03-03 15:07:49 +01:00
|
|
|
} catch (e) {}
|
|
|
|
try {
|
2019-03-07 03:32:06 +01:00
|
|
|
return payments.p2wpkh({ output, network }).address as string;
|
2019-03-03 15:07:49 +01:00
|
|
|
} catch (e) {}
|
|
|
|
try {
|
2019-03-07 03:32:06 +01:00
|
|
|
return payments.p2wsh({ output, network }).address as string;
|
2019-03-03 15:07:49 +01:00
|
|
|
} catch (e) {}
|
2021-02-16 22:48:05 +01:00
|
|
|
try {
|
|
|
|
return _toFutureSegwitAddress(output, network);
|
|
|
|
} catch (e) {}
|
2016-09-28 00:44:21 +02:00
|
|
|
|
2019-03-03 15:07:49 +01:00
|
|
|
throw new Error(bscript.toASM(output) + ' has no matching Address');
|
2016-09-28 00:44:21 +02:00
|
|
|
}
|
|
|
|
|
2019-04-11 08:55:33 +02:00
|
|
|
export function toOutputScript(address: string, network?: Network): Buffer {
|
2019-03-03 15:07:49 +01:00
|
|
|
network = network || networks.bitcoin;
|
2015-07-24 03:59:48 +02:00
|
|
|
|
2019-03-07 03:32:06 +01:00
|
|
|
let decodeBase58: Base58CheckResult | undefined;
|
|
|
|
let decodeBech32: Bech32Result | undefined;
|
2017-08-17 06:12:43 +02:00
|
|
|
try {
|
2019-03-03 15:07:49 +01:00
|
|
|
decodeBase58 = fromBase58Check(address);
|
2017-08-17 06:12:43 +02:00
|
|
|
} catch (e) {}
|
|
|
|
|
2018-12-26 08:13:43 +01:00
|
|
|
if (decodeBase58) {
|
2019-03-03 15:07:49 +01:00
|
|
|
if (decodeBase58.version === network.pubKeyHash)
|
2019-03-07 03:32:06 +01:00
|
|
|
return payments.p2pkh({ hash: decodeBase58.hash }).output as Buffer;
|
2019-03-03 15:07:49 +01:00
|
|
|
if (decodeBase58.version === network.scriptHash)
|
2019-03-07 03:32:06 +01:00
|
|
|
return payments.p2sh({ hash: decodeBase58.hash }).output as Buffer;
|
2017-08-17 06:12:43 +02:00
|
|
|
} else {
|
|
|
|
try {
|
2019-03-03 15:07:49 +01:00
|
|
|
decodeBech32 = fromBech32(address);
|
2017-08-17 06:12:43 +02:00
|
|
|
} catch (e) {}
|
|
|
|
|
2018-12-26 08:13:43 +01:00
|
|
|
if (decodeBech32) {
|
2019-03-03 15:07:49 +01:00
|
|
|
if (decodeBech32.prefix !== network.bech32)
|
|
|
|
throw new Error(address + ' has an invalid prefix');
|
2018-12-26 08:13:43 +01:00
|
|
|
if (decodeBech32.version === 0) {
|
2019-03-03 15:07:49 +01:00
|
|
|
if (decodeBech32.data.length === 20)
|
2019-03-07 03:32:06 +01:00
|
|
|
return payments.p2wpkh({ hash: decodeBech32.data }).output as Buffer;
|
2019-03-03 15:07:49 +01:00
|
|
|
if (decodeBech32.data.length === 32)
|
2019-03-07 03:32:06 +01:00
|
|
|
return payments.p2wsh({ hash: decodeBech32.data }).output as Buffer;
|
2021-02-16 22:48:05 +01:00
|
|
|
} else if (
|
|
|
|
decodeBech32.version >= FUTURE_SEGWIT_MIN_VERSION &&
|
|
|
|
decodeBech32.version <= FUTURE_SEGWIT_MAX_VERSION &&
|
|
|
|
decodeBech32.data.length >= FUTURE_SEGWIT_MIN_SIZE &&
|
|
|
|
decodeBech32.data.length <= FUTURE_SEGWIT_MAX_SIZE
|
|
|
|
)
|
|
|
|
return bscript.compile([
|
|
|
|
decodeBech32.version + FUTURE_SEGWIT_VERSION_DIFF,
|
|
|
|
decodeBech32.data,
|
|
|
|
]);
|
2017-08-17 06:12:43 +02:00
|
|
|
}
|
|
|
|
}
|
2014-05-05 07:31:40 +02:00
|
|
|
|
2019-03-03 15:07:49 +01:00
|
|
|
throw new Error(address + ' has no matching Script');
|
2014-05-05 07:31:40 +02:00
|
|
|
}
|