-
Notifications
You must be signed in to change notification settings - Fork 45
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add coinselect and basic wallet functionality to sdk
Signed-off-by: Gregory Hill <[email protected]>
- Loading branch information
Showing
4 changed files
with
105 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
declare module "coinselect" { | ||
interface Input { | ||
txId: string, | ||
vout: number, | ||
value: number, | ||
// For use with PSBT: | ||
// not needed for coinSelect, but will be passed on to inputs later | ||
nonWitnessUtxo?: Buffer, | ||
// OR | ||
// if your utxo is a segwit output, you can use witnessUtxo instead | ||
witnessUtxo?: { | ||
script: Buffer, | ||
value: number, | ||
} | ||
} | ||
|
||
interface Output { | ||
address?: string, | ||
value: number | ||
} | ||
|
||
/** | ||
* Blackjack, with Accumulative fallback | ||
* @param inputs The UTXOs to spend from. | ||
* @param outputs The transaction outputs. | ||
* @param feeRate The current Bitcoin fee rate. | ||
* @returns The fee paid to the network. | ||
*/ | ||
function coinSelect( | ||
inputs: Array<Input>, | ||
outputs: Array<Output>, | ||
feeRate: number // satoshis per byte | ||
): { | ||
inputs?: Array<Input>, | ||
outputs?: Array<Output>, | ||
fee: number, | ||
}; | ||
|
||
export = coinSelect; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
import { Psbt, Transaction, Network, TxOutput, address as addressUtils } from "bitcoinjs-lib"; | ||
import { ElectrsClient } from "./electrs"; | ||
|
||
import coinSelect = require('coinselect'); | ||
|
||
export interface Wallet { | ||
getAddress(): Promise<string>; | ||
getChangeAddress(): Promise<string>; | ||
signPsbt(psbt: Psbt): Promise<Psbt>; | ||
} | ||
|
||
export async function createAndFundTransaction(electrsClient: ElectrsClient, wallet: Wallet, network: Network, txOutputs: Array<TxOutput>): Promise<Transaction> { | ||
const address = await wallet.getAddress(); | ||
const utxos = await electrsClient.getAddressUtxos(address); | ||
|
||
const { inputs, outputs } = coinSelect( | ||
utxos.map(utxo => { | ||
return { | ||
txId: utxo.txid, | ||
vout: utxo.vout, | ||
value: utxo.value, | ||
} | ||
}), | ||
txOutputs.map(txOut => { | ||
return { | ||
address: addressUtils.fromOutputScript(txOut.script, network), | ||
value: txOut.value, | ||
} | ||
}), | ||
1 | ||
); | ||
|
||
let psbt = new Psbt({ network }) | ||
|
||
inputs.forEach(input => | ||
psbt.addInput({ | ||
hash: input.txId, | ||
index: input.vout, | ||
// TODO: determine these from electrs utxos | ||
nonWitnessUtxo: input.nonWitnessUtxo, | ||
witnessUtxo: input.witnessUtxo, | ||
}) | ||
); | ||
|
||
const changeAddress = await wallet.getChangeAddress(); | ||
outputs.forEach(output => { | ||
// watch out, outputs may have been added that you need to provide | ||
// an output address/script for | ||
if (!output.address) { | ||
output.address = changeAddress; | ||
} | ||
|
||
psbt.addOutput({ | ||
address: output.address, | ||
value: output.value, | ||
}) | ||
}); | ||
|
||
await wallet.signPsbt(psbt); | ||
return psbt.extractTransaction(); | ||
} |