2019-09-07 06:42:03 +02:00
|
|
|
import * as assert from 'assert';
|
|
|
|
import { describe, it } from 'mocha';
|
2019-06-26 13:20:49 +02:00
|
|
|
|
2020-05-31 03:13:16 +02:00
|
|
|
import {
|
|
|
|
bip32,
|
|
|
|
ECPair,
|
|
|
|
networks as NETWORKS,
|
|
|
|
Psbt,
|
|
|
|
Signer,
|
|
|
|
SignerAsync,
|
|
|
|
} from '..';
|
2019-06-26 13:20:49 +02:00
|
|
|
|
2019-09-07 06:42:03 +02:00
|
|
|
import * as preFixtures from './fixtures/psbt.json';
|
2019-07-09 09:35:40 +02:00
|
|
|
|
2019-09-07 06:42:03 +02:00
|
|
|
const initBuffers = (object: any): typeof preFixtures =>
|
|
|
|
JSON.parse(JSON.stringify(object), (_, value) => {
|
|
|
|
const regex = new RegExp(/^Buffer.from\(['"](.*)['"], ['"](.*)['"]\)$/);
|
|
|
|
const result = regex.exec(value);
|
|
|
|
if (!result) return value;
|
2019-07-09 09:35:40 +02:00
|
|
|
|
2019-09-07 06:42:03 +02:00
|
|
|
const data = result[1];
|
|
|
|
const encoding = result[2];
|
2019-07-09 09:35:40 +02:00
|
|
|
|
2019-09-12 10:47:39 +02:00
|
|
|
return Buffer.from(data, encoding as BufferEncoding);
|
2019-09-07 06:42:03 +02:00
|
|
|
});
|
2019-06-26 13:20:49 +02:00
|
|
|
|
2019-09-07 06:42:03 +02:00
|
|
|
const fixtures = initBuffers(preFixtures);
|
2019-07-04 09:53:51 +02:00
|
|
|
|
2019-09-12 10:35:08 +02:00
|
|
|
const upperCaseFirstLetter = (str: string): string =>
|
2019-09-07 06:42:03 +02:00
|
|
|
str.replace(/^./, s => s.toUpperCase());
|
|
|
|
|
2020-05-31 03:13:16 +02:00
|
|
|
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);
|
|
|
|
},
|
|
|
|
);
|
|
|
|
},
|
|
|
|
};
|
|
|
|
};
|
2019-09-07 06:42:03 +02:00
|
|
|
// const b = (hex: string) => Buffer.from(hex, 'hex');
|
2019-07-04 09:53:51 +02:00
|
|
|
|
2019-06-26 13:20:49 +02:00
|
|
|
describe(`Psbt`, () => {
|
2019-07-03 11:40:37 +02:00
|
|
|
describe('BIP174 Test Vectors', () => {
|
2019-07-03 12:20:27 +02:00
|
|
|
fixtures.bip174.invalid.forEach(f => {
|
|
|
|
it(`Invalid: ${f.description}`, () => {
|
2019-07-03 11:40:37 +02:00
|
|
|
assert.throws(() => {
|
2019-09-07 06:42:03 +02:00
|
|
|
Psbt.fromBase64(f.psbt);
|
|
|
|
}, new RegExp(f.errorMessage));
|
|
|
|
});
|
|
|
|
});
|
2019-07-03 12:02:02 +02:00
|
|
|
|
2019-07-03 12:24:43 +02:00
|
|
|
fixtures.bip174.valid.forEach(f => {
|
|
|
|
it(`Valid: ${f.description}`, () => {
|
2019-07-03 12:02:02 +02:00
|
|
|
assert.doesNotThrow(() => {
|
2019-09-07 06:42:03 +02:00
|
|
|
Psbt.fromBase64(f.psbt);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
2019-07-03 12:38:09 +02:00
|
|
|
|
|
|
|
fixtures.bip174.failSignChecks.forEach(f => {
|
2019-09-07 06:42:03 +02:00
|
|
|
const keyPair = ECPair.makeRandom();
|
2019-07-03 12:38:09 +02:00
|
|
|
it(`Fails Signer checks: ${f.description}`, () => {
|
2019-09-07 06:42:03 +02:00
|
|
|
const psbt = Psbt.fromBase64(f.psbt);
|
2019-07-03 12:38:09 +02:00
|
|
|
assert.throws(() => {
|
2019-09-07 06:42:03 +02:00
|
|
|
psbt.signInput(f.inputToCheck, keyPair);
|
|
|
|
}, new RegExp(f.errorMessage));
|
|
|
|
});
|
|
|
|
});
|
2019-07-04 08:47:18 +02:00
|
|
|
|
|
|
|
fixtures.bip174.creator.forEach(f => {
|
|
|
|
it('Creates expected PSBT', () => {
|
2019-09-07 06:42:03 +02:00
|
|
|
const psbt = new Psbt();
|
2019-07-04 08:47:18 +02:00
|
|
|
for (const input of f.inputs) {
|
2019-09-07 06:42:03 +02:00
|
|
|
psbt.addInput(input);
|
2019-07-04 08:47:18 +02:00
|
|
|
}
|
|
|
|
for (const output of f.outputs) {
|
|
|
|
const script = Buffer.from(output.script, 'hex');
|
2019-09-07 06:42:03 +02:00
|
|
|
psbt.addOutput({ ...output, script });
|
2019-07-04 08:47:18 +02:00
|
|
|
}
|
2019-09-07 06:42:03 +02:00
|
|
|
assert.strictEqual(psbt.toBase64(), f.result);
|
|
|
|
});
|
|
|
|
});
|
2019-07-04 09:53:51 +02:00
|
|
|
|
|
|
|
fixtures.bip174.updater.forEach(f => {
|
|
|
|
it('Updates PSBT to the expected result', () => {
|
2019-09-07 06:42:03 +02:00
|
|
|
const psbt = Psbt.fromBase64(f.psbt);
|
2019-07-04 09:53:51 +02:00
|
|
|
|
|
|
|
for (const inputOrOutput of ['input', 'output']) {
|
2019-09-07 06:42:03 +02:00
|
|
|
const fixtureData = (f as any)[`${inputOrOutput}Data`];
|
2019-07-04 09:53:51 +02:00
|
|
|
if (fixtureData) {
|
|
|
|
for (const [i, data] of fixtureData.entries()) {
|
2019-09-07 06:42:03 +02:00
|
|
|
const txt = upperCaseFirstLetter(inputOrOutput);
|
|
|
|
(psbt as any)[`update${txt}`](i, data);
|
2019-07-04 09:53:51 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-09-07 06:42:03 +02:00
|
|
|
assert.strictEqual(psbt.toBase64(), f.result);
|
|
|
|
});
|
|
|
|
});
|
2019-07-04 10:32:16 +02:00
|
|
|
|
2019-07-10 13:54:05 +02:00
|
|
|
fixtures.bip174.signer.forEach(f => {
|
|
|
|
it('Signs PSBT to the expected result', () => {
|
2019-09-07 06:42:03 +02:00
|
|
|
const psbt = Psbt.fromBase64(f.psbt);
|
2019-07-11 03:11:51 +02:00
|
|
|
|
2019-09-07 06:42:03 +02:00
|
|
|
f.keys.forEach(({ inputToSign, WIF }) => {
|
2019-07-10 13:54:05 +02:00
|
|
|
const keyPair = ECPair.fromWIF(WIF, NETWORKS.testnet);
|
|
|
|
psbt.signInput(inputToSign, keyPair);
|
2019-09-07 06:42:03 +02:00
|
|
|
});
|
2019-07-11 03:11:51 +02:00
|
|
|
|
2019-09-07 06:42:03 +02:00
|
|
|
assert.strictEqual(psbt.toBase64(), f.result);
|
|
|
|
});
|
|
|
|
});
|
2019-07-11 03:11:51 +02:00
|
|
|
|
2019-07-10 13:54:05 +02:00
|
|
|
fixtures.bip174.combiner.forEach(f => {
|
|
|
|
it('Combines two PSBTs to the expected result', () => {
|
2019-09-07 06:42:03 +02:00
|
|
|
const psbts = f.psbts.map(psbt => Psbt.fromBase64(psbt));
|
2019-07-11 03:11:51 +02:00
|
|
|
|
2019-09-07 06:42:03 +02:00
|
|
|
psbts[0].combine(psbts[1]);
|
2019-07-11 03:11:51 +02:00
|
|
|
|
2019-07-10 13:54:05 +02:00
|
|
|
// Produces a different Base64 string due to implemetation specific key-value ordering.
|
|
|
|
// That means this test will fail:
|
|
|
|
// assert.strictEqual(psbts[0].toBase64(), f.result)
|
|
|
|
// However, if we compare the actual PSBT properties we can see they are logically identical:
|
2019-09-07 06:42:03 +02:00
|
|
|
assert.deepStrictEqual(psbts[0], Psbt.fromBase64(f.result));
|
|
|
|
});
|
|
|
|
});
|
2019-07-11 03:11:51 +02:00
|
|
|
|
2019-07-10 13:54:05 +02:00
|
|
|
fixtures.bip174.finalizer.forEach(f => {
|
2019-09-07 06:42:03 +02:00
|
|
|
it('Finalizes inputs and gives the expected PSBT', () => {
|
|
|
|
const psbt = Psbt.fromBase64(f.psbt);
|
2019-07-11 03:11:51 +02:00
|
|
|
|
2019-09-07 06:42:03 +02:00
|
|
|
psbt.finalizeAllInputs();
|
2019-07-11 03:11:51 +02:00
|
|
|
|
2019-09-07 06:42:03 +02:00
|
|
|
assert.strictEqual(psbt.toBase64(), f.result);
|
|
|
|
});
|
|
|
|
});
|
2019-07-11 03:11:51 +02:00
|
|
|
|
2019-07-10 13:54:05 +02:00
|
|
|
fixtures.bip174.extractor.forEach(f => {
|
|
|
|
it('Extracts the expected transaction from a PSBT', () => {
|
2019-09-07 06:42:03 +02:00
|
|
|
const psbt1 = Psbt.fromBase64(f.psbt);
|
|
|
|
const transaction1 = psbt1.extractTransaction(true).toHex();
|
2019-07-11 03:11:51 +02:00
|
|
|
|
2019-09-07 06:42:03 +02:00
|
|
|
const psbt2 = Psbt.fromBase64(f.psbt);
|
|
|
|
const transaction2 = psbt2.extractTransaction().toHex();
|
2019-07-11 03:11:51 +02:00
|
|
|
|
2019-09-07 06:42:03 +02:00
|
|
|
assert.strictEqual(transaction1, transaction2);
|
|
|
|
assert.strictEqual(transaction1, f.transaction);
|
2019-07-11 03:11:51 +02:00
|
|
|
|
2019-09-07 06:42:03 +02:00
|
|
|
const psbt3 = Psbt.fromBase64(f.psbt);
|
|
|
|
delete psbt3.data.inputs[0].finalScriptSig;
|
|
|
|
delete psbt3.data.inputs[0].finalScriptWitness;
|
2019-07-10 13:54:05 +02:00
|
|
|
assert.throws(() => {
|
2019-09-07 06:42:03 +02:00
|
|
|
psbt3.extractTransaction();
|
|
|
|
}, new RegExp('Not finalized'));
|
2019-07-11 03:11:51 +02:00
|
|
|
|
2019-09-07 06:42:03 +02:00
|
|
|
const psbt4 = Psbt.fromBase64(f.psbt);
|
|
|
|
psbt4.setMaximumFeeRate(1);
|
2019-07-10 13:54:05 +02:00
|
|
|
assert.throws(() => {
|
2019-09-07 06:42:03 +02:00
|
|
|
psbt4.extractTransaction();
|
|
|
|
}, new RegExp('Warning: You are paying around [\\d.]+ in fees'));
|
|
|
|
|
|
|
|
const psbt5 = Psbt.fromBase64(f.psbt);
|
|
|
|
psbt5.extractTransaction(true);
|
|
|
|
const fr1 = psbt5.getFeeRate();
|
|
|
|
const fr2 = psbt5.getFeeRate();
|
|
|
|
assert.strictEqual(fr1, fr2);
|
|
|
|
|
|
|
|
const psbt6 = Psbt.fromBase64(f.psbt);
|
|
|
|
const f1 = psbt6.getFee();
|
|
|
|
const f2 = psbt6.getFee();
|
|
|
|
assert.strictEqual(f1, f2);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
2019-07-05 09:42:13 +02:00
|
|
|
|
|
|
|
describe('signInputAsync', () => {
|
|
|
|
fixtures.signInput.checks.forEach(f => {
|
|
|
|
it(f.description, async () => {
|
2019-07-11 11:24:35 +02:00
|
|
|
if (f.shouldSign) {
|
2019-09-07 06:42:03 +02:00
|
|
|
const psbtThatShouldsign = Psbt.fromBase64(f.shouldSign.psbt);
|
2020-05-31 03:13:16 +02:00
|
|
|
await assert.doesNotReject(async () => {
|
2019-07-11 11:24:35 +02:00
|
|
|
await psbtThatShouldsign.signInputAsync(
|
|
|
|
f.shouldSign.inputToCheck,
|
|
|
|
ECPair.fromWIF(f.shouldSign.WIF),
|
|
|
|
f.shouldSign.sighashTypes || undefined,
|
2019-09-07 06:42:03 +02:00
|
|
|
);
|
|
|
|
});
|
2020-05-31 03:13:16 +02:00
|
|
|
await assert.rejects(async () => {
|
|
|
|
await psbtThatShouldsign.signInputAsync(
|
|
|
|
f.shouldSign.inputToCheck,
|
|
|
|
failedAsyncSigner(ECPair.fromWIF(f.shouldSign.WIF).publicKey),
|
|
|
|
f.shouldSign.sighashTypes || undefined,
|
|
|
|
);
|
|
|
|
}, /sign failed/);
|
2019-07-11 11:24:35 +02:00
|
|
|
}
|
2019-07-04 11:23:55 +02:00
|
|
|
|
2019-07-11 11:24:35 +02:00
|
|
|
if (f.shouldThrow) {
|
2019-09-07 06:42:03 +02:00
|
|
|
const psbtThatShouldThrow = Psbt.fromBase64(f.shouldThrow.psbt);
|
2020-05-31 03:13:16 +02:00
|
|
|
await assert.rejects(async () => {
|
2019-07-11 11:24:35 +02:00
|
|
|
await psbtThatShouldThrow.signInputAsync(
|
|
|
|
f.shouldThrow.inputToCheck,
|
|
|
|
ECPair.fromWIF(f.shouldThrow.WIF),
|
2019-09-07 06:42:03 +02:00
|
|
|
(f.shouldThrow as any).sighashTypes || undefined,
|
|
|
|
);
|
|
|
|
}, new RegExp(f.shouldThrow.errorMessage));
|
2020-05-31 03:13:16 +02:00
|
|
|
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 () => {
|
2019-09-07 06:42:03 +02:00
|
|
|
await (psbtThatShouldThrow.signInputAsync as any)(
|
2019-07-11 11:24:35 +02:00
|
|
|
f.shouldThrow.inputToCheck,
|
2019-09-07 06:42:03 +02:00
|
|
|
);
|
|
|
|
}, new RegExp('Need Signer to sign input'));
|
2019-07-11 11:24:35 +02:00
|
|
|
}
|
2019-09-07 06:42:03 +02:00
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
2019-07-04 11:23:55 +02:00
|
|
|
|
2019-06-26 13:20:49 +02:00
|
|
|
describe('signInput', () => {
|
2019-06-28 11:55:00 +02:00
|
|
|
fixtures.signInput.checks.forEach(f => {
|
|
|
|
it(f.description, () => {
|
2019-07-11 11:24:35 +02:00
|
|
|
if (f.shouldSign) {
|
2019-09-07 06:42:03 +02:00
|
|
|
const psbtThatShouldsign = Psbt.fromBase64(f.shouldSign.psbt);
|
2019-07-11 11:24:35 +02:00
|
|
|
assert.doesNotThrow(() => {
|
|
|
|
psbtThatShouldsign.signInput(
|
|
|
|
f.shouldSign.inputToCheck,
|
|
|
|
ECPair.fromWIF(f.shouldSign.WIF),
|
|
|
|
f.shouldSign.sighashTypes || undefined,
|
2019-09-07 06:42:03 +02:00
|
|
|
);
|
|
|
|
});
|
2019-07-11 11:24:35 +02:00
|
|
|
}
|
2019-07-05 05:51:13 +02:00
|
|
|
|
2019-07-11 11:24:35 +02:00
|
|
|
if (f.shouldThrow) {
|
2019-09-07 06:42:03 +02:00
|
|
|
const psbtThatShouldThrow = Psbt.fromBase64(f.shouldThrow.psbt);
|
2019-07-11 11:24:35 +02:00
|
|
|
assert.throws(() => {
|
|
|
|
psbtThatShouldThrow.signInput(
|
|
|
|
f.shouldThrow.inputToCheck,
|
|
|
|
ECPair.fromWIF(f.shouldThrow.WIF),
|
2019-09-07 06:42:03 +02:00
|
|
|
(f.shouldThrow as any).sighashTypes || undefined,
|
|
|
|
);
|
|
|
|
}, new RegExp(f.shouldThrow.errorMessage));
|
2019-07-11 11:24:35 +02:00
|
|
|
assert.throws(() => {
|
2019-09-07 06:42:03 +02:00
|
|
|
(psbtThatShouldThrow.signInput as any)(f.shouldThrow.inputToCheck);
|
|
|
|
}, new RegExp('Need Signer to sign input'));
|
2019-07-11 11:24:35 +02:00
|
|
|
}
|
2019-09-07 06:42:03 +02:00
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
2019-07-04 12:00:01 +02:00
|
|
|
|
2019-07-19 10:21:31 +02:00
|
|
|
describe('signAllInputsAsync', () => {
|
2019-07-08 10:40:21 +02:00
|
|
|
fixtures.signInput.checks.forEach(f => {
|
2019-09-07 06:42:03 +02:00
|
|
|
if (f.description === 'checks the input exists') return;
|
2019-07-08 10:40:21 +02:00
|
|
|
it(f.description, async () => {
|
2019-07-11 11:24:35 +02:00
|
|
|
if (f.shouldSign) {
|
2019-09-07 06:42:03 +02:00
|
|
|
const psbtThatShouldsign = Psbt.fromBase64(f.shouldSign.psbt);
|
2020-05-31 03:13:16 +02:00
|
|
|
await assert.doesNotReject(async () => {
|
2019-07-19 10:21:31 +02:00
|
|
|
await psbtThatShouldsign.signAllInputsAsync(
|
2019-07-11 11:24:35 +02:00
|
|
|
ECPair.fromWIF(f.shouldSign.WIF),
|
|
|
|
f.shouldSign.sighashTypes || undefined,
|
2019-09-07 06:42:03 +02:00
|
|
|
);
|
|
|
|
});
|
2019-07-11 11:24:35 +02:00
|
|
|
}
|
2019-07-08 10:40:21 +02:00
|
|
|
|
2019-07-11 11:24:35 +02:00
|
|
|
if (f.shouldThrow) {
|
2019-09-07 06:42:03 +02:00
|
|
|
const psbtThatShouldThrow = Psbt.fromBase64(f.shouldThrow.psbt);
|
2020-05-31 03:13:16 +02:00
|
|
|
await assert.rejects(async () => {
|
2019-07-19 10:21:31 +02:00
|
|
|
await psbtThatShouldThrow.signAllInputsAsync(
|
2019-07-11 11:24:35 +02:00
|
|
|
ECPair.fromWIF(f.shouldThrow.WIF),
|
2019-09-07 06:42:03 +02:00
|
|
|
(f.shouldThrow as any).sighashTypes || undefined,
|
|
|
|
);
|
|
|
|
}, new RegExp('No inputs were signed'));
|
2020-05-31 03:13:16 +02:00
|
|
|
await assert.rejects(async () => {
|
2019-09-07 06:42:03 +02:00
|
|
|
await (psbtThatShouldThrow.signAllInputsAsync as any)();
|
|
|
|
}, new RegExp('Need Signer to sign input'));
|
2019-07-11 11:24:35 +02:00
|
|
|
}
|
2019-09-07 06:42:03 +02:00
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
2019-07-08 10:40:21 +02:00
|
|
|
|
2019-07-19 07:53:54 +02:00
|
|
|
describe('signAllInputs', () => {
|
2019-07-08 10:40:21 +02:00
|
|
|
fixtures.signInput.checks.forEach(f => {
|
2019-09-07 06:42:03 +02:00
|
|
|
if (f.description === 'checks the input exists') return;
|
2019-07-08 10:40:21 +02:00
|
|
|
it(f.description, () => {
|
2019-07-11 11:24:35 +02:00
|
|
|
if (f.shouldSign) {
|
2019-09-07 06:42:03 +02:00
|
|
|
const psbtThatShouldsign = Psbt.fromBase64(f.shouldSign.psbt);
|
2019-07-11 11:24:35 +02:00
|
|
|
assert.doesNotThrow(() => {
|
2019-07-19 07:53:54 +02:00
|
|
|
psbtThatShouldsign.signAllInputs(
|
2019-07-11 11:24:35 +02:00
|
|
|
ECPair.fromWIF(f.shouldSign.WIF),
|
|
|
|
f.shouldSign.sighashTypes || undefined,
|
2019-09-07 06:42:03 +02:00
|
|
|
);
|
|
|
|
});
|
2019-07-11 11:24:35 +02:00
|
|
|
}
|
2019-07-08 10:40:21 +02:00
|
|
|
|
2019-07-11 11:24:35 +02:00
|
|
|
if (f.shouldThrow) {
|
2019-09-07 06:42:03 +02:00
|
|
|
const psbtThatShouldThrow = Psbt.fromBase64(f.shouldThrow.psbt);
|
2019-07-11 11:24:35 +02:00
|
|
|
assert.throws(() => {
|
2019-07-19 07:53:54 +02:00
|
|
|
psbtThatShouldThrow.signAllInputs(
|
2019-07-11 11:24:35 +02:00
|
|
|
ECPair.fromWIF(f.shouldThrow.WIF),
|
2019-09-07 06:42:03 +02:00
|
|
|
(f.shouldThrow as any).sighashTypes || undefined,
|
|
|
|
);
|
|
|
|
}, new RegExp('No inputs were signed'));
|
2019-07-11 11:24:35 +02:00
|
|
|
assert.throws(() => {
|
2019-09-07 06:42:03 +02:00
|
|
|
(psbtThatShouldThrow.signAllInputs as any)();
|
|
|
|
}, new RegExp('Need Signer to sign input'));
|
2019-07-11 11:24:35 +02:00
|
|
|
}
|
2019-09-07 06:42:03 +02:00
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
2019-07-08 10:40:21 +02:00
|
|
|
|
2019-07-19 04:42:45 +02:00
|
|
|
describe('signInputHDAsync', () => {
|
|
|
|
fixtures.signInputHD.checks.forEach(f => {
|
|
|
|
it(f.description, async () => {
|
|
|
|
if (f.shouldSign) {
|
2019-09-07 06:42:03 +02:00
|
|
|
const psbtThatShouldsign = Psbt.fromBase64(f.shouldSign.psbt);
|
2020-05-31 03:13:16 +02:00
|
|
|
await assert.doesNotReject(async () => {
|
2019-07-19 04:42:45 +02:00
|
|
|
await psbtThatShouldsign.signInputHDAsync(
|
|
|
|
f.shouldSign.inputToCheck,
|
|
|
|
bip32.fromBase58(f.shouldSign.xprv),
|
2019-09-07 06:42:03 +02:00
|
|
|
(f.shouldSign as any).sighashTypes || undefined,
|
|
|
|
);
|
|
|
|
});
|
2019-07-19 04:42:45 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if (f.shouldThrow) {
|
2019-09-07 06:42:03 +02:00
|
|
|
const psbtThatShouldThrow = Psbt.fromBase64(f.shouldThrow.psbt);
|
2020-05-31 03:13:16 +02:00
|
|
|
await assert.rejects(async () => {
|
2019-07-19 04:42:45 +02:00
|
|
|
await psbtThatShouldThrow.signInputHDAsync(
|
|
|
|
f.shouldThrow.inputToCheck,
|
|
|
|
bip32.fromBase58(f.shouldThrow.xprv),
|
2019-09-07 06:42:03 +02:00
|
|
|
(f.shouldThrow as any).sighashTypes || undefined,
|
|
|
|
);
|
|
|
|
}, new RegExp(f.shouldThrow.errorMessage));
|
2020-05-31 03:13:16 +02:00
|
|
|
await assert.rejects(async () => {
|
2019-09-07 06:42:03 +02:00
|
|
|
await (psbtThatShouldThrow.signInputHDAsync as any)(
|
2019-07-19 04:42:45 +02:00
|
|
|
f.shouldThrow.inputToCheck,
|
2019-09-07 06:42:03 +02:00
|
|
|
);
|
|
|
|
}, new RegExp('Need HDSigner to sign input'));
|
2019-07-19 04:42:45 +02:00
|
|
|
}
|
2019-09-07 06:42:03 +02:00
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
2019-07-19 04:42:45 +02:00
|
|
|
|
|
|
|
describe('signInputHD', () => {
|
|
|
|
fixtures.signInputHD.checks.forEach(f => {
|
|
|
|
it(f.description, () => {
|
|
|
|
if (f.shouldSign) {
|
2019-09-07 06:42:03 +02:00
|
|
|
const psbtThatShouldsign = Psbt.fromBase64(f.shouldSign.psbt);
|
2019-07-19 04:42:45 +02:00
|
|
|
assert.doesNotThrow(() => {
|
|
|
|
psbtThatShouldsign.signInputHD(
|
|
|
|
f.shouldSign.inputToCheck,
|
|
|
|
bip32.fromBase58(f.shouldSign.xprv),
|
2019-09-07 06:42:03 +02:00
|
|
|
(f.shouldSign as any).sighashTypes || undefined,
|
|
|
|
);
|
|
|
|
});
|
2019-07-19 04:42:45 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if (f.shouldThrow) {
|
2019-09-07 06:42:03 +02:00
|
|
|
const psbtThatShouldThrow = Psbt.fromBase64(f.shouldThrow.psbt);
|
2019-07-19 04:42:45 +02:00
|
|
|
assert.throws(() => {
|
|
|
|
psbtThatShouldThrow.signInputHD(
|
|
|
|
f.shouldThrow.inputToCheck,
|
|
|
|
bip32.fromBase58(f.shouldThrow.xprv),
|
2019-09-07 06:42:03 +02:00
|
|
|
(f.shouldThrow as any).sighashTypes || undefined,
|
|
|
|
);
|
|
|
|
}, new RegExp(f.shouldThrow.errorMessage));
|
2019-07-19 04:42:45 +02:00
|
|
|
assert.throws(() => {
|
2019-09-07 06:42:03 +02:00
|
|
|
(psbtThatShouldThrow.signInputHD as any)(
|
2019-07-19 04:42:45 +02:00
|
|
|
f.shouldThrow.inputToCheck,
|
2019-09-07 06:42:03 +02:00
|
|
|
);
|
|
|
|
}, new RegExp('Need HDSigner to sign input'));
|
2019-07-19 04:42:45 +02:00
|
|
|
}
|
2019-09-07 06:42:03 +02:00
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
2019-07-19 04:42:45 +02:00
|
|
|
|
2019-07-19 10:21:31 +02:00
|
|
|
describe('signAllInputsHDAsync', () => {
|
2019-07-19 04:42:45 +02:00
|
|
|
fixtures.signInputHD.checks.forEach(f => {
|
|
|
|
it(f.description, async () => {
|
|
|
|
if (f.shouldSign) {
|
2019-09-07 06:42:03 +02:00
|
|
|
const psbtThatShouldsign = Psbt.fromBase64(f.shouldSign.psbt);
|
2020-05-31 03:13:16 +02:00
|
|
|
await assert.doesNotReject(async () => {
|
2019-07-19 10:21:31 +02:00
|
|
|
await psbtThatShouldsign.signAllInputsHDAsync(
|
2019-07-19 04:42:45 +02:00
|
|
|
bip32.fromBase58(f.shouldSign.xprv),
|
2019-09-07 06:42:03 +02:00
|
|
|
(f.shouldSign as any).sighashTypes || undefined,
|
|
|
|
);
|
|
|
|
});
|
2019-07-19 04:42:45 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if (f.shouldThrow) {
|
2019-09-07 06:42:03 +02:00
|
|
|
const psbtThatShouldThrow = Psbt.fromBase64(f.shouldThrow.psbt);
|
2020-05-31 03:13:16 +02:00
|
|
|
await assert.rejects(async () => {
|
2019-07-19 10:21:31 +02:00
|
|
|
await psbtThatShouldThrow.signAllInputsHDAsync(
|
2019-07-19 04:42:45 +02:00
|
|
|
bip32.fromBase58(f.shouldThrow.xprv),
|
2019-09-07 06:42:03 +02:00
|
|
|
(f.shouldThrow as any).sighashTypes || undefined,
|
|
|
|
);
|
|
|
|
}, new RegExp('No inputs were signed'));
|
2020-05-31 03:13:16 +02:00
|
|
|
await assert.rejects(async () => {
|
2019-09-07 06:42:03 +02:00
|
|
|
await (psbtThatShouldThrow.signAllInputsHDAsync as any)();
|
|
|
|
}, new RegExp('Need HDSigner to sign input'));
|
2019-07-19 04:42:45 +02:00
|
|
|
}
|
2019-09-07 06:42:03 +02:00
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
2019-07-19 04:42:45 +02:00
|
|
|
|
2019-07-19 07:53:54 +02:00
|
|
|
describe('signAllInputsHD', () => {
|
2019-07-19 04:42:45 +02:00
|
|
|
fixtures.signInputHD.checks.forEach(f => {
|
|
|
|
it(f.description, () => {
|
|
|
|
if (f.shouldSign) {
|
2019-09-07 06:42:03 +02:00
|
|
|
const psbtThatShouldsign = Psbt.fromBase64(f.shouldSign.psbt);
|
2019-07-19 04:42:45 +02:00
|
|
|
assert.doesNotThrow(() => {
|
2019-07-19 07:53:54 +02:00
|
|
|
psbtThatShouldsign.signAllInputsHD(
|
2019-07-19 04:42:45 +02:00
|
|
|
bip32.fromBase58(f.shouldSign.xprv),
|
2019-09-07 06:42:03 +02:00
|
|
|
(f.shouldSign as any).sighashTypes || undefined,
|
|
|
|
);
|
|
|
|
});
|
2019-07-19 04:42:45 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if (f.shouldThrow) {
|
2019-09-07 06:42:03 +02:00
|
|
|
const psbtThatShouldThrow = Psbt.fromBase64(f.shouldThrow.psbt);
|
2019-07-19 04:42:45 +02:00
|
|
|
assert.throws(() => {
|
2019-07-19 07:53:54 +02:00
|
|
|
psbtThatShouldThrow.signAllInputsHD(
|
2019-07-19 04:42:45 +02:00
|
|
|
bip32.fromBase58(f.shouldThrow.xprv),
|
2019-09-07 06:42:03 +02:00
|
|
|
(f.shouldThrow as any).sighashTypes || undefined,
|
|
|
|
);
|
|
|
|
}, new RegExp('No inputs were signed'));
|
2019-07-19 04:42:45 +02:00
|
|
|
assert.throws(() => {
|
2019-09-07 06:42:03 +02:00
|
|
|
(psbtThatShouldThrow.signAllInputsHD as any)();
|
|
|
|
}, new RegExp('Need HDSigner to sign input'));
|
2019-07-19 04:42:45 +02:00
|
|
|
}
|
2019-09-07 06:42:03 +02:00
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
2019-07-19 04:42:45 +02:00
|
|
|
|
2019-07-10 14:20:10 +02:00
|
|
|
describe('finalizeAllInputs', () => {
|
|
|
|
fixtures.finalizeAllInputs.forEach(f => {
|
|
|
|
it(`Finalizes inputs of type "${f.type}"`, () => {
|
2019-09-07 06:42:03 +02:00
|
|
|
const psbt = Psbt.fromBase64(f.psbt);
|
2019-07-10 14:20:10 +02:00
|
|
|
|
2019-09-07 06:42:03 +02:00
|
|
|
psbt.finalizeAllInputs();
|
2019-07-10 14:20:10 +02:00
|
|
|
|
2019-09-07 06:42:03 +02:00
|
|
|
assert.strictEqual(psbt.toBase64(), f.result);
|
|
|
|
});
|
|
|
|
});
|
2019-07-11 04:09:05 +02:00
|
|
|
it('fails if no script found', () => {
|
2019-09-07 06:42:03 +02:00
|
|
|
const psbt = new Psbt();
|
2019-07-11 04:09:05 +02:00
|
|
|
psbt.addInput({
|
2019-09-07 06:42:03 +02:00
|
|
|
hash:
|
|
|
|
'0000000000000000000000000000000000000000000000000000000000000000',
|
|
|
|
index: 0,
|
|
|
|
});
|
2019-07-11 04:09:05 +02:00
|
|
|
assert.throws(() => {
|
2019-09-07 06:42:03 +02:00
|
|
|
psbt.finalizeAllInputs();
|
|
|
|
}, new RegExp('No script found for input #0'));
|
2019-07-18 04:43:24 +02:00
|
|
|
psbt.updateInput(0, {
|
|
|
|
witnessUtxo: {
|
2019-09-07 06:42:03 +02:00
|
|
|
script: Buffer.from(
|
|
|
|
'0014d85c2b71d0060b09c9886aeb815e50991dda124d',
|
|
|
|
'hex',
|
|
|
|
),
|
|
|
|
value: 2e5,
|
|
|
|
},
|
|
|
|
});
|
2019-07-11 04:09:05 +02:00
|
|
|
assert.throws(() => {
|
2019-09-07 06:42:03 +02:00
|
|
|
psbt.finalizeAllInputs();
|
|
|
|
}, new RegExp('Can not finalize input #0'));
|
|
|
|
});
|
|
|
|
});
|
2019-07-10 14:20:10 +02:00
|
|
|
|
2019-07-05 09:42:13 +02:00
|
|
|
describe('addInput', () => {
|
|
|
|
fixtures.addInput.checks.forEach(f => {
|
|
|
|
it(f.description, () => {
|
2019-09-07 06:42:03 +02:00
|
|
|
const psbt = new Psbt();
|
2019-07-05 09:42:13 +02:00
|
|
|
|
|
|
|
if (f.exception) {
|
|
|
|
assert.throws(() => {
|
2019-09-07 06:42:03 +02:00
|
|
|
psbt.addInput(f.inputData as any);
|
|
|
|
}, new RegExp(f.exception));
|
2019-07-11 04:09:05 +02:00
|
|
|
assert.throws(() => {
|
2019-09-07 06:42:03 +02:00
|
|
|
psbt.addInputs([f.inputData as any]);
|
|
|
|
}, new RegExp(f.exception));
|
2019-07-05 09:42:13 +02:00
|
|
|
} else {
|
|
|
|
assert.doesNotThrow(() => {
|
2019-09-07 06:42:03 +02:00
|
|
|
psbt.addInputs([f.inputData as any]);
|
2019-07-05 09:42:13 +02:00
|
|
|
if (f.equals) {
|
2019-09-07 06:42:03 +02:00
|
|
|
assert.strictEqual(psbt.toBase64(), f.equals);
|
2019-07-05 09:42:13 +02:00
|
|
|
}
|
2019-09-07 06:42:03 +02:00
|
|
|
});
|
2019-07-05 11:26:52 +02:00
|
|
|
assert.throws(() => {
|
2019-09-07 06:42:03 +02:00
|
|
|
psbt.addInput(f.inputData as any);
|
|
|
|
}, new RegExp('Duplicate input detected.'));
|
2019-07-05 09:42:13 +02:00
|
|
|
}
|
2019-09-07 06:42:03 +02:00
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
2019-07-05 09:42:13 +02:00
|
|
|
|
|
|
|
describe('addOutput', () => {
|
|
|
|
fixtures.addOutput.checks.forEach(f => {
|
|
|
|
it(f.description, () => {
|
2019-09-07 06:42:03 +02:00
|
|
|
const psbt = new Psbt();
|
2019-07-05 09:42:13 +02:00
|
|
|
|
|
|
|
if (f.exception) {
|
|
|
|
assert.throws(() => {
|
2019-09-07 06:42:03 +02:00
|
|
|
psbt.addOutput(f.outputData as any);
|
|
|
|
}, new RegExp(f.exception));
|
2019-07-11 04:09:05 +02:00
|
|
|
assert.throws(() => {
|
2019-09-07 06:42:03 +02:00
|
|
|
psbt.addOutputs([f.outputData as any]);
|
|
|
|
}, new RegExp(f.exception));
|
2019-07-05 09:42:13 +02:00
|
|
|
} else {
|
|
|
|
assert.doesNotThrow(() => {
|
2019-09-07 06:42:03 +02:00
|
|
|
psbt.addOutput(f.outputData as any);
|
|
|
|
});
|
2019-07-11 04:09:05 +02:00
|
|
|
assert.doesNotThrow(() => {
|
2019-09-07 06:42:03 +02:00
|
|
|
psbt.addOutputs([f.outputData as any]);
|
|
|
|
});
|
2019-07-05 09:42:13 +02:00
|
|
|
}
|
2019-09-07 06:42:03 +02:00
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
2019-07-05 09:42:13 +02:00
|
|
|
|
2019-07-04 12:00:20 +02:00
|
|
|
describe('setVersion', () => {
|
|
|
|
it('Sets the version value of the unsigned transaction', () => {
|
2019-09-07 06:42:03 +02:00
|
|
|
const psbt = new Psbt();
|
2019-07-04 12:00:20 +02:00
|
|
|
|
2019-09-07 06:42:03 +02:00
|
|
|
assert.strictEqual(psbt.extractTransaction().version, 2);
|
|
|
|
psbt.setVersion(1);
|
|
|
|
assert.strictEqual(psbt.extractTransaction().version, 1);
|
|
|
|
});
|
|
|
|
});
|
2019-07-04 12:00:34 +02:00
|
|
|
|
|
|
|
describe('setLocktime', () => {
|
|
|
|
it('Sets the nLockTime value of the unsigned transaction', () => {
|
2019-09-07 06:42:03 +02:00
|
|
|
const psbt = new Psbt();
|
2019-07-04 12:00:34 +02:00
|
|
|
|
2019-09-07 06:42:03 +02:00
|
|
|
assert.strictEqual(psbt.extractTransaction().locktime, 0);
|
|
|
|
psbt.setLocktime(1);
|
|
|
|
assert.strictEqual(psbt.extractTransaction().locktime, 1);
|
|
|
|
});
|
|
|
|
});
|
2019-07-04 12:20:16 +02:00
|
|
|
|
2019-07-19 07:53:54 +02:00
|
|
|
describe('setInputSequence', () => {
|
2019-07-04 12:20:16 +02:00
|
|
|
it('Sets the sequence number for a given input', () => {
|
2019-09-07 06:42:03 +02:00
|
|
|
const psbt = new Psbt();
|
2019-07-04 12:20:16 +02:00
|
|
|
psbt.addInput({
|
2019-09-07 06:42:03 +02:00
|
|
|
hash:
|
|
|
|
'0000000000000000000000000000000000000000000000000000000000000000',
|
|
|
|
index: 0,
|
2019-07-04 12:20:16 +02:00
|
|
|
});
|
|
|
|
|
2019-09-07 06:42:03 +02:00
|
|
|
assert.strictEqual(psbt.inputCount, 1);
|
2020-04-26 13:07:09 +02:00
|
|
|
assert.strictEqual(psbt.txInputs[0].sequence, 0xffffffff);
|
2019-09-07 06:42:03 +02:00
|
|
|
psbt.setInputSequence(0, 0);
|
2020-04-26 13:07:09 +02:00
|
|
|
assert.strictEqual(psbt.txInputs[0].sequence, 0);
|
2019-09-07 06:42:03 +02:00
|
|
|
});
|
2019-07-04 12:20:16 +02:00
|
|
|
|
|
|
|
it('throws if input index is too high', () => {
|
2019-09-07 06:42:03 +02:00
|
|
|
const psbt = new Psbt();
|
2019-07-04 12:20:16 +02:00
|
|
|
psbt.addInput({
|
2019-09-07 06:42:03 +02:00
|
|
|
hash:
|
|
|
|
'0000000000000000000000000000000000000000000000000000000000000000',
|
|
|
|
index: 0,
|
2019-07-04 12:20:16 +02:00
|
|
|
});
|
|
|
|
|
|
|
|
assert.throws(() => {
|
2019-09-07 06:42:03 +02:00
|
|
|
psbt.setInputSequence(1, 0);
|
|
|
|
}, new RegExp('Input index too high'));
|
|
|
|
});
|
|
|
|
});
|
2019-07-11 04:09:05 +02:00
|
|
|
|
|
|
|
describe('clone', () => {
|
|
|
|
it('Should clone a psbt exactly with no reference', () => {
|
2019-09-07 06:42:03 +02:00
|
|
|
const f = fixtures.clone;
|
|
|
|
const psbt = Psbt.fromBase64(f.psbt);
|
|
|
|
const notAClone = Object.assign(new Psbt(), psbt); // references still active
|
|
|
|
const clone = psbt.clone();
|
|
|
|
|
|
|
|
assert.strictEqual(psbt.validateSignaturesOfAllInputs(), true);
|
|
|
|
|
|
|
|
assert.strictEqual(clone.toBase64(), psbt.toBase64());
|
|
|
|
assert.strictEqual(clone.toBase64(), notAClone.toBase64());
|
|
|
|
assert.strictEqual(psbt.toBase64(), notAClone.toBase64());
|
|
|
|
(psbt as any).__CACHE.__TX.version |= 0xff0000;
|
|
|
|
assert.notStrictEqual(clone.toBase64(), psbt.toBase64());
|
|
|
|
assert.notStrictEqual(clone.toBase64(), notAClone.toBase64());
|
|
|
|
assert.strictEqual(psbt.toBase64(), notAClone.toBase64());
|
|
|
|
});
|
|
|
|
});
|
2019-07-05 05:51:13 +02:00
|
|
|
|
2019-07-05 09:42:13 +02:00
|
|
|
describe('setMaximumFeeRate', () => {
|
|
|
|
it('Sets the maximumFeeRate value', () => {
|
2019-09-07 06:42:03 +02:00
|
|
|
const psbt = new Psbt();
|
2019-07-05 09:42:13 +02:00
|
|
|
|
2019-09-07 06:42:03 +02:00
|
|
|
assert.strictEqual((psbt as any).opts.maximumFeeRate, 5000);
|
|
|
|
psbt.setMaximumFeeRate(6000);
|
|
|
|
assert.strictEqual((psbt as any).opts.maximumFeeRate, 6000);
|
|
|
|
});
|
|
|
|
});
|
2019-07-05 09:42:13 +02:00
|
|
|
|
2019-07-19 07:53:54 +02:00
|
|
|
describe('validateSignaturesOfInput', () => {
|
2019-09-07 06:42:03 +02:00
|
|
|
const f = fixtures.validateSignaturesOfInput;
|
2019-07-10 12:59:11 +02:00
|
|
|
it('Correctly validates a signature', () => {
|
2019-09-07 06:42:03 +02:00
|
|
|
const psbt = Psbt.fromBase64(f.psbt);
|
2019-07-10 12:59:11 +02:00
|
|
|
|
2019-09-07 06:42:03 +02:00
|
|
|
assert.strictEqual(psbt.validateSignaturesOfInput(f.index), true);
|
2019-07-10 12:59:11 +02:00
|
|
|
assert.throws(() => {
|
2019-09-07 06:42:03 +02:00
|
|
|
psbt.validateSignaturesOfInput(f.nonExistantIndex);
|
|
|
|
}, new RegExp('No signatures to validate'));
|
|
|
|
});
|
2019-07-10 12:59:11 +02:00
|
|
|
|
|
|
|
it('Correctly validates a signature against a pubkey', () => {
|
2019-09-07 06:42:03 +02:00
|
|
|
const psbt = Psbt.fromBase64(f.psbt);
|
|
|
|
assert.strictEqual(
|
|
|
|
psbt.validateSignaturesOfInput(f.index, f.pubkey as any),
|
|
|
|
true,
|
|
|
|
);
|
2019-07-10 12:59:11 +02:00
|
|
|
assert.throws(() => {
|
2019-09-07 06:42:03 +02:00
|
|
|
psbt.validateSignaturesOfInput(f.index, f.incorrectPubkey as any);
|
|
|
|
}, new RegExp('No signatures for this pubkey'));
|
|
|
|
});
|
|
|
|
});
|
2019-07-10 12:59:11 +02:00
|
|
|
|
2019-07-10 13:05:30 +02:00
|
|
|
describe('getFeeRate', () => {
|
|
|
|
it('Throws error if called before inputs are finalized', () => {
|
2019-09-07 06:42:03 +02:00
|
|
|
const f = fixtures.getFeeRate;
|
|
|
|
const psbt = Psbt.fromBase64(f.psbt);
|
2019-07-10 13:05:30 +02:00
|
|
|
|
|
|
|
assert.throws(() => {
|
2019-09-07 06:42:03 +02:00
|
|
|
psbt.getFeeRate();
|
|
|
|
}, new RegExp('PSBT must be finalized to calculate fee rate'));
|
2019-07-10 13:05:30 +02:00
|
|
|
|
2019-09-07 06:42:03 +02:00
|
|
|
psbt.finalizeAllInputs();
|
2019-07-10 13:05:30 +02:00
|
|
|
|
2019-09-07 06:42:03 +02:00
|
|
|
assert.strictEqual(psbt.getFeeRate(), f.fee);
|
|
|
|
(psbt as any).__CACHE.__FEE_RATE = undefined;
|
|
|
|
assert.strictEqual(psbt.getFeeRate(), f.fee);
|
|
|
|
});
|
|
|
|
});
|
2019-07-10 13:05:30 +02:00
|
|
|
|
2019-07-08 10:40:21 +02:00
|
|
|
describe('create 1-to-1 transaction', () => {
|
2019-09-07 06:42:03 +02:00
|
|
|
const alice = ECPair.fromWIF(
|
|
|
|
'L2uPYXe17xSTqbCjZvL2DsyXPCbXspvcu5mHLDYUgzdUbZGSKrSr',
|
|
|
|
);
|
|
|
|
const psbt = new Psbt();
|
2019-07-08 10:40:21 +02:00
|
|
|
psbt.addInput({
|
|
|
|
hash: '7d067b4a697a09d2c3cff7d4d9506c9955e93bff41bf82d439da7d030382bc3e',
|
|
|
|
index: 0,
|
|
|
|
nonWitnessUtxo: Buffer.from(
|
|
|
|
'0200000001f9f34e95b9d5c8abcd20fc5bd4a825d1517be62f0f775e5f36da944d9' +
|
2019-09-07 06:42:03 +02:00
|
|
|
'452e550000000006b483045022100c86e9a111afc90f64b4904bd609e9eaed80d48' +
|
|
|
|
'ca17c162b1aca0a788ac3526f002207bb79b60d4fc6526329bf18a77135dc566020' +
|
|
|
|
'9e761da46e1c2f1152ec013215801210211755115eabf846720f5cb18f248666fec' +
|
|
|
|
'631e5e1e66009ce3710ceea5b1ad13ffffffff01905f0100000000001976a9148bb' +
|
|
|
|
'c95d2709c71607c60ee3f097c1217482f518d88ac00000000',
|
2019-07-08 10:40:21 +02:00
|
|
|
'hex',
|
|
|
|
),
|
|
|
|
sighashType: 1,
|
2019-09-07 06:42:03 +02:00
|
|
|
});
|
2019-07-08 10:40:21 +02:00
|
|
|
psbt.addOutput({
|
|
|
|
address: '1KRMKfeZcmosxALVYESdPNez1AP1mEtywp',
|
2019-09-07 06:42:03 +02:00
|
|
|
value: 80000,
|
|
|
|
});
|
|
|
|
psbt.signInput(0, alice);
|
2019-07-08 10:40:21 +02:00
|
|
|
assert.throws(() => {
|
2019-09-07 06:42:03 +02:00
|
|
|
psbt.setVersion(3);
|
|
|
|
}, new RegExp('Can not modify transaction, signatures exist.'));
|
|
|
|
psbt.validateSignaturesOfInput(0);
|
|
|
|
psbt.finalizeAllInputs();
|
2019-07-23 07:59:54 +02:00
|
|
|
assert.throws(() => {
|
2019-09-07 06:42:03 +02:00
|
|
|
psbt.setVersion(3);
|
|
|
|
}, new RegExp('Can not modify transaction, signatures exist.'));
|
2019-07-08 10:40:21 +02:00
|
|
|
assert.strictEqual(
|
|
|
|
psbt.extractTransaction().toHex(),
|
|
|
|
'02000000013ebc8203037dda39d482bf41ff3be955996c50d9d4f7cfc3d2097a694a7' +
|
2019-09-07 06:42:03 +02:00
|
|
|
'b067d000000006b483045022100931b6db94aed25d5486884d83fc37160f37f3368c0' +
|
|
|
|
'd7f48c757112abefec983802205fda64cff98c849577026eb2ce916a50ea70626a766' +
|
|
|
|
'9f8596dd89b720a26b4d501210365db9da3f8a260078a7e8f8b708a1161468fb2323f' +
|
|
|
|
'fda5ec16b261ec1056f455ffffffff0180380100000000001976a914ca0d36044e0dc' +
|
|
|
|
'08a22724efa6f6a07b0ec4c79aa88ac00000000',
|
|
|
|
);
|
|
|
|
});
|
2019-07-08 10:40:21 +02:00
|
|
|
|
2019-07-05 05:51:13 +02:00
|
|
|
describe('Method return types', () => {
|
|
|
|
it('fromBuffer returns Psbt type (not base class)', () => {
|
2019-09-07 06:42:03 +02:00
|
|
|
const psbt = Psbt.fromBuffer(
|
|
|
|
Buffer.from(
|
|
|
|
'70736274ff01000a01000000000000000000000000',
|
2019-09-12 10:35:08 +02:00
|
|
|
'hex', // cHNidP8BAAoBAAAAAAAAAAAAAAAA
|
2019-09-07 06:42:03 +02:00
|
|
|
),
|
|
|
|
);
|
2019-07-05 05:51:13 +02:00
|
|
|
assert.strictEqual(psbt instanceof Psbt, true);
|
2019-09-07 06:42:03 +02:00
|
|
|
assert.ok((psbt as any).__CACHE.__TX);
|
|
|
|
});
|
2019-07-05 05:51:13 +02:00
|
|
|
it('fromBase64 returns Psbt type (not base class)', () => {
|
|
|
|
const psbt = Psbt.fromBase64('cHNidP8BAAoBAAAAAAAAAAAAAAAA');
|
|
|
|
assert.strictEqual(psbt instanceof Psbt, true);
|
2019-09-07 06:42:03 +02:00
|
|
|
assert.ok((psbt as any).__CACHE.__TX);
|
|
|
|
});
|
2019-07-05 05:51:13 +02:00
|
|
|
it('fromHex returns Psbt type (not base class)', () => {
|
|
|
|
const psbt = Psbt.fromHex('70736274ff01000a01000000000000000000000000');
|
|
|
|
assert.strictEqual(psbt instanceof Psbt, true);
|
2019-09-07 06:42:03 +02:00
|
|
|
assert.ok((psbt as any).__CACHE.__TX);
|
|
|
|
});
|
|
|
|
});
|
2019-07-09 11:21:00 +02:00
|
|
|
|
|
|
|
describe('Cache', () => {
|
|
|
|
it('non-witness UTXOs are cached', () => {
|
|
|
|
const f = fixtures.cache.nonWitnessUtxo;
|
2019-09-07 06:42:03 +02:00
|
|
|
const psbt = Psbt.fromBase64(f.psbt);
|
2019-07-09 11:21:00 +02:00
|
|
|
const index = f.inputIndex;
|
|
|
|
|
|
|
|
// Cache is empty
|
2019-09-07 06:42:03 +02:00
|
|
|
assert.strictEqual(
|
|
|
|
(psbt as any).__CACHE.__NON_WITNESS_UTXO_BUF_CACHE[index],
|
|
|
|
undefined,
|
|
|
|
);
|
2019-07-09 11:21:00 +02:00
|
|
|
|
|
|
|
// Cache is populated
|
2019-09-07 06:42:03 +02:00
|
|
|
psbt.updateInput(index, { nonWitnessUtxo: f.nonWitnessUtxo as any });
|
|
|
|
const value = psbt.data.inputs[index].nonWitnessUtxo;
|
|
|
|
assert.ok(
|
|
|
|
(psbt as any).__CACHE.__NON_WITNESS_UTXO_BUF_CACHE[index].equals(value),
|
|
|
|
);
|
|
|
|
assert.ok(
|
|
|
|
(psbt as any).__CACHE.__NON_WITNESS_UTXO_BUF_CACHE[index].equals(
|
|
|
|
f.nonWitnessUtxo,
|
|
|
|
),
|
|
|
|
);
|
2019-07-09 11:21:00 +02:00
|
|
|
|
|
|
|
// Cache is rebuilt from internal transaction object when cleared
|
2019-09-07 06:42:03 +02:00
|
|
|
psbt.data.inputs[index].nonWitnessUtxo = Buffer.from([1, 2, 3]);
|
|
|
|
(psbt as any).__CACHE.__NON_WITNESS_UTXO_BUF_CACHE[index] = undefined;
|
|
|
|
assert.ok((psbt as any).data.inputs[index].nonWitnessUtxo.equals(value));
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|