Fix tests to use new sign method
This commit is contained in:
parent
17f5f35569
commit
969b3a5e18
9 changed files with 744 additions and 164 deletions
ts_src
|
@ -23,11 +23,17 @@ const PREVOUT_TYPES: Set<string> = new Set([
|
|||
'p2wpkh',
|
||||
'p2ms',
|
||||
// P2SH wrapped
|
||||
'p2sh-p2pkh',
|
||||
'p2sh-p2pk',
|
||||
'p2sh-p2wpkh',
|
||||
'p2sh-p2ms',
|
||||
// P2WSH wrapped
|
||||
'p2wsh-p2pkh',
|
||||
'p2wsh-p2pk',
|
||||
'p2wsh-p2ms',
|
||||
// P2SH-P2WSH wrapper
|
||||
'p2sh-p2wsh-p2pkh',
|
||||
'p2sh-p2wsh-p2pk',
|
||||
'p2sh-p2wsh-p2ms',
|
||||
]);
|
||||
|
||||
|
@ -75,6 +81,14 @@ interface TxbSignArg {
|
|||
witnessScript?: Buffer;
|
||||
}
|
||||
|
||||
function tfMessage(type: any, value: any, message: string): void {
|
||||
try {
|
||||
typeforce(type, value);
|
||||
} catch (err) {
|
||||
throw new Error(message);
|
||||
}
|
||||
}
|
||||
|
||||
function txIsString(tx: Buffer | string | Transaction): tx is string {
|
||||
return typeof tx === 'string' || tx instanceof String;
|
||||
}
|
||||
|
@ -223,7 +237,7 @@ export class TransactionBuilder {
|
|||
|
||||
sign(
|
||||
signParams: number | TxbSignArg,
|
||||
keyPair: ECPairInterface,
|
||||
keyPair?: ECPairInterface,
|
||||
redeemScript?: Buffer,
|
||||
hashType?: number,
|
||||
witnessValue?: number,
|
||||
|
@ -248,66 +262,198 @@ export class TransactionBuilder {
|
|||
]);
|
||||
vin = signParams.vin;
|
||||
keyPair = signParams.keyPair;
|
||||
hashType = signParams.hashType;
|
||||
const prevOutType = (this.__INPUTS[vin] || []).prevOutType;
|
||||
switch (signParams.prevOutScriptType) {
|
||||
const posType = signParams.prevOutScriptType;
|
||||
switch (posType) {
|
||||
case 'p2pkh':
|
||||
if (prevOutType !== 'pubkeyhash') {
|
||||
throw new TypeError(`input #${vin} is not of type p2pkh`);
|
||||
if (prevOutType && prevOutType !== 'pubkeyhash') {
|
||||
throw new TypeError(
|
||||
`input #${vin} is not of type p2pkh: ${prevOutType}`,
|
||||
);
|
||||
}
|
||||
tfMessage(
|
||||
typeforce.value(undefined),
|
||||
signParams.witnessScript,
|
||||
`${posType} requires NO witnessScript`,
|
||||
);
|
||||
tfMessage(
|
||||
typeforce.value(undefined),
|
||||
signParams.redeemScript,
|
||||
`${posType} requires NO redeemScript`,
|
||||
);
|
||||
tfMessage(
|
||||
typeforce.value(undefined),
|
||||
signParams.witnessValue,
|
||||
`${posType} requires NO witnessValue`,
|
||||
);
|
||||
break;
|
||||
case 'p2pk':
|
||||
if (prevOutType !== 'pubkey') {
|
||||
throw new TypeError(`input #${vin} is not of type p2pk`);
|
||||
if (prevOutType && prevOutType !== 'pubkey') {
|
||||
throw new TypeError(
|
||||
`input #${vin} is not of type p2pk: ${prevOutType}`,
|
||||
);
|
||||
}
|
||||
tfMessage(
|
||||
typeforce.value(undefined),
|
||||
signParams.witnessScript,
|
||||
`${posType} requires NO witnessScript`,
|
||||
);
|
||||
tfMessage(
|
||||
typeforce.value(undefined),
|
||||
signParams.redeemScript,
|
||||
`${posType} requires NO redeemScript`,
|
||||
);
|
||||
tfMessage(
|
||||
typeforce.value(undefined),
|
||||
signParams.witnessValue,
|
||||
`${posType} requires NO witnessValue`,
|
||||
);
|
||||
break;
|
||||
case 'p2wpkh':
|
||||
if (prevOutType !== 'witnesspubkeyhash') {
|
||||
throw new TypeError(`input #${vin} is not of type p2wpkh`);
|
||||
if (prevOutType && prevOutType !== 'witnesspubkeyhash') {
|
||||
throw new TypeError(
|
||||
`input #${vin} is not of type p2wpkh: ${prevOutType}`,
|
||||
);
|
||||
}
|
||||
typeforce(typeforce.Buffer, signParams.witnessScript);
|
||||
typeforce(typeforce.Satoshi, signParams.witnessValue);
|
||||
witnessScript = signParams.witnessScript;
|
||||
tfMessage(
|
||||
typeforce.value(undefined),
|
||||
signParams.witnessScript,
|
||||
`${posType} requires NO witnessScript`,
|
||||
);
|
||||
tfMessage(
|
||||
typeforce.value(undefined),
|
||||
signParams.redeemScript,
|
||||
`${posType} requires NO redeemScript`,
|
||||
);
|
||||
tfMessage(
|
||||
types.Satoshi,
|
||||
signParams.witnessValue,
|
||||
`${posType} requires witnessValue`,
|
||||
);
|
||||
witnessValue = signParams.witnessValue;
|
||||
break;
|
||||
case 'p2ms':
|
||||
if (prevOutType !== 'multisig') {
|
||||
throw new TypeError(`input #${vin} is not of type p2ms`);
|
||||
if (prevOutType && prevOutType !== 'multisig') {
|
||||
throw new TypeError(
|
||||
`input #${vin} is not of type p2ms: ${prevOutType}`,
|
||||
);
|
||||
}
|
||||
tfMessage(
|
||||
typeforce.value(undefined),
|
||||
signParams.witnessScript,
|
||||
`${posType} requires NO witnessScript`,
|
||||
);
|
||||
tfMessage(
|
||||
typeforce.value(undefined),
|
||||
signParams.redeemScript,
|
||||
`${posType} requires NO redeemScript`,
|
||||
);
|
||||
tfMessage(
|
||||
typeforce.value(undefined),
|
||||
signParams.witnessValue,
|
||||
`${posType} requires NO witnessValue`,
|
||||
);
|
||||
break;
|
||||
case 'p2sh-p2wpkh':
|
||||
if (prevOutType !== 'scripthash') {
|
||||
throw new TypeError(`input #${vin} is not of type p2sh-p2wpkh`);
|
||||
if (prevOutType && prevOutType !== 'scripthash') {
|
||||
throw new TypeError(
|
||||
`input #${vin} is not of type p2sh-p2wpkh: ${prevOutType}`,
|
||||
);
|
||||
}
|
||||
typeforce(typeforce.Buffer, signParams.witnessScript);
|
||||
typeforce(typeforce.Buffer, signParams.redeemScript);
|
||||
typeforce(typeforce.Satoshi, signParams.witnessValue);
|
||||
tfMessage(
|
||||
typeforce.value(undefined),
|
||||
signParams.witnessScript,
|
||||
`${posType} requires NO witnessScript`,
|
||||
);
|
||||
tfMessage(
|
||||
typeforce.Buffer,
|
||||
signParams.redeemScript,
|
||||
`${posType} requires redeemScript`,
|
||||
);
|
||||
tfMessage(
|
||||
types.Satoshi,
|
||||
signParams.witnessValue,
|
||||
`${posType} requires witnessValue`,
|
||||
);
|
||||
witnessScript = signParams.witnessScript;
|
||||
redeemScript = signParams.redeemScript;
|
||||
witnessValue = signParams.witnessValue;
|
||||
break;
|
||||
case 'p2sh-p2ms':
|
||||
if (prevOutType !== 'scripthash') {
|
||||
throw new TypeError(`input #${vin} is not of type p2sh-p2ms`);
|
||||
case 'p2sh-p2pk':
|
||||
case 'p2sh-p2pkh':
|
||||
if (prevOutType && prevOutType !== 'scripthash') {
|
||||
throw new TypeError(
|
||||
`input #${vin} is not of type ${posType}: ${prevOutType}`,
|
||||
);
|
||||
}
|
||||
typeforce(typeforce.Buffer, signParams.redeemScript);
|
||||
tfMessage(
|
||||
typeforce.value(undefined),
|
||||
signParams.witnessScript,
|
||||
`${posType} requires NO witnessScript`,
|
||||
);
|
||||
tfMessage(
|
||||
typeforce.Buffer,
|
||||
signParams.redeemScript,
|
||||
`${posType} requires redeemScript`,
|
||||
);
|
||||
tfMessage(
|
||||
typeforce.value(undefined),
|
||||
signParams.witnessValue,
|
||||
`${posType} requires NO witnessValue`,
|
||||
);
|
||||
redeemScript = signParams.redeemScript;
|
||||
break;
|
||||
case 'p2wsh-p2ms':
|
||||
if (prevOutType !== 'witnessscripthash') {
|
||||
throw new TypeError(`input #${vin} is not of type p2wsh-p2ms`);
|
||||
case 'p2wsh-p2pk':
|
||||
case 'p2wsh-p2pkh':
|
||||
if (prevOutType && prevOutType !== 'witnessscripthash') {
|
||||
throw new TypeError(
|
||||
`input #${vin} is not of type ${posType}: ${prevOutType}`,
|
||||
);
|
||||
}
|
||||
typeforce(typeforce.Buffer, signParams.witnessScript);
|
||||
typeforce(typeforce.Satoshi, signParams.witnessValue);
|
||||
tfMessage(
|
||||
typeforce.Buffer,
|
||||
signParams.witnessScript,
|
||||
`${posType} requires witnessScript`,
|
||||
);
|
||||
tfMessage(
|
||||
typeforce.value(undefined),
|
||||
signParams.redeemScript,
|
||||
`${posType} requires NO redeemScript`,
|
||||
);
|
||||
tfMessage(
|
||||
types.Satoshi,
|
||||
signParams.witnessValue,
|
||||
`${posType} requires witnessValue`,
|
||||
);
|
||||
witnessScript = signParams.witnessScript;
|
||||
witnessValue = signParams.witnessValue;
|
||||
break;
|
||||
case 'p2sh-p2wsh-p2ms':
|
||||
if (prevOutType !== 'scripthash') {
|
||||
throw new TypeError(`input #${vin} is not of type p2sh-p2wsh-p2ms`);
|
||||
case 'p2sh-p2wsh-p2pk':
|
||||
case 'p2sh-p2wsh-p2pkh':
|
||||
if (prevOutType && prevOutType !== 'scripthash') {
|
||||
throw new TypeError(
|
||||
`input #${vin} is not of type ${posType}: ${prevOutType}`,
|
||||
);
|
||||
}
|
||||
typeforce(typeforce.Buffer, signParams.witnessScript);
|
||||
typeforce(typeforce.Buffer, signParams.redeemScript);
|
||||
typeforce(typeforce.Satoshi, signParams.witnessValue);
|
||||
tfMessage(
|
||||
typeforce.Buffer,
|
||||
signParams.witnessScript,
|
||||
`${posType} requires witnessScript`,
|
||||
);
|
||||
tfMessage(
|
||||
typeforce.Buffer,
|
||||
signParams.redeemScript,
|
||||
`${posType} requires witnessScript`,
|
||||
);
|
||||
tfMessage(
|
||||
types.Satoshi,
|
||||
signParams.witnessValue,
|
||||
`${posType} requires witnessScript`,
|
||||
);
|
||||
witnessScript = signParams.witnessScript;
|
||||
redeemScript = signParams.redeemScript;
|
||||
witnessValue = signParams.witnessValue;
|
||||
|
@ -318,6 +464,9 @@ export class TransactionBuilder {
|
|||
'TransactionBuilder sign first arg must be TxbSignArg or number',
|
||||
);
|
||||
}
|
||||
if (keyPair === undefined) {
|
||||
throw new Error('sign requires keypair');
|
||||
}
|
||||
// TODO: remove keyPair.network matching in 4.0.0
|
||||
if (keyPair.network && keyPair.network !== this.network)
|
||||
throw new TypeError('Inconsistent network');
|
||||
|
@ -391,7 +540,7 @@ export class TransactionBuilder {
|
|||
);
|
||||
}
|
||||
|
||||
const signature = keyPair.sign(signatureHash, this.__USE_LOW_R);
|
||||
const signature = keyPair!.sign(signatureHash, this.__USE_LOW_R);
|
||||
input.signatures![i] = bscript.signature.encode(signature, hashType!);
|
||||
return true;
|
||||
});
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue