-
I'm using an external wallet that returns a signature when provided with pre-sign transaction data (i.e., pre-image data hash). The signature verification with the public key returning true. However, when the signature is compiled to get the signed transaction, it returns Error_signing (i.e., error_code = 7). My code is shown below: Signature (Testnet) : const walletCore = await initWasm();
export const getSignedTxHashBtc = async (signature: string, txObject: any) => {
if (signature == "") {
return "";
}
const inputEncoded = await getEncodedInput(txObject);
const pubKey = localStorageService.generatedPublicKey.get();
const pubData = walletCore.HexCoding.decode(pubKey ?? "");
const publicKey = walletCore.PublicKey.createWithData(
pubData,
walletCore.PublicKeyType.secp256k1
);
const preimageOutputEncoded = await generatePreSignTxHash(txObject);
const preimageOutput = walletCore.HexCoding.decode(preimageOutputEncoded);
const isValid = publicKey.verify(Buffer.from(signature, "hex"), preimageOutput); // isValid returns true
const signData = walletCore.HexCoding.decode(signature);
const signatures = walletCore.DataVector.createWithData(signData);
const publicKeys = walletCore.DataVector.createWithData(pubData);
const outputData = walletCore.TransactionCompiler.compileWithSignatures(
walletCore.CoinType.bitcoin,
inputEncoded,
signatures,
publicKeys
);
const output = TW.Bitcoin.Proto.SigningOutput.decode(outputData);
const outputBytes = output.toJSON();
return outputBytes;
};
export const getEncodedInput = async (inputs: any) => {
const { utxos, fromAddress, outputAmmount, toAddress } = inputs;
const utxo = utxos[0];
const utxo1 = TW.Bitcoin.Proto.UnspentTransaction.create({
outPoint: {
hash: Buffer.from(utxo.txid, "hex").reverse(),
index: utxo.vout,
sequence: 4294967295 // default sequence
},
amount: new Long(utxo.satoshis),
script: walletCore.BitcoinScript.lockScriptForAddress(
fromAddress,
walletCore.CoinType.bitcoin
).data()
});
const txInput = TW.Bitcoin.Proto.SigningInput.create({
hashType: walletCore.BitcoinSigHashType.all.value,
coinType: walletCore.CoinType.bitcoin.value,
amount: new Long(outputAmmount),
toAddress: toAddress,
lockTime: 0,
changeAddress: fromAddress,
utxo: [utxo1]
});
return TW.Bitcoin.Proto.SigningInput.encode(txInput).finish();
};
export const generatePreSignTxHash = async (txObject: any) => {
const {
version,
utxos,
outputs,
lockTime,
fromAddress,
forFeeAmount,
feeRate,
outputAmmount,
toAddress
} = txObject;
const inputEncoded = await getEncodedInput({
utxos,
fromAddress,
outputAmmount,
toAddress
});
const preimageOutputData = walletCore.TransactionCompiler.preImageHashes(
walletCore.CoinType.bitcoin,
inputEncoded
);
const preimageOutput =
TW.TxCompiler.Proto.PreSigningOutput.decode(preimageOutputData);
const inputDataHashHex = walletCore.HexCoding.encode(preimageOutput.dataHash);
return inputDataHashHex;
}; |
Beta Was this translation helpful? Give feedback.
Replies: 7 comments 14 replies
-
@satoshiotomakan signature verification against public key returns true. i'm missing something here const signData = walletCore.HexCoding.decode(signature);
const signatures = walletCore.DataVector.createWithData(signData);
const publicKeys = walletCore.DataVector.createWithData(pubData);
const outputData = walletCore.TransactionCompiler.compileWithSignatures(
walletCore.CoinType.bitcoin,
inputEncoded,
signatures,
publicKeys
); |
Beta Was this translation helpful? Give feedback.
-
Hi @harshavnaik, I think the error should be due to the wrong |
Beta Was this translation helpful? Give feedback.
-
@satoshiotomakan, the above code does not work properly when the sent amount is 0.01BTC. it works fine when the amount is 0.001BTC. I'm wondering what could be going wrong here? |
Beta Was this translation helpful? Give feedback.
-
@satoshiotomakan tried for multiple values. works fine for 0.001, 0.002, 0.003 and 0.004. above 0.004 its not working. const outputData = walletCore.TransactionCompiler.compileWithSignatures(
walletCore.CoinType.bitcoin,
inputEncoded,
signatures,
publicKeys
)
|
Beta Was this translation helpful? Give feedback.
-
@satoshiotomakan, did you have a chance to go through it? |
Beta Was this translation helpful? Give feedback.
-
@satoshiotomakan can you go through my code and let me know? what going wrong here? |
Beta Was this translation helpful? Give feedback.
-
Whenever there are multiple UTXOs, Each UTXO needs to be signed separately |
Beta Was this translation helpful? Give feedback.
Whenever there are multiple UTXOs, Each UTXO needs to be signed separately