Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
return co(function*() {
// take txHex first always, but as it might already be signed, take halfSigned second
const txHex = params.txHex || (params.halfSigned && params.halfSigned.txHex);
if (!txHex) {
throw new Error('missing required param txHex or halfSigned.txHex');
}
let tx;
try {
const txToHex = Buffer.from(txHex, 'base64');
const decodedTx = Encoding.decode(txToHex);
// if we are a signed msig tx, the structure actually has the { msig, txn } as the root object
// if we are not signed, the decoded tx is the txn - refer to partialSignTxn and MultiSig constructor
// in algosdk for more information
const txnForDecoding = decodedTx.txn || decodedTx;
tx = Multisig.MultiSigTransaction.from_obj_for_encoding(txnForDecoding);
} catch (ex) {
throw new Error('txHex needs to be a valid tx encoded as base64 string');
}
const id = tx.txID();
const fee = { fee: tx.fee };
const outputAmount = tx.amount || 0;
const outputs: { amount: number; address: string }[] = [];
signTransaction(params: SignTransactionOptions): SignedTransaction {
const { txHex, addressVersion, keys, sk, isHalfSigned } = this.verifySignTransactionParams(params);
const encodedPublicKeys = _.map(keys, k => Address.decode(k).publicKey);
// decode our unsigned/half-signed tx
let transaction;
let txToHex;
try {
txToHex = Buffer.from(txHex, 'base64');
const initialDecodedTx = Encoding.decode(txToHex);
// we need to scrub the txn of sigs for half-signed
const decodedTx = isHalfSigned ? initialDecodedTx.txn : initialDecodedTx;
transaction = Multisig.MultiSigTransaction.from_obj_for_encoding(decodedTx);
} catch (e) {
throw new Error('transaction needs to be a valid tx encoded as base64 string');
}
// sign our tx
let signed = transaction.partialSignTxn({ version: addressVersion, threshold: 2, pks: encodedPublicKeys }, sk);
// if we have already signed it, we'll have to merge that with our previous tx
if (isHalfSigned) {
signed = mergeMultisigTransactions([Buffer.from(signed), txToHex]);
}