Changeset View
Changeset View
Standalone View
Standalone View
cashtab/src/transactions/index.js
// Copyright (c) 2023 The Bitcoin developers | // Copyright (c) 2023 The Bitcoin developers | ||||
// Distributed under the MIT software license, see the accompanying | // Distributed under the MIT software license, see the accompanying | ||||
// file COPYING or http://www.opensource.org/licenses/mit-license.php. | // file COPYING or http://www.opensource.org/licenses/mit-license.php. | ||||
import * as utxolib from '@bitgo/utxo-lib'; | import * as utxolib from '@bitgo/utxo-lib'; | ||||
import { coinSelect } from 'ecash-coinselect'; | import { coinSelect } from 'ecash-coinselect'; | ||||
import cashaddr from 'ecashaddrjs'; | import cashaddr from 'ecashaddrjs'; | ||||
import { isValidMultiSendUserInput } from 'validation'; | import { isValidMultiSendUserInput } from 'validation'; | ||||
import { toSatoshis } from 'wallet'; | import { toSatoshis } from 'wallet'; | ||||
/** | /** | ||||
* Sign tx inputs | * Sign tx inputs | ||||
* @param {object} txBuilder an initialized TransactionBuilder with inputs and outputs added | * @param {object} txBuilder an initialized TransactionBuilder with inputs and outputs added | ||||
* @param {array} accounts [...{address: <cashaddr>, wif: <wif>}] | * @param {array} paths paths key of a cashtab wallet | ||||
* @param {array} inputs [...{address: <cashaddr>, value: <number>}] | * @param {array} inputs [...{address: <cashaddr>, value: <number>}] | ||||
* @throws {error} if private key is corrupted or wif is undefined | * @throws {error} if private key is corrupted or wif is undefined | ||||
*/ | */ | ||||
export const signInputs = (txBuilder, accounts, inputs) => { | export const signInputs = (txBuilder, paths, inputs) => { | ||||
inputs.forEach((input, index) => { | inputs.forEach((input, index) => { | ||||
// Select the correct signing key based on the address of the input | // Select the correct signing key based on the address of the input | ||||
const wif = accounts | const wif = paths.get(input.path).wif; | ||||
.filter(acc => acc.address === input.address) | |||||
.pop().wif; | |||||
// TODO store this in wallet instead of generating it every time you sign a tx | // TODO store this in wallet instead of generating it every time you sign a tx | ||||
const utxoECPair = utxolib.ECPair.fromWIF(wif, utxolib.networks.ecash); | const utxoECPair = utxolib.ECPair.fromWIF(wif, utxolib.networks.ecash); | ||||
// Specify hash type | // Specify hash type | ||||
// TODO fix this in utxo-lib | // TODO fix this in utxo-lib | ||||
const hashTypes = { | const hashTypes = { | ||||
SIGHASH_ALL: 0x01, | SIGHASH_ALL: 0x01, | ||||
▲ Show 20 Lines • Show All 60 Lines • ▼ Show 20 Lines | for (const output of outputs) { | ||||
let isChange = !isOpReturn && !('address' in output); | let isChange = !isOpReturn && !('address' in output); | ||||
if (isChange) { | if (isChange) { | ||||
// Note that you may now have a change output with no specified address | // Note that you may now have a change output with no specified address | ||||
// This is expected behavior of coinSelect | // This is expected behavior of coinSelect | ||||
// User provides target output, coinSelect adds change output if necessary (with no address key) | // User provides target output, coinSelect adds change output if necessary (with no address key) | ||||
// Change address is wallet address | // Change address is wallet address | ||||
// Use Path1899 address as change address | // Use Path1899 address as change address | ||||
output.address = wallet.paths.find( | output.address = wallet.paths.get(1899).address; | ||||
pathInfo => pathInfo.path === 1899, | |||||
).address; | |||||
} | } | ||||
// TODO add cashaddr support for eCash to txBuilder in utxo-lib | // TODO add cashaddr support for eCash to txBuilder in utxo-lib | ||||
txBuilder.addOutput( | txBuilder.addOutput( | ||||
isOpReturn ? output.script : cashaddr.toLegacy(output.address), | isOpReturn ? output.script : cashaddr.toLegacy(output.address), | ||||
output.value, | output.value, | ||||
); | ); | ||||
} | } | ||||
▲ Show 20 Lines • Show All 62 Lines • Show Last 20 Lines |