mirror of
https://github.com/zhigang1992/DefinitelyTyped.git
synced 2026-04-22 11:57:33 +08:00
upgrade bitcoin-js to v4 (#28818)
This commit is contained in:
committed by
Ryan Cavanaugh
parent
1d1dc5e817
commit
a25d5975ef
@@ -1,4 +1,3 @@
|
||||
import bigi = require('bigi');
|
||||
import bitcoin = require('bitcoinjs-lib');
|
||||
|
||||
// For testing only
|
||||
@@ -8,19 +7,18 @@ function rng() {
|
||||
|
||||
// Generate a random bitcoin address
|
||||
const keyPair1 = bitcoin.ECPair.makeRandom({rng});
|
||||
const address = keyPair1.getAddress();
|
||||
const { address } = bitcoin.payments.p2pkh({ pubkey: keyPair1.publicKey });
|
||||
keyPair1.toWIF();
|
||||
|
||||
// Generate an address from a SHA256 hash
|
||||
const hash = bitcoin.crypto.sha256(Buffer.from('correct horse battery staple', 'utf8'));
|
||||
const d = bigi.fromBuffer(hash);
|
||||
const keyPair2 = new bitcoin.ECPair(d);
|
||||
const keyPair2 = bitcoin.ECPair.fromPrivateKey(hash);
|
||||
|
||||
// Generate a random keypair for alternative networks
|
||||
const keyPair3 = bitcoin.ECPair.makeRandom({network: bitcoin.networks.litecoin, rng});
|
||||
const keyPair3 = bitcoin.ECPair.makeRandom({network: bitcoin.networks.testnet, rng});
|
||||
keyPair3.toWIF();
|
||||
keyPair3.getAddress();
|
||||
const network = keyPair3.getNetwork();
|
||||
bitcoin.payments.p2pkh({ pubkey: keyPair3.publicKey });
|
||||
const network = keyPair3.network;
|
||||
|
||||
// Test TransactionBuilder and Transaction
|
||||
const txb = new bitcoin.TransactionBuilder();
|
||||
@@ -41,22 +39,3 @@ bitcoin.address.toBase58Check(rsBase58Check.hash, rsBase58Check.version);
|
||||
bitcoin.address.toBech32(rsBech32.data, rsBech32.version, rsBech32.prefix);
|
||||
bitcoin.address.toOutputScript(address);
|
||||
bitcoin.address.toOutputScript(address, network);
|
||||
|
||||
const redeemScript = bitcoin.script.multisig.output.encode(
|
||||
2,
|
||||
[
|
||||
new Buffer('12345678901234567890123456789012'),
|
||||
new Buffer('12345678901234567890123456789012'),
|
||||
new Buffer('12345678901234567890123456789012'),
|
||||
]
|
||||
);
|
||||
bitcoin.script.scriptHash.input.encode(
|
||||
bitcoin.script.multisig.input.encodeStack(
|
||||
[
|
||||
new Buffer('12345678901234567890123456789012'),
|
||||
new Buffer('12345678901234567890123456789012'),
|
||||
],
|
||||
redeemScript,
|
||||
),
|
||||
redeemScript,
|
||||
);
|
||||
|
||||
186
types/bitcoinjs-lib/index.d.ts
vendored
186
types/bitcoinjs-lib/index.d.ts
vendored
@@ -1,4 +1,4 @@
|
||||
// Type definitions for bitcoinjs-lib 3.4
|
||||
// Type definitions for bitcoinjs-lib 4.0
|
||||
// Project: https://github.com/bitcoinjs/bitcoinjs-lib
|
||||
// Definitions by: Mohamed Hegazy <https://github.com/mhegazy>
|
||||
// Daniel <https://github.com/dlebrecht>
|
||||
@@ -12,8 +12,6 @@
|
||||
|
||||
/// <reference types="node" />
|
||||
|
||||
import BigInteger = require('bigi');
|
||||
|
||||
export interface Out {
|
||||
script: Buffer;
|
||||
value: number;
|
||||
@@ -68,29 +66,23 @@ export class Block {
|
||||
}
|
||||
|
||||
export class ECPair {
|
||||
constructor(d: BigInteger, Q?: null, options?: { compressed?: boolean, network?: Network });
|
||||
|
||||
constructor(d: null | undefined, Q: any, options?: { compressed?: boolean, network?: Network }); // Q should be ECPoint, but not sure how to define such type
|
||||
|
||||
d: BigInteger;
|
||||
|
||||
readonly compressed: boolean;
|
||||
|
||||
readonly privateKey: Buffer;
|
||||
|
||||
readonly publicKey: Buffer;
|
||||
|
||||
readonly network: Network;
|
||||
|
||||
getAddress(): string;
|
||||
|
||||
getNetwork(): Network;
|
||||
|
||||
getPublicKeyBuffer(): Buffer;
|
||||
|
||||
sign(hash: Buffer): ECSignature;
|
||||
sign(hash: Buffer): Buffer;
|
||||
|
||||
toWIF(): string;
|
||||
|
||||
verify(hash: Buffer, signature: ECSignature): boolean;
|
||||
verify(hash: Buffer, signature: Buffer): boolean;
|
||||
|
||||
static fromPublicKeyBuffer(buffer: Buffer, network: Network): ECPair;
|
||||
static fromPrivateKey(buffer: Buffer, options?: { compressed?: boolean, network?: Network }): ECPair;
|
||||
|
||||
static fromPublicKey(buffer: Buffer, options?: { compressed?: boolean, network?: Network }): ECPair;
|
||||
|
||||
static fromWIF(string: string, network?: Network): ECPair;
|
||||
|
||||
@@ -99,66 +91,6 @@ export class ECPair {
|
||||
|
||||
export type Rng = (size: number) => Buffer;
|
||||
|
||||
export class ECSignature {
|
||||
constructor(r: BigInteger, s: BigInteger);
|
||||
|
||||
toCompact(i: number, compressed: boolean): Buffer;
|
||||
|
||||
toDER(): Buffer;
|
||||
|
||||
toScriptSignature(hashType: number): Buffer;
|
||||
|
||||
static fromDER(buffer: Buffer): ECSignature;
|
||||
|
||||
static parseCompact(buffer: Buffer): { compressed: boolean, i: number, signature: ECSignature };
|
||||
|
||||
static parseScriptSignature(buffer: Buffer): { signature: ECSignature, hashType: number };
|
||||
}
|
||||
|
||||
export class HDNode {
|
||||
constructor(keyPair: ECPair, chainCode: Buffer);
|
||||
|
||||
keyPair: ECPair;
|
||||
|
||||
derive(index: number): HDNode;
|
||||
|
||||
deriveHardened(index: number): HDNode;
|
||||
|
||||
derivePath(path: string): HDNode;
|
||||
|
||||
getAddress(): string;
|
||||
|
||||
getFingerprint(): Buffer;
|
||||
|
||||
getIdentifier(): Buffer;
|
||||
|
||||
getNetwork(): Network;
|
||||
|
||||
getPublicKeyBuffer(): Buffer;
|
||||
|
||||
isNeutered(): boolean;
|
||||
|
||||
neutered(): HDNode;
|
||||
|
||||
sign(hash: Buffer): ECSignature;
|
||||
|
||||
toBase58(): string;
|
||||
|
||||
verify(hash: Buffer, signature: ECSignature): boolean;
|
||||
|
||||
static HIGHEST_BIT: number;
|
||||
|
||||
static LENGTH: number;
|
||||
|
||||
static fromBase58(string: string, networks?: Network[] | Network): HDNode;
|
||||
|
||||
static fromSeedBuffer(seed: Buffer, network?: Network): HDNode;
|
||||
|
||||
static fromSeedHex(hex: string, network?: Network): HDNode;
|
||||
|
||||
static MASTER_SECRET: Buffer;
|
||||
}
|
||||
|
||||
export class Transaction {
|
||||
version: number;
|
||||
locktime: number;
|
||||
@@ -236,9 +168,6 @@ export interface Input {
|
||||
}
|
||||
|
||||
export class TransactionBuilder {
|
||||
tx: Transaction;
|
||||
inputs: Input[];
|
||||
|
||||
constructor(network?: Network, maximumFeeRate?: number);
|
||||
|
||||
addInput(txhash: Buffer | string | Transaction, vout: number, sequence?: number, prevOutScript?: Buffer): number;
|
||||
@@ -261,7 +190,6 @@ export class TransactionBuilder {
|
||||
|
||||
export const networks: {
|
||||
bitcoin: Network;
|
||||
litecoin: Network;
|
||||
testnet: Network;
|
||||
};
|
||||
|
||||
@@ -391,7 +319,7 @@ export namespace address {
|
||||
/** @since 3.2.0 */
|
||||
function fromBech32(address: string): { data: Buffer, prefix: string, version: number };
|
||||
|
||||
function fromOutputScript(outputScript: Buffer, network?: Network): string;
|
||||
function fromOutputScript(output: Buffer, network?: Network): string;
|
||||
|
||||
function toBase58Check(hash: Buffer, version: number): string;
|
||||
|
||||
@@ -401,26 +329,6 @@ export namespace address {
|
||||
function toOutputScript(address: string, network?: Network): Buffer;
|
||||
}
|
||||
|
||||
export namespace bufferutils {
|
||||
function pushDataSize(i: number): number;
|
||||
|
||||
function readPushDataInt(buffer: Buffer, offset: number): { opcode: number, number: number, size: number };
|
||||
|
||||
function readUInt64LE(buffer: Buffer, offset: number): number;
|
||||
|
||||
function readVarInt(buffer: Buffer, offset: number): { number: number, size: number };
|
||||
|
||||
function varIntBuffer(number: number, buffer: Buffer, offset: number): Buffer;
|
||||
|
||||
function varIntSize(number: number): number;
|
||||
|
||||
function writePushDataInt(buffer: Buffer, number: number, offset: number): number;
|
||||
|
||||
function writeUInt64LE(buffer: Buffer, value: number, offset: number): number;
|
||||
|
||||
function writeVarInt(buffer: Buffer, number: number, offset: number): number;
|
||||
}
|
||||
|
||||
export namespace crypto {
|
||||
function hash160(buffer: Buffer): Buffer;
|
||||
|
||||
@@ -449,7 +357,7 @@ export namespace script {
|
||||
|
||||
function isCanonicalPubKey(buffer: Buffer): boolean;
|
||||
|
||||
function isCanonicalSignature(buffer: Buffer): boolean;
|
||||
function isCanonicalScriptSignature(buffer: Buffer): boolean;
|
||||
|
||||
function isDefinedHashType(hashType: any): boolean;
|
||||
|
||||
@@ -465,66 +373,48 @@ export namespace script {
|
||||
function encode(number: number): Buffer;
|
||||
}
|
||||
|
||||
namespace signature {
|
||||
function decode(buffer: Buffer): { signature: Buffer, hashType: number };
|
||||
|
||||
function encode(signature: Buffer, hashType: number): Buffer;
|
||||
}
|
||||
|
||||
const multisig: {
|
||||
input: {
|
||||
check(script: Buffer, allowIncomplete: boolean): boolean;
|
||||
decode(buffer: Buffer): Array<Buffer | number>;
|
||||
decodeStack(stack: Buffer[], allowIncomplete: boolean): Array<Buffer | number>;
|
||||
encode(signatures: Buffer[], scriptPubKey: Buffer): Buffer;
|
||||
encodeStack(signatures: Buffer[], scriptPubKey: Buffer): Buffer[];
|
||||
};
|
||||
output: {
|
||||
check(script: Buffer, allowIncomplete: boolean): boolean;
|
||||
decode(buffer: Buffer, allowIncomplete: boolean): { m: number; pubKeys: Array<Buffer | number> };
|
||||
encode(m: number, pubKeys: Array<Buffer | number>): Buffer;
|
||||
};
|
||||
};
|
||||
|
||||
const pubKey: {
|
||||
input: {
|
||||
check(script: Buffer): boolean;
|
||||
decode(buffer: Buffer): Array<Buffer | number>;
|
||||
decodeStack(stack: Buffer[]): Array<Buffer | number>;
|
||||
encode(signature: Buffer): Buffer;
|
||||
encodeStack(signature: Buffer): Buffer[];
|
||||
};
|
||||
|
||||
output: {
|
||||
check(script: Buffer): boolean;
|
||||
decode(buffer: Buffer): Buffer | number;
|
||||
encode(pubKey: Buffer): Buffer;
|
||||
};
|
||||
};
|
||||
|
||||
const pubKeyHash: {
|
||||
input: {
|
||||
check(script: Buffer): boolean;
|
||||
decode(buffer: Buffer): { signature: Buffer; pubKey: Buffer };
|
||||
decodeStack(stack: Buffer[]): { signature: Buffer; pubKey: Buffer };
|
||||
encode(signature: Buffer, pubKey: Buffer): Buffer;
|
||||
encodeStack(signature: Buffer, pubKey: Buffer): [Buffer, Buffer];
|
||||
};
|
||||
|
||||
output: {
|
||||
check(script: Buffer): boolean;
|
||||
decode(buffer: Buffer): Buffer;
|
||||
encode(pubKeyHash: Buffer): Buffer;
|
||||
};
|
||||
};
|
||||
|
||||
const scriptHash: {
|
||||
input: {
|
||||
check(script: Buffer, allowIncomplete: boolean): boolean;
|
||||
decode(buffer: Buffer): { redeemScriptStack: Buffer[]; redeemScript: Buffer };
|
||||
decodeStack(stack: Buffer[]): { redeemScriptStack: Buffer[]; redeemScript: Buffer };
|
||||
encode(redeemScriptSig: Array<Buffer | number>, redeemScript: Buffer): Buffer;
|
||||
encodeStack(redeemScriptStack: Buffer[], redeemScript: Buffer): Buffer[];
|
||||
};
|
||||
|
||||
output: {
|
||||
check(script: Buffer): boolean;
|
||||
decode(buffer: Buffer): Buffer;
|
||||
encode(scriptHash: Buffer): Buffer;
|
||||
};
|
||||
};
|
||||
|
||||
@@ -539,36 +429,56 @@ export namespace script {
|
||||
const witnessPubKeyHash: {
|
||||
input: {
|
||||
check(script: Buffer): boolean;
|
||||
decodeStack(stack: Buffer[]): { signature: Buffer; pubKey: Buffer };
|
||||
encodeStack(signature: Buffer, pubKey: Buffer): [Buffer, Buffer];
|
||||
};
|
||||
|
||||
output: {
|
||||
check(script: Buffer): boolean;
|
||||
decode(buffer: Buffer): Buffer;
|
||||
encode(pubKeyHash: Buffer): Buffer;
|
||||
};
|
||||
};
|
||||
|
||||
const witnessScriptHash: {
|
||||
input: {
|
||||
check(script: Buffer, allowIncomplete: boolean): boolean;
|
||||
decodeStack(stack: Buffer[]): { redeemScriptStack: Buffer[]; redeemScript: Buffer };
|
||||
encodeStack(redeemScriptStack: Buffer[], redeemScript: Buffer): Buffer[];
|
||||
};
|
||||
|
||||
output: {
|
||||
check(script: Buffer): boolean;
|
||||
decode(buffer: Buffer): Buffer;
|
||||
encode(scriptHash: Buffer): Buffer;
|
||||
};
|
||||
};
|
||||
|
||||
const nullData: {
|
||||
output: {
|
||||
check(script: Buffer): boolean;
|
||||
decode(buffer: Buffer): Buffer;
|
||||
encode(data: Buffer): Buffer;
|
||||
};
|
||||
};
|
||||
}
|
||||
|
||||
export namespace payments {
|
||||
function p2data(a: { network?: Network, output?: Buffer, data?: Buffer[] }, opts?: { validate?: boolean }):
|
||||
{ output: Buffer, data: Buffer[] };
|
||||
|
||||
function p2ms(a: { network?: Network, m?: number, n?: number, output?: Buffer, pubkeys?: Buffer[], signatures?: Buffer[], input?: Buffer }, opts?: { validate?: boolean }):
|
||||
{ output: Buffer, m: number, n: number, pubkeys: Buffer[], signatures: Buffer[], input: Buffer, witness: Buffer[] };
|
||||
|
||||
function p2pk(a: { input?: Buffer, network?: Network, output?: Buffer, pubkey?: Buffer, signature?: Buffer }, opts?: { validate?: boolean }):
|
||||
{ output: Buffer, pubkey: Buffer, signature: Buffer, input: Buffer, witness: Buffer[] };
|
||||
|
||||
function p2pkh(a: { address?: string, hash?: Buffer, input?: Buffer, network?: Network, output?: Buffer, pubkey?: Buffer, signature?: Buffer }, opts?: { validate?: boolean }):
|
||||
{ address: string, hash: Buffer, output: Buffer, pubkey: Buffer, signature: Buffer, input: Buffer, witness: Buffer[] };
|
||||
|
||||
function p2sh(a: { address?: string, hash?: Buffer, input?: Buffer, network?: Network, output?: Buffer, witness?: Buffer[], redeem?: Redeem }, opts?: { validate?: boolean }):
|
||||
{ address: string, hash: Buffer, output: Buffer, redeem: Redeem, input: Buffer, witness: Buffer[] };
|
||||
|
||||
function p2wpkh(a: { address?: string, hash?: Buffer, input?: Buffer, network?: Network, output?: Buffer, pubkey?: Buffer, signature?: Buffer, witness?: Buffer[] },
|
||||
opts?: { validate?: boolean }): { address: string, hash: Buffer, output: Buffer, pubkey: Buffer, signature: Buffer, input: Buffer, witness: Buffer[] };
|
||||
|
||||
function p2wsh(a: { address?: string, hash?: Buffer, input?: Buffer, network?: Network, output?: Buffer, witness?: Buffer[], redeem?: Redeem }, opts?: { validate?: boolean }):
|
||||
{ address: string, hash: Buffer, output: Buffer, redeem: Redeem, input: Buffer, witness: Buffer[] };
|
||||
|
||||
class Redeem {
|
||||
input?: Buffer;
|
||||
network?: Network;
|
||||
output?: Buffer;
|
||||
witness?: Buffer[];
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user