bitpay / bitcore

A full stack for bitcoin and blockchain-based applications
https://bitcore.io/
MIT License
4.81k stars 2.08k forks source link

Spend from multiple address to the address #3599

Closed samyan closed 1 year ago

samyan commented 1 year ago

Hi, i'm trying to collect all funds from our hd wallets and send it to the main wallet. I'm looking in docs but i can find nothing about.

const txHex: string = new Bitcore.Transaction()
    .from(utxos) // Utxos from different wallets
    .to("toAddress", 10000000)
    .fee(200000)
    .sign(pKeys ???? ) // How sign with multiple pkeys
    .serialize();

I don't know if I'm on the right track, or what I require is done completely differently? Thanks in advance.

samyan commented 1 year ago

Got it. Just added the typings of typescript for set array of keys to [sign method] as the original is obsolete:

original:

sign(privateKey: PrivateKey | string): this;

replaced:

sign(privateKey: PrivateKey | PrivateKey[] | string | string[]): this;

final code

const txHex: string = new Bitcore.Transaction()
    .from(utxos)
    .to(toAddress, spendAmount.toNumber())
    .change(toAddress)
    .fee(this.feeAmount.toNumber())
    .sign(fromSources.map((fromSources) => fromSources.privateKey))
    .serialize();