Merge branch 'master' into addPsbtMethods
This commit is contained in:
commit
17c47e9102
22 changed files with 412 additions and 79 deletions
|
@ -42,7 +42,7 @@ describe('bufferutils', () => {
|
|||
});
|
||||
});
|
||||
|
||||
fixtures.invalid.readUInt64LE.forEach(f => {
|
||||
fixtures.invalid.writeUInt64LE.forEach(f => {
|
||||
it('throws on ' + f.description, () => {
|
||||
const buffer = Buffer.alloc(8, 0);
|
||||
|
||||
|
|
|
@ -146,6 +146,13 @@ describe('ECPair', () => {
|
|||
assert.strictEqual(result, f.WIF);
|
||||
});
|
||||
});
|
||||
it('throws if no private key is found', () => {
|
||||
assert.throws(() => {
|
||||
const keyPair = ECPair.makeRandom();
|
||||
delete (keyPair as any).__D;
|
||||
keyPair.toWIF();
|
||||
}, /Missing private key/);
|
||||
});
|
||||
});
|
||||
|
||||
describe('makeRandom', () => {
|
||||
|
|
8
test/fixtures/address.json
vendored
8
test/fixtures/address.json
vendored
|
@ -189,6 +189,14 @@
|
|||
{
|
||||
"exception": "has no matching Script",
|
||||
"address": "BC13W508D6QEJXTDG4Y5R3ZARVARY0C5XW7KN40WF2"
|
||||
},
|
||||
{
|
||||
"exception": "has no matching Script",
|
||||
"address": "bc1qw508d6qejxtdg4y5r3zarvary0c5xw7kv8f3t5"
|
||||
},
|
||||
{
|
||||
"exception": "has no matching Script",
|
||||
"address": "bc1qqqqqqqqqqv9qus"
|
||||
}
|
||||
]
|
||||
}
|
||||
|
|
26
test/fixtures/bufferutils.json
vendored
26
test/fixtures/bufferutils.json
vendored
|
@ -71,6 +71,32 @@
|
|||
"hex": "0100000000002000",
|
||||
"dec": 9007199254740993
|
||||
}
|
||||
],
|
||||
"writeUInt64LE": [
|
||||
{
|
||||
"description": "n === 2^53",
|
||||
"exception": "RangeError: value out of range",
|
||||
"hex": "0000000000002000",
|
||||
"dec": 9007199254740992
|
||||
},
|
||||
{
|
||||
"description": "n > 2^53",
|
||||
"exception": "RangeError: value out of range",
|
||||
"hex": "0100000000002000",
|
||||
"dec": 9007199254740993
|
||||
},
|
||||
{
|
||||
"description": "n < 0",
|
||||
"exception": "specified a negative value for writing an unsigned value",
|
||||
"hex": "",
|
||||
"dec": -1
|
||||
},
|
||||
{
|
||||
"description": "0 < n < 1",
|
||||
"exception": "value has a fractional component",
|
||||
"hex": "",
|
||||
"dec": 0.1
|
||||
}
|
||||
]
|
||||
}
|
||||
}
|
||||
|
|
30
test/fixtures/embed.json
vendored
30
test/fixtures/embed.json
vendored
|
@ -44,6 +44,36 @@
|
|||
"arguments": {
|
||||
"output": "OP_1 OP_2 OP_ADD"
|
||||
}
|
||||
},
|
||||
{
|
||||
"description": "Return value and data do not match",
|
||||
"exception": "Data mismatch",
|
||||
"options": {},
|
||||
"arguments": {
|
||||
"output": "OP_RETURN a3b147dbe4a85579fc4b5a1811e76620560e07267e62b9a0d6858f9127735cadd82f67e06c24dbc4",
|
||||
"data": [
|
||||
"a3b147dbe4a85579fc4b5a1855555555555555555555555555555555555555555555555555555555"
|
||||
]
|
||||
}
|
||||
},
|
||||
{
|
||||
"description": "Script length incorrect",
|
||||
"exception": "Data mismatch",
|
||||
"options": {},
|
||||
"arguments": {
|
||||
"output": "OP_RETURN a3b1 47db",
|
||||
"data": [
|
||||
"a3b1"
|
||||
]
|
||||
}
|
||||
},
|
||||
{
|
||||
"description": "Return data is not buffer",
|
||||
"exception": "Output is invalid",
|
||||
"options": {},
|
||||
"arguments": {
|
||||
"output": "OP_RETURN OP_1"
|
||||
}
|
||||
}
|
||||
],
|
||||
"dynamic": {
|
||||
|
|
20
test/fixtures/p2sh.json
vendored
20
test/fixtures/p2sh.json
vendored
|
@ -323,6 +323,26 @@
|
|||
}
|
||||
}
|
||||
},
|
||||
{
|
||||
"exception": "Input and witness provided",
|
||||
"arguments": {
|
||||
"redeem": {
|
||||
"input": "OP_0",
|
||||
"witness": [
|
||||
"030000000000000000000000000000000000000000000000000000000000000001"
|
||||
]
|
||||
}
|
||||
}
|
||||
},
|
||||
{
|
||||
"exception": "Non push-only scriptSig",
|
||||
"arguments": {
|
||||
"redeem": {
|
||||
"input": "OP_RETURN",
|
||||
"output": "OP_1"
|
||||
}
|
||||
}
|
||||
},
|
||||
{
|
||||
"exception": "Redeem.output too short",
|
||||
"arguments": {
|
||||
|
|
27
test/fixtures/p2wpkh.json
vendored
27
test/fixtures/p2wpkh.json
vendored
|
@ -113,6 +113,23 @@
|
|||
"output": "OP_RESERVED ea6d525c0c955d90d3dbd29a81ef8bfb79003727"
|
||||
}
|
||||
},
|
||||
{
|
||||
"exception": "Invalid pubkey for p2wpkh",
|
||||
"description": "Uncompressed pubkey in pubkey",
|
||||
"arguments": {
|
||||
"pubkey": "049fa211b0fca342589ca381cc96520c3d0e3924832158d9f29891936cac091e80687acdca51868ee1f89a3bb36bb16f186262938e1f94c1e7692924935b9b1457"
|
||||
}
|
||||
},
|
||||
{
|
||||
"exception": "Witness has invalid pubkey",
|
||||
"description": "Uncompressed pubkey in witness",
|
||||
"arguments": {
|
||||
"witness": [
|
||||
"300602010002010001",
|
||||
"049fa211b0fca342589ca381cc96520c3d0e3924832158d9f29891936cac091e80687acdca51868ee1f89a3bb36bb16f186262938e1f94c1e7692924935b9b1457"
|
||||
]
|
||||
}
|
||||
},
|
||||
{
|
||||
"exception": "Pubkey mismatch",
|
||||
"options": {},
|
||||
|
@ -124,6 +141,16 @@
|
|||
]
|
||||
}
|
||||
},
|
||||
{
|
||||
"exception": "Signature mismatch",
|
||||
"arguments": {
|
||||
"signature": "300602010002010002",
|
||||
"witness": [
|
||||
"300602010002010001",
|
||||
"030000000000000000000000000000000000000000000000000000000000000001"
|
||||
]
|
||||
}
|
||||
},
|
||||
{
|
||||
"exception": "Invalid prefix or Network mismatch",
|
||||
"arguments": {
|
||||
|
|
24
test/fixtures/p2wsh.json
vendored
24
test/fixtures/p2wsh.json
vendored
|
@ -344,6 +344,30 @@
|
|||
}
|
||||
}
|
||||
},
|
||||
{
|
||||
"exception": "redeem.input or redeem.output contains uncompressed pubkey",
|
||||
"arguments": {
|
||||
"redeem": {
|
||||
"output": "049fa211b0fca342589ca381cc96520c3d0e3924832158d9f29891936cac091e80687acdca51868ee1f89a3bb36bb16f186262938e1f94c1e7692924935b9b1457 OP_CHECKSIG"
|
||||
}
|
||||
}
|
||||
},
|
||||
{
|
||||
"exception": "redeem.input or redeem.output contains uncompressed pubkey",
|
||||
"arguments": {
|
||||
"redeem": {
|
||||
"input": "049fa211b0fca342589ca381cc96520c3d0e3924832158d9f29891936cac091e80687acdca51868ee1f89a3bb36bb16f186262938e1f94c1e7692924935b9b1457"
|
||||
}
|
||||
}
|
||||
},
|
||||
{
|
||||
"exception": "Witness contains uncompressed pubkey",
|
||||
"arguments": {
|
||||
"witness": [
|
||||
"049fa211b0fca342589ca381cc96520c3d0e3924832158d9f29891936cac091e80687acdca51868ee1f89a3bb36bb16f186262938e1f94c1e7692924935b9b1457"
|
||||
]
|
||||
}
|
||||
},
|
||||
{
|
||||
"exception": "Invalid prefix or Network mismatch",
|
||||
"arguments": {
|
||||
|
|
4
test/fixtures/transaction_builder.json
vendored
4
test/fixtures/transaction_builder.json
vendored
|
@ -2122,7 +2122,7 @@
|
|||
},
|
||||
{
|
||||
"description": "Transaction w/ P2WSH(P2PK), signing with uncompressed public key",
|
||||
"exception": "BIP143 rejects uncompressed public keys in P2WPKH or P2WSH",
|
||||
"exception": "redeem.input or redeem.output contains uncompressed pubkey",
|
||||
"inputs": [
|
||||
{
|
||||
"txId": "2fddebc1a7e67e04fc6b77645ae9ae10eeaa35e168606587d79b031ebca33345",
|
||||
|
@ -2148,7 +2148,7 @@
|
|||
},
|
||||
{
|
||||
"description": "Transaction w/ P2SH(P2WSH(P2PK)), signing with uncompressed public key",
|
||||
"exception": "BIP143 rejects uncompressed public keys in P2WPKH or P2WSH",
|
||||
"exception": "redeem.input or redeem.output contains uncompressed pubkey",
|
||||
"inputs": [
|
||||
{
|
||||
"txId": "2fddebc1a7e67e04fc6b77645ae9ae10eeaa35e168606587d79b031ebca33345",
|
||||
|
|
|
@ -649,7 +649,7 @@ function createPayment(_type: string, myKeys?: any[], network?: any): any {
|
|||
if (type.slice(0, 4) === 'p2ms') {
|
||||
payment = bitcoin.payments.p2ms({
|
||||
m,
|
||||
pubkeys: keys.map(key => key.publicKey).sort(),
|
||||
pubkeys: keys.map(key => key.publicKey).sort((a, b) => a.compare(b)),
|
||||
network,
|
||||
});
|
||||
} else if (['p2sh', 'p2wsh'].indexOf(type) > -1) {
|
||||
|
|
|
@ -2,7 +2,15 @@ import * as assert from 'assert';
|
|||
import * as crypto from 'crypto';
|
||||
import { describe, it } from 'mocha';
|
||||
|
||||
import { bip32, ECPair, networks as NETWORKS, payments, Psbt } from '..';
|
||||
import {
|
||||
bip32,
|
||||
ECPair,
|
||||
networks as NETWORKS,
|
||||
payments,
|
||||
Psbt,
|
||||
Signer,
|
||||
SignerAsync,
|
||||
} from '..';
|
||||
|
||||
import * as preFixtures from './fixtures/psbt.json';
|
||||
|
||||
|
@ -23,6 +31,40 @@ const fixtures = initBuffers(preFixtures);
|
|||
const upperCaseFirstLetter = (str: string): string =>
|
||||
str.replace(/^./, s => s.toUpperCase());
|
||||
|
||||
const toAsyncSigner = (signer: Signer): SignerAsync => {
|
||||
const ret: SignerAsync = {
|
||||
publicKey: signer.publicKey,
|
||||
sign: (hash: Buffer, lowerR: boolean | undefined): Promise<Buffer> => {
|
||||
return new Promise(
|
||||
(resolve, rejects): void => {
|
||||
setTimeout(() => {
|
||||
try {
|
||||
const r = signer.sign(hash, lowerR);
|
||||
resolve(r);
|
||||
} catch (e) {
|
||||
rejects(e);
|
||||
}
|
||||
}, 10);
|
||||
},
|
||||
);
|
||||
},
|
||||
};
|
||||
return ret;
|
||||
};
|
||||
const failedAsyncSigner = (publicKey: Buffer): SignerAsync => {
|
||||
return {
|
||||
publicKey,
|
||||
sign: (__: Buffer): Promise<Buffer> => {
|
||||
return new Promise(
|
||||
(_, reject): void => {
|
||||
setTimeout(() => {
|
||||
reject(new Error('sign failed'));
|
||||
}, 10);
|
||||
},
|
||||
);
|
||||
},
|
||||
};
|
||||
};
|
||||
// const b = (hex: string) => Buffer.from(hex, 'hex');
|
||||
|
||||
describe(`Psbt`, () => {
|
||||
|
@ -165,25 +207,39 @@ describe(`Psbt`, () => {
|
|||
it(f.description, async () => {
|
||||
if (f.shouldSign) {
|
||||
const psbtThatShouldsign = Psbt.fromBase64(f.shouldSign.psbt);
|
||||
assert.doesNotReject(async () => {
|
||||
await assert.doesNotReject(async () => {
|
||||
await psbtThatShouldsign.signInputAsync(
|
||||
f.shouldSign.inputToCheck,
|
||||
ECPair.fromWIF(f.shouldSign.WIF),
|
||||
f.shouldSign.sighashTypes || undefined,
|
||||
);
|
||||
});
|
||||
await assert.rejects(async () => {
|
||||
await psbtThatShouldsign.signInputAsync(
|
||||
f.shouldSign.inputToCheck,
|
||||
failedAsyncSigner(ECPair.fromWIF(f.shouldSign.WIF).publicKey),
|
||||
f.shouldSign.sighashTypes || undefined,
|
||||
);
|
||||
}, /sign failed/);
|
||||
}
|
||||
|
||||
if (f.shouldThrow) {
|
||||
const psbtThatShouldThrow = Psbt.fromBase64(f.shouldThrow.psbt);
|
||||
assert.rejects(async () => {
|
||||
await assert.rejects(async () => {
|
||||
await psbtThatShouldThrow.signInputAsync(
|
||||
f.shouldThrow.inputToCheck,
|
||||
ECPair.fromWIF(f.shouldThrow.WIF),
|
||||
(f.shouldThrow as any).sighashTypes || undefined,
|
||||
);
|
||||
}, new RegExp(f.shouldThrow.errorMessage));
|
||||
assert.rejects(async () => {
|
||||
await assert.rejects(async () => {
|
||||
await psbtThatShouldThrow.signInputAsync(
|
||||
f.shouldThrow.inputToCheck,
|
||||
toAsyncSigner(ECPair.fromWIF(f.shouldThrow.WIF)),
|
||||
(f.shouldThrow as any).sighashTypes || undefined,
|
||||
);
|
||||
}, new RegExp(f.shouldThrow.errorMessage));
|
||||
await assert.rejects(async () => {
|
||||
await (psbtThatShouldThrow.signInputAsync as any)(
|
||||
f.shouldThrow.inputToCheck,
|
||||
);
|
||||
|
@ -230,7 +286,7 @@ describe(`Psbt`, () => {
|
|||
it(f.description, async () => {
|
||||
if (f.shouldSign) {
|
||||
const psbtThatShouldsign = Psbt.fromBase64(f.shouldSign.psbt);
|
||||
assert.doesNotReject(async () => {
|
||||
await assert.doesNotReject(async () => {
|
||||
await psbtThatShouldsign.signAllInputsAsync(
|
||||
ECPair.fromWIF(f.shouldSign.WIF),
|
||||
f.shouldSign.sighashTypes || undefined,
|
||||
|
@ -240,13 +296,13 @@ describe(`Psbt`, () => {
|
|||
|
||||
if (f.shouldThrow) {
|
||||
const psbtThatShouldThrow = Psbt.fromBase64(f.shouldThrow.psbt);
|
||||
assert.rejects(async () => {
|
||||
await assert.rejects(async () => {
|
||||
await psbtThatShouldThrow.signAllInputsAsync(
|
||||
ECPair.fromWIF(f.shouldThrow.WIF),
|
||||
(f.shouldThrow as any).sighashTypes || undefined,
|
||||
);
|
||||
}, new RegExp('No inputs were signed'));
|
||||
assert.rejects(async () => {
|
||||
await assert.rejects(async () => {
|
||||
await (psbtThatShouldThrow.signAllInputsAsync as any)();
|
||||
}, new RegExp('Need Signer to sign input'));
|
||||
}
|
||||
|
@ -289,7 +345,7 @@ describe(`Psbt`, () => {
|
|||
it(f.description, async () => {
|
||||
if (f.shouldSign) {
|
||||
const psbtThatShouldsign = Psbt.fromBase64(f.shouldSign.psbt);
|
||||
assert.doesNotReject(async () => {
|
||||
await assert.doesNotReject(async () => {
|
||||
await psbtThatShouldsign.signInputHDAsync(
|
||||
f.shouldSign.inputToCheck,
|
||||
bip32.fromBase58(f.shouldSign.xprv),
|
||||
|
@ -300,14 +356,14 @@ describe(`Psbt`, () => {
|
|||
|
||||
if (f.shouldThrow) {
|
||||
const psbtThatShouldThrow = Psbt.fromBase64(f.shouldThrow.psbt);
|
||||
assert.rejects(async () => {
|
||||
await assert.rejects(async () => {
|
||||
await psbtThatShouldThrow.signInputHDAsync(
|
||||
f.shouldThrow.inputToCheck,
|
||||
bip32.fromBase58(f.shouldThrow.xprv),
|
||||
(f.shouldThrow as any).sighashTypes || undefined,
|
||||
);
|
||||
}, new RegExp(f.shouldThrow.errorMessage));
|
||||
assert.rejects(async () => {
|
||||
await assert.rejects(async () => {
|
||||
await (psbtThatShouldThrow.signInputHDAsync as any)(
|
||||
f.shouldThrow.inputToCheck,
|
||||
);
|
||||
|
@ -355,7 +411,7 @@ describe(`Psbt`, () => {
|
|||
it(f.description, async () => {
|
||||
if (f.shouldSign) {
|
||||
const psbtThatShouldsign = Psbt.fromBase64(f.shouldSign.psbt);
|
||||
assert.doesNotReject(async () => {
|
||||
await assert.doesNotReject(async () => {
|
||||
await psbtThatShouldsign.signAllInputsHDAsync(
|
||||
bip32.fromBase58(f.shouldSign.xprv),
|
||||
(f.shouldSign as any).sighashTypes || undefined,
|
||||
|
@ -365,13 +421,13 @@ describe(`Psbt`, () => {
|
|||
|
||||
if (f.shouldThrow) {
|
||||
const psbtThatShouldThrow = Psbt.fromBase64(f.shouldThrow.psbt);
|
||||
assert.rejects(async () => {
|
||||
await assert.rejects(async () => {
|
||||
await psbtThatShouldThrow.signAllInputsHDAsync(
|
||||
bip32.fromBase58(f.shouldThrow.xprv),
|
||||
(f.shouldThrow as any).sighashTypes || undefined,
|
||||
);
|
||||
}, new RegExp('No inputs were signed'));
|
||||
assert.rejects(async () => {
|
||||
await assert.rejects(async () => {
|
||||
await (psbtThatShouldThrow.signAllInputsHDAsync as any)();
|
||||
}, new RegExp('Need HDSigner to sign input'));
|
||||
}
|
||||
|
|
|
@ -41,6 +41,21 @@ describe('script', () => {
|
|||
});
|
||||
});
|
||||
|
||||
describe('toASM', () => {
|
||||
const OP_RETURN = bscript.OPS.OP_RETURN;
|
||||
it('encodes empty buffer as OP_0', () => {
|
||||
const chunks = [OP_RETURN, Buffer.from([])];
|
||||
assert.strictEqual(bscript.toASM(chunks), 'OP_RETURN OP_0');
|
||||
});
|
||||
|
||||
for (let i = 1; i <= 16; i++) {
|
||||
it(`encodes one byte buffer [${i}] as OP_${i}`, () => {
|
||||
const chunks = [OP_RETURN, Buffer.from([i])];
|
||||
assert.strictEqual(bscript.toASM(chunks), 'OP_RETURN OP_' + i);
|
||||
});
|
||||
}
|
||||
});
|
||||
|
||||
describe('fromASM/toASM (templates)', () => {
|
||||
fixtures2.valid.forEach(f => {
|
||||
if (f.inputHex) {
|
||||
|
|
|
@ -54,4 +54,41 @@ describe('types', () => {
|
|||
});
|
||||
});
|
||||
});
|
||||
|
||||
describe('UInt31', () => {
|
||||
const UINT31_MAX = Math.pow(2, 31) - 1;
|
||||
it('return true for valid values', () => {
|
||||
assert.strictEqual(types.UInt31(0), true);
|
||||
assert.strictEqual(types.UInt31(1000), true);
|
||||
assert.strictEqual(types.UInt31(UINT31_MAX), true);
|
||||
});
|
||||
|
||||
it('return false for negative values', () => {
|
||||
assert.strictEqual(types.UInt31(-1), false);
|
||||
assert.strictEqual(types.UInt31(-UINT31_MAX), false);
|
||||
});
|
||||
|
||||
it(`return false for value > ${UINT31_MAX}`, () => {
|
||||
assert.strictEqual(types.UInt31(UINT31_MAX + 1), false);
|
||||
});
|
||||
});
|
||||
|
||||
describe('BIP32Path', () => {
|
||||
it('return true for valid paths', () => {
|
||||
assert.strictEqual(types.BIP32Path("m/0'/0'"), true);
|
||||
assert.strictEqual(types.BIP32Path("m/0'/0"), true);
|
||||
assert.strictEqual(types.BIP32Path("m/0'/1'/2'/3/4'"), true);
|
||||
});
|
||||
|
||||
it('return false for invalid paths', () => {
|
||||
assert.strictEqual(types.BIP32Path('m'), false);
|
||||
assert.strictEqual(types.BIP32Path("n/0'/0'"), false);
|
||||
assert.strictEqual(types.BIP32Path("m/0'/x"), false);
|
||||
});
|
||||
|
||||
it('return "BIP32 derivation path" for JSON.strigify()', () => {
|
||||
const toJsonValue = JSON.stringify(types.BIP32Path);
|
||||
assert.equal(toJsonValue, '"BIP32 derivation path"');
|
||||
});
|
||||
});
|
||||
});
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue