This module provides functions for interacting with PSBTs, see BIP174 https://github.com/bitcoin/bips/blob/master/bip-0174.mediawiki
Methods
(static) addSignaturesToPSBT(network, psbt, pubkeys, signatures) → {null|string}
Given an unsigned PSBT, an array of signing public key(s) (one per input), an array of signature(s) (one per input) in the same order as the pubkey(s), adds partial signature object(s) to each input and returns the PSBT with partial signature(s) included.
FIXME - maybe we add functionality of sending in a single pubkey as well, which would assume all of the signature(s) are for that pubkey.
Parameters:
Name | Type | Description |
---|---|---|
network |
module:networks.NETWORKS | bitcoin network |
psbt |
String | PSBT as base64 or hex string |
pubkeys |
Array.<Buffer> | public keys map 1:1 with signature(s) |
signatures |
Array.<Buffer> | transaction signatures map 1:1 with public key(s) |
Returns:
- partially signed PSBT in Base64
- Type
- null | string
(static) autoLoadPSBT(psbtFromFile, optionsopt) → {null|Psbt}
Given a string, try to create a Psbt object based on MAGIC (hex or Base64)
Parameters:
Name | Type | Attributes | Description |
---|---|---|---|
psbtFromFile |
String | Base64 or hex PSBT |
|
options |
Object |
<optional> |
options, e.g. TESTNET |
Returns:
- Psbt object from bitcoinjs-lib or null if failed to detect
- Type
- null | Psbt
(static) parseSignatureArrayFromPSBT(psbtFromFile) → {Object}
Extracts signatures in order of inputs and returns as array (or array of arrays if multiple signature sets)
Parameters:
Name | Type | Description |
---|---|---|
psbtFromFile |
String | base64 or hex |
Returns:
returns an array of arrays of ordered signatures or an array of signatures if only 1 signer
- Type
- Object
(static) parseSignaturesFromPSBT(psbtFromFile) → {Object}
Extracts the signature(s) from a PSBT. NOTE: there should be one signature per input, per signer.
ADDITIONAL NOTE: because of the restrictions we place on braids to march their multisig addresses (slices) forward at the same index across each chain of the braid, we do not run into a possible collision with this data structure. BUT - to have this method accommodate the most general form of signature parsing, it would be wise to wrap this one level deeper like:
address: [pubkey : [signature(s)]]
that way if your braid only advanced one chain's (member's) index so that a pubkey could be used in more than one address, everything would still function properly.
Parameters:
Name | Type | Description |
---|---|---|
psbtFromFile |
String | base64 or hex |
Returns:
returns an object with signatureSet(s) - an object with format {pubkey : [signature(s)]}
- Type
- Object
(static) psbtInputFormatter(input) → {module:inputs.MultisigTransactionPSBTInput}
Take a MultisigTransactionInput and turn it into a MultisigTransactionPSBTInput
Parameters:
Name | Type | Description |
---|---|---|
input |
module:inputs.MultisigTransactionInput | to decorate for PSBT |
Returns:
outputs the PSBT-ready Transaction Input
- Type
- module:inputs.MultisigTransactionPSBTInput
(static) psbtOutputFormatter(output) → {module:outputs.TransactionPSBTOutput}
Take a MultisigTransactionOutput and turn it into a MultisigTransactionPSBTOutput
Parameters:
Name | Type | Description |
---|---|---|
output |
module:outputs.TransactionOutput | output to decorate for PSBT |
Returns:
outputs the PSBT-ready Transaction Object
- Type
- module:outputs.TransactionPSBTOutput
(static) translatePSBT(network, addressType, psbt, signingKeyDetails) → {null|Object}
Translates a PSBT into inputs/outputs consumable by supported non-PSBT devices in the
unchained-wallets
library.
FIXME - Have only confirmed this is working for P2SH addresses on Ledger on regtest
Parameters:
Name | Type | Description |
---|---|---|
network |
module:networks.NETWORKS | bitcoin network |
addressType |
String | address type |
psbt |
String | PSBT as a base64 or hex string |
signingKeyDetails |
Object | Object containing signing key details (Fingerprint + bip32path prefix) |
Returns:
returns unchained-wallets transaction object with the format { inputs: [], outputs: [], bip32Derivations: [], }
- Type
- null | Object
(inner) filterRelevantBip32Derivations(psbt, signingKeyDetails) → {Array.<Object>}
Create unchained-wallets style transaction input objects
Parameters:
Name | Type | Description |
---|---|---|
psbt |
Object | Psbt bitcoinjs-lib object |
signingKeyDetails |
Object | Object containing signing key details (Fingerprint + bip32path prefix) |
Returns:
bip32Derivations - array of signing bip32Derivation objects
- Type
- Array.<Object>
(inner) getBip32Derivation(multisig, indexopt) → {Array.<Object>}
Return the getBip32Derivation (if known) for a given Multisig
object.
Parameters:
Name | Type | Attributes | Default | Description |
---|---|---|---|---|
multisig |
module:multisig.Multisig | the |
||
index |
number |
<optional> |
0 | the index to generate at |
Returns:
the getBip32Derivation includes all paths/root fingerprints to all pubkeys in the multisig object
- Type
- Array.<Object>
Example
import {
getBip32Derivation,
generateBraidFromExtendedPublicKeys,
generateMultisigFromPublicKeys, MAINNET, P2SH,
braidConfig,
} from "unchained-bitcoin";
const multisig = generateMultisigFromPublicKeys(MAINNET, P2SH, 2, "03a...", "03b...", "03c...");
console.log(getBip32Derivation(multisig, 0)); // null, Multisig object isn't aware of its braid.
const braid = generateBraidFromExtendedPublicKeys(MAINNET, P2SH, {{'xpub...', bip32path: "m/45'/0'/0'"}, {'xpub...', bip32path: "m/45'/0/0"}, {'xpub...', bip32path: "m/45'/0/0"}}, 2);
const multisig = braid.deriveMultisigByIndex("0");
console.log(getBip32Derivation(multisig, 0)); // {
{masterFingerprint: Buffer('1234..', 'hex'), path: "m/45'/0'/0'/0/0", pubkey: Buffer.from("02...", 'hex')}
{masterFingerprint: Buffer('3453..', 'hex'), path: "m/45'/0/0/0/0", pubkey: Buffer.from("03...", 'hex')}
{masterFingerprint: Buffer('1533..', 'hex'), path: "m/45'/0/0/0/0", pubkey: Buffer.from("02...", 'hex')}
}
(inner) getNumSigners(psbt) → {int}
Get number of signers in the PSBT
Parameters:
Name | Type | Description |
---|---|---|
psbt |
Psbt | bitcoinjs-lib object |
Returns:
number of signers in the PSBT
- Type
- int
(inner) getUnchainedInputsFromPSBT(network, addressType, psbt) → {Array.<Object>}
Create unchained-wallets style transaction input objects from a PSBT
Parameters:
Name | Type | Description |
---|---|---|
network |
module:networks.NETWORKS | bitcoin network |
addressType |
String | address type |
psbt |
Object | Psbt bitcoinjs-lib object |
Returns:
unchained multisig transaction inputs array
- Type
- Array.<Object>
(inner) getUnchainedOutputsFromPSBT(psbt) → {Array.<Object>}
Create unchained-wallets style transaction output objects from a PSBT
Parameters:
Name | Type | Description |
---|---|---|
psbt |
Object | Psbt bitcoinjs-lib object |
Returns:
unchained multisig transaction outputs array
- Type
- Array.<Object>
(inner) getWitnessOutputScriptFromInput(input) → {Output}
Gets the Witness script from the ouput that generated the input
Parameters:
Name | Type | Description |
---|---|---|
input |
module:inputs.MultisigTransactionInput | input you are requesting output's script from |
Returns:
bitcoinjs-lib Output object (amount+script)
- Type
- Output
(inner) psbtInputDerivation(input) → {Array.<Object>}
Grabs appropriate bip32Derivation based on the input's last index
Parameters:
Name | Type | Description |
---|---|---|
input |
module:inputs.MultisigTransactionInput | input you are requesting bip32Derivation from |
Returns:
array of objects containing (rootFingerprints && pubkeys && bip32paths) for this Multisig
- Type
- Array.<Object>
(inner) psbtMultisigLock(multisig) → {Object}
Return the locking script for the given Multisig
object in a PSBT consumable format
Parameters:
Name | Type | Description |
---|---|---|
multisig |
module:multisig.Multisig | the |
Returns:
returns an object with proper parameters attached that are needed to validate spending
- Type
- Object
(inner) psbtOutputDerivation(output) → {Array.<Object>}
Grabs appropriate bip32Derivation for a change output
Parameters:
Name | Type | Description |
---|---|---|
output |
module:outputs.TransactionOutput | output you are requesting bip32Derivation from |
Returns:
array of objects containing (rootFingerprints && pubkeys && bip32paths) for this Multisig
- Type
- Array.<Object>
Type Definitions
MultisigTransactionPSBTInput
Represents a transaction PSBT input.
The Multisig
object represents
the address the corresponding UTXO belongs to.
Type:
- Object
Properties:
Name | Type | Description |
---|---|---|
hash |
string | The transaction ID where funds were received |
index |
number | The index in the transaction referred to by {txid} |
utxoToVerify |
Buffer | Object | The UTXO to verify |
multisigScript |
Multisig | Locking script(s) for the multisig address |
bip32Derivation |
Object | the set of (rootFingerprints && pubkeys && bip32paths) for this Multisig |
TransactionPSBTOutput
Represents an output in a PSBT transaction.
Type:
- Object
Properties:
Name | Type | Attributes | Description |
---|---|---|---|
address |
string | the output address |
|
value |
number | output amount in Satoshis |
|
redeemScript |
Object |
<optional> |
For change addresses - Locking script(s) for the multisig address |
witnessScript |
Object |
<optional> |
For change addresses - Locking script(s) for the multisig address |
bip32Derivation |
Object |
<optional> |
For change addresses - the set of (rootFingerprints && pubkeys && bip32paths) for this Multisig |