|
@ -52,6 +52,7 @@ export class Psbt extends PsbtBase { |
|
|
} |
|
|
} |
|
|
return psbt as InstanceType<T>; |
|
|
return psbt as InstanceType<T>; |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
static fromBuffer<T extends typeof PsbtBase>( |
|
|
static fromBuffer<T extends typeof PsbtBase>( |
|
|
this: T, |
|
|
this: T, |
|
|
buffer: Buffer, |
|
|
buffer: Buffer, |
|
@ -75,6 +76,7 @@ export class Psbt extends PsbtBase { |
|
|
checkTxForDupeIns(tx!, psbt.__CACHE); |
|
|
checkTxForDupeIns(tx!, psbt.__CACHE); |
|
|
return psbt as InstanceType<T>; |
|
|
return psbt as InstanceType<T>; |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
private __CACHE: PsbtCache = { |
|
|
private __CACHE: PsbtCache = { |
|
|
__NON_WITNESS_UTXO_TX_CACHE: [], |
|
|
__NON_WITNESS_UTXO_TX_CACHE: [], |
|
|
__NON_WITNESS_UTXO_BUF_CACHE: [], |
|
|
__NON_WITNESS_UTXO_BUF_CACHE: [], |
|
@ -82,28 +84,30 @@ export class Psbt extends PsbtBase { |
|
|
__TX: new Transaction(), |
|
|
__TX: new Transaction(), |
|
|
}; |
|
|
}; |
|
|
private opts: PsbtOpts; |
|
|
private opts: PsbtOpts; |
|
|
|
|
|
|
|
|
constructor(opts: PsbtOptsOptional = {}) { |
|
|
constructor(opts: PsbtOptsOptional = {}) { |
|
|
super(); |
|
|
super(); |
|
|
// set defaults
|
|
|
// set defaults
|
|
|
this.opts = Object.assign({}, DEFAULT_OPTS, opts); |
|
|
this.opts = Object.assign({}, DEFAULT_OPTS, opts); |
|
|
this.__CACHE.__TX = Transaction.fromBuffer(this.globalMap.unsignedTx!); |
|
|
const c = this.__CACHE; |
|
|
|
|
|
c.__TX = Transaction.fromBuffer(this.globalMap.unsignedTx!); |
|
|
this.setVersion(2); |
|
|
this.setVersion(2); |
|
|
|
|
|
|
|
|
// set cache
|
|
|
// set cache
|
|
|
const self = this; |
|
|
|
|
|
delete this.globalMap.unsignedTx; |
|
|
delete this.globalMap.unsignedTx; |
|
|
Object.defineProperty(this.globalMap, 'unsignedTx', { |
|
|
Object.defineProperty(this.globalMap, 'unsignedTx', { |
|
|
enumerable: true, |
|
|
enumerable: true, |
|
|
get(): Buffer { |
|
|
get(): Buffer { |
|
|
if (self.__CACHE.__TX_BUF_CACHE !== undefined) { |
|
|
const buf = c.__TX_BUF_CACHE; |
|
|
return self.__CACHE.__TX_BUF_CACHE; |
|
|
if (buf !== undefined) { |
|
|
|
|
|
return buf; |
|
|
} else { |
|
|
} else { |
|
|
self.__CACHE.__TX_BUF_CACHE = self.__CACHE.__TX.toBuffer(); |
|
|
c.__TX_BUF_CACHE = c.__TX.toBuffer(); |
|
|
return self.__CACHE.__TX_BUF_CACHE; |
|
|
return c.__TX_BUF_CACHE; |
|
|
} |
|
|
} |
|
|
}, |
|
|
}, |
|
|
set(data: Buffer): void { |
|
|
set(data: Buffer): void { |
|
|
self.__CACHE.__TX_BUF_CACHE = data; |
|
|
c.__TX_BUF_CACHE = data; |
|
|
}, |
|
|
}, |
|
|
}); |
|
|
}); |
|
|
|
|
|
|
|
@ -118,8 +122,6 @@ export class Psbt extends PsbtBase { |
|
|
enumerable, |
|
|
enumerable, |
|
|
writable, |
|
|
writable, |
|
|
}); |
|
|
}); |
|
|
dpew(this, '__TX', false, true); |
|
|
|
|
|
dpew(this, '__EXTRACTED_TX', false, true); |
|
|
|
|
|
dpew(this, '__CACHE', false, true); |
|
|
dpew(this, '__CACHE', false, true); |
|
|
dpew(this, 'opts', false, true); |
|
|
dpew(this, 'opts', false, true); |
|
|
} |
|
|
} |
|
@ -265,7 +267,6 @@ export class Psbt extends PsbtBase { |
|
|
const { script, isP2SH, isP2WSH, isSegwit } = getScriptFromInput( |
|
|
const { script, isP2SH, isP2WSH, isSegwit } = getScriptFromInput( |
|
|
inputIndex, |
|
|
inputIndex, |
|
|
input, |
|
|
input, |
|
|
this.__CACHE.__TX, |
|
|
|
|
|
this.__CACHE, |
|
|
this.__CACHE, |
|
|
); |
|
|
); |
|
|
if (!script) return false; |
|
|
if (!script) return false; |
|
@ -312,7 +313,6 @@ export class Psbt extends PsbtBase { |
|
|
? getHashForSig( |
|
|
? getHashForSig( |
|
|
inputIndex, |
|
|
inputIndex, |
|
|
Object.assign({}, input, { sighashType: sig.hashType }), |
|
|
Object.assign({}, input, { sighashType: sig.hashType }), |
|
|
this.__CACHE.__TX, |
|
|
|
|
|
this.__CACHE, |
|
|
this.__CACHE, |
|
|
) |
|
|
) |
|
|
: { hash: hashCache!, script: scriptCache! }; |
|
|
: { hash: hashCache!, script: scriptCache! }; |
|
@ -388,7 +388,6 @@ export class Psbt extends PsbtBase { |
|
|
this.inputs, |
|
|
this.inputs, |
|
|
inputIndex, |
|
|
inputIndex, |
|
|
keyPair.publicKey, |
|
|
keyPair.publicKey, |
|
|
this.__CACHE.__TX, |
|
|
|
|
|
this.__CACHE, |
|
|
this.__CACHE, |
|
|
); |
|
|
); |
|
|
|
|
|
|
|
@ -409,7 +408,6 @@ export class Psbt extends PsbtBase { |
|
|
this.inputs, |
|
|
this.inputs, |
|
|
inputIndex, |
|
|
inputIndex, |
|
|
keyPair.publicKey, |
|
|
keyPair.publicKey, |
|
|
this.__CACHE.__TX, |
|
|
|
|
|
this.__CACHE, |
|
|
this.__CACHE, |
|
|
); |
|
|
); |
|
|
|
|
|
|
|
@ -513,19 +511,13 @@ function getHashAndSighashType( |
|
|
inputs: PsbtInput[], |
|
|
inputs: PsbtInput[], |
|
|
inputIndex: number, |
|
|
inputIndex: number, |
|
|
pubkey: Buffer, |
|
|
pubkey: Buffer, |
|
|
unsignedTx: Transaction, |
|
|
|
|
|
cache: PsbtCache, |
|
|
cache: PsbtCache, |
|
|
): { |
|
|
): { |
|
|
hash: Buffer; |
|
|
hash: Buffer; |
|
|
sighashType: number; |
|
|
sighashType: number; |
|
|
} { |
|
|
} { |
|
|
const input = checkForInput(inputs, inputIndex); |
|
|
const input = checkForInput(inputs, inputIndex); |
|
|
const { hash, sighashType, script } = getHashForSig( |
|
|
const { hash, sighashType, script } = getHashForSig(inputIndex, input, cache); |
|
|
inputIndex, |
|
|
|
|
|
input, |
|
|
|
|
|
unsignedTx, |
|
|
|
|
|
cache, |
|
|
|
|
|
); |
|
|
|
|
|
checkScriptForPubkey(pubkey, script, 'sign'); |
|
|
checkScriptForPubkey(pubkey, script, 'sign'); |
|
|
return { |
|
|
return { |
|
|
hash, |
|
|
hash, |
|
@ -678,9 +670,9 @@ interface HashForSigData { |
|
|
function getHashForSig( |
|
|
function getHashForSig( |
|
|
inputIndex: number, |
|
|
inputIndex: number, |
|
|
input: PsbtInput, |
|
|
input: PsbtInput, |
|
|
unsignedTx: Transaction, |
|
|
|
|
|
cache: PsbtCache, |
|
|
cache: PsbtCache, |
|
|
): HashForSigData { |
|
|
): HashForSigData { |
|
|
|
|
|
const unsignedTx = cache.__TX; |
|
|
const sighashType = input.sighashType || Transaction.SIGHASH_ALL; |
|
|
const sighashType = input.sighashType || Transaction.SIGHASH_ALL; |
|
|
let hash: Buffer; |
|
|
let hash: Buffer; |
|
|
let script: Buffer; |
|
|
let script: Buffer; |
|
@ -831,9 +823,9 @@ interface GetScriptReturn { |
|
|
function getScriptFromInput( |
|
|
function getScriptFromInput( |
|
|
inputIndex: number, |
|
|
inputIndex: number, |
|
|
input: PsbtInput, |
|
|
input: PsbtInput, |
|
|
unsignedTx: Transaction, |
|
|
|
|
|
cache: PsbtCache, |
|
|
cache: PsbtCache, |
|
|
): GetScriptReturn { |
|
|
): GetScriptReturn { |
|
|
|
|
|
const unsignedTx = cache.__TX; |
|
|
const res: GetScriptReturn = { |
|
|
const res: GetScriptReturn = { |
|
|
script: null, |
|
|
script: null, |
|
|
isSegwit: false, |
|
|
isSegwit: false, |
|
|