Browse Source

Throw error when signing with a privkey that doesn't match the pubkey

psbt
Luke Childs 6 years ago
parent
commit
8d74bebe04
  1. 32
      src/psbt.js
  2. 44
      ts_src/psbt.ts

32
src/psbt.js

@ -2,6 +2,7 @@
Object.defineProperty(exports, '__esModule', { value: true }); Object.defineProperty(exports, '__esModule', { value: true });
const bip174_1 = require('bip174'); const bip174_1 = require('bip174');
const utils_1 = require('bip174/src/lib/utils'); const utils_1 = require('bip174/src/lib/utils');
const crypto_1 = require('./crypto');
const payments = require('./payments'); const payments = require('./payments');
const bscript = require('./script'); const bscript = require('./script');
const transaction_1 = require('./transaction'); const transaction_1 = require('./transaction');
@ -54,29 +55,24 @@ class Psbt extends bip174_1.Psbt {
signInput(inputIndex, keyPair) { signInput(inputIndex, keyPair) {
const input = this.inputs[inputIndex]; const input = this.inputs[inputIndex];
if (input === undefined) throw new Error(`No input #${inputIndex}`); if (input === undefined) throw new Error(`No input #${inputIndex}`);
const { hash, sighashType } = getHashForSig( const { hash, sighashType, script } = getHashForSig(
inputIndex, inputIndex,
input, input,
this.globalMap.unsignedTx, this.globalMap.unsignedTx,
); );
const pubkey = keyPair.publicKey; const pubkey = keyPair.publicKey;
// // TODO: throw error when the pubkey or pubkey hash is not found anywhere const pubkeyHash = crypto_1.hash160(keyPair.publicKey);
// // in the script const decompiled = bscript.decompile(script);
// const pubkeyHash = hash160(keyPair.publicKey); if (decompiled === null) throw new Error('Unknown script error');
// const hasKey = decompiled.some(element => {
// const decompiled = bscript.decompile(script); if (typeof element === 'number') return false;
// if (decompiled === null) throw new Error('Unknown script error'); return element.equals(pubkey) || element.equals(pubkeyHash);
// });
// const hasKey = decompiled.some(element => { if (!hasKey) {
// if (typeof element === 'number') return false; throw new Error(
// return element.equals(pubkey) || element.equals(pubkeyHash); `Can not sign for this input with the key ${pubkey.toString('hex')}`,
// }); );
// }
// if (!hasKey) {
// throw new Error(
// `Can not sign for this input with the key ${pubkey.toString('hex')}`,
// );
// }
const partialSig = { const partialSig = {
pubkey, pubkey,
signature: bscript.signature.encode(keyPair.sign(hash), sighashType), signature: bscript.signature.encode(keyPair.sign(hash), sighashType),

44
ts_src/psbt.ts

@ -1,7 +1,7 @@
import { Psbt as PsbtBase } from 'bip174'; import { Psbt as PsbtBase } from 'bip174';
import { PsbtInput } from 'bip174/src/lib/interfaces'; import { PsbtInput } from 'bip174/src/lib/interfaces';
import { checkForInput } from 'bip174/src/lib/utils'; import { checkForInput } from 'bip174/src/lib/utils';
// import { hash160 } from './crypto'; // TODO: used in pubkey check import { hash160 } from './crypto';
import { Signer } from './ecpair'; import { Signer } from './ecpair';
import { Network } from './networks'; import { Network } from './networks';
import * as payments from './payments'; import * as payments from './payments';
@ -60,30 +60,28 @@ export class Psbt extends PsbtBase {
signInput(inputIndex: number, keyPair: Signer): Psbt { signInput(inputIndex: number, keyPair: Signer): Psbt {
const input = this.inputs[inputIndex]; const input = this.inputs[inputIndex];
if (input === undefined) throw new Error(`No input #${inputIndex}`); if (input === undefined) throw new Error(`No input #${inputIndex}`);
const { const { hash, sighashType, script } = getHashForSig(
hash, inputIndex,
sighashType, input,
// script, // TODO: use for pubkey check below this.globalMap.unsignedTx!,
} = getHashForSig(inputIndex, input, this.globalMap.unsignedTx!); );
const pubkey = keyPair.publicKey; const pubkey = keyPair.publicKey;
// // TODO: throw error when the pubkey or pubkey hash is not found anywhere const pubkeyHash = hash160(keyPair.publicKey);
// // in the script
// const pubkeyHash = hash160(keyPair.publicKey); const decompiled = bscript.decompile(script);
// if (decompiled === null) throw new Error('Unknown script error');
// const decompiled = bscript.decompile(script);
// if (decompiled === null) throw new Error('Unknown script error'); const hasKey = decompiled.some(element => {
// if (typeof element === 'number') return false;
// const hasKey = decompiled.some(element => { return element.equals(pubkey) || element.equals(pubkeyHash);
// if (typeof element === 'number') return false; });
// return element.equals(pubkey) || element.equals(pubkeyHash);
// }); if (!hasKey) {
// throw new Error(
// if (!hasKey) { `Can not sign for this input with the key ${pubkey.toString('hex')}`,
// throw new Error( );
// `Can not sign for this input with the key ${pubkey.toString('hex')}`, }
// );
// }
const partialSig = { const partialSig = {
pubkey, pubkey,

Loading…
Cancel
Save