@secux/app-btc
SecuX Hardware Wallet BTC API
Usage
import { SecuxBTC, ScriptType } from "@secux/app-btc";
First, create instance of ITransport.
Examples
-
Get address by purpose and script type.
- tweaked key-spend only address (default script: P2TR)
const path = "m/86'/0'/0'/0/0";
const address = await device.getAddress(path);
- native segwit address (default script: P2WPKH)
const address = await device.getAddress("m/84'/0'/0'/0/0");
- segwit address (default script: P2SH_P2WPKH)
const address = await device.getAddress("m/49'/0'/0'/0/0");
- legacy address (default script: P2PKH)
const address = await device.getAddress("m/44'/0'/0'/0/0");
-
For bitcoin ecosystem, you can use specific cointype by the coin.
- native segwit address for
dogecoin
const address = await device.getAddress("m/84'/3'/0'/0/0");
- segwit address for
litecoin
const address = await device.getAddress("m/49'/2'/0'/0/0");
- legacy address for
bitcoincash
const address = await device.getAddress("m/44'/145'/0'/0/0");
- you can refer to here for cointype.
-
Sign transaction.
const inputs = [
{
hash: "0b062e71e165fba9634d9fb1b5ba703e774bf374815b1f5a617c8d1e7d43dc01",
vout: 0,
txHex: "0100000001b103a004f672080ceae8277e83c296b5ac090ae78157979211da3e2d41399d1b010000006b483045022100f19d88e6a17789dc399ff2a93b4516bb44af32928d4986138f1a4f7f37ab277b022046fc14c958bc8aa97fea1d2fbf80982534cf51634d46c4d5178e5ca6698bca07012102f8667cfb5b80c3695e3f0c9078589cb04e8d15e71bdae89ebf24b82f9d663d5cffffffff02bc020000000000001976a9145c592f40134c6179a1ce5b06b28d5c2ae443113188ac00040000000000001976a9146d65ced4ef49e23cdbb4be9d510b38e5be28e10688ac00000000",
satoshis: 700,
path: "m/44'/0'/0'/0/0",
},
{
hash: "07ad0a13e501d292bc8b9e16a3a8b62f99f77ab9e37ea8d3b8453984a2899984",
vout: 0,
satoshis: 6000,
path: "m/49'/0'/0'/0/0",
},
{
hash: "8686aee2b9dcf559798b9718ed26ca92e0c64bef11c433e576cae658678c497d",
vout: 1,
satoshis: 1083,
path: "m/84'/0'/0'/1/0",
}
];
const to = {
address: "bc1qs0k3ekx0z7a7yuq3lse7prw373s8cr8lhxvccd",
satoshis: 1500
};
const utxo = {
path: "m/44'/0'/0'/0/0",
satoshis: 6100,
};
const { raw_tx } = await device.sign(
inputs,
{ to, utxo },
);
-
Derive address from xpub, ypub, or zpub.
- tweaked key-spend only address (default script: P2TR)
const xpub = "xpub6BgBgsespWvERF3LHQu6CnqdvfEvtMcQjYrcRzx53QJjSxarj2afYWcLteoGVky7D3UKDP9QyrLprQ3VCECoY49yfdDEHGCtMMj92pReUsQ";
const address = SecuxBTC.deriveAddress(xpub, 0, 0,
{
script: ScriptType.P2TR
});
- native segwit address (default script: P2WPKH)
const zpub = "zpub6rFR7y4Q2AijBEqTUquhVz398htDFrtymD9xYYfG1m4wAcvPhXNfE3EfH1r1ADqtfSdVCToUG868RvUUkgDKf31mGDtKsAYz2oz2AGutZYs";
const address = SecuxBTC.deriveAddress(zpub, 0, 0,
{
});
- segwit address (default script: P2SH_P2WPKH)
const ypub = "ypub6Ww3ibxVfGzLtJR4F9SRBicspAfvmvw54yern9Q6qZWFC9T6FYA34K57La5Sgs8pXuyvpDfEHX5KNZRiZRukUWaVPyL4NxA69sEAqdoV8ve";
const address = SecuxBTC.deriveAddress(ypub, 0, 1);
- legacy address (default script: P2PKH)
const xpub = "xpub6BosfCnifzxcFwrSzQiqu2DBVTshkCXacvNsWGYJVVhhawA7d4R5WSWGFNbi8Aw6ZRc1brxMyWMzG3DSSSSoekkudhUd9yLb6qx39T9nMdj";
const address = SecuxBTC.deriveAddress(xpub, 1, 0);
-
Estimate transaction size
const size = SecuxBTC.getVirtualSize(
[
ScriptType.P2PKH,
ScriptType.P2SH_P2WPKH,
ScriptType.P2SH_P2WPKH,
ScriptType.P2WPKH,
...
],
[
ScriptType.P2PKH,
ScriptType.P2PKH,
ScriptType.P2WPKH,
...
]
);
API Reference
BTC package for SecuX device
Kind: global class
- SecuxBTC
- .prepareAddress ⇒
communicationData
- .addressConvert(publickey, path) ⇒
string
- .resolveAddress(response, path) ⇒
string
- .preparePublickey(path, [option]) ⇒
communicationData
- .resolvePublickey(response) ⇒
string
- .prepareXPublickey(path) ⇒
communicationData
- .resolveXPublickey(response, path) ⇒
string
- .prepareSign(inputs, outputs, [option]) ⇒
prepared
- .resolveSignatureList(response) ⇒
Array.<string>
- .resolveTransaction(response, unsigned, publickeys, [coin]) ⇒
string
- .deriveAddress(xpub, change, addressIndex, [option]) ⇒
string
- .getVirtualSize(inputs, outputs) ⇒
number
SecuxBTC.prepareAddress ⇒ communicationData
Prepare data for address generation.
Returns: communicationData
- data for sending to device
Param | Type | Description |
---|
path | string | BIP32 path, ex: m/44'/0'/0'/0/0 |
[option] | AddressOption | for path validation |
SecuxBTC.addressConvert(publickey, path) ⇒ string
Convert publickey to BTC address.
Returns: string
- address
Param | Type | Description |
---|
publickey | string | Buffer | secp256k1 publickey (hex string or buffer) |
path | string | PathObject | BIP32 path, ex: m/44'/0'/0'/0/0 |
SecuxBTC.resolveAddress(response, path) ⇒ string
Generate address from response data.
Returns: string
- address
Param | Type | Description |
---|
response | communicationData | data from device |
path | string | PathObject | BIP32 path, ex: m/44'/0'/0'/0/0 |
SecuxBTC.preparePublickey(path, [option]) ⇒ communicationData
Prepare data for secp256k1 publickey.
Returns: communicationData
- data for sending to device
Param | Type | Description |
---|
path | string | BIP32 path, ex: m/44'/0'/0'/0/0 |
[option] | AddressOption | for path validation |
SecuxBTC.resolvePublickey(response) ⇒ string
Resolve secp256k1 publickey from response data.
Returns: string
- secp256k1 publickey (hex string)
Param | Type | Description |
---|
response | communicationData | data from device |
SecuxBTC.prepareXPublickey(path) ⇒ communicationData
Prepare data for extended publickey generation.
Returns: communicationData
- data for sending to device
Param | Type | Description |
---|
path | string | BIP32 path, ex: m/44'/0'/0'/0/0 |
SecuxBTC.resolveXPublickey(response, path) ⇒ string
Generate extended publickey from response data.
Returns: string
- extended publickey (xpub, ypub or zpub)
Param | Type | Description |
---|
response | communicationData | data from device |
path | string | BIP32 path, ex: m/44'/0'/0'/0/0 |
SecuxBTC.prepareSign(inputs, outputs, [option]) ⇒ prepared
Prepare data for signing.
SecuxBTC.resolveSignatureList(response) ⇒ Array.<string>
Reslove signature from response data.
Returns: Array.<string>
- signature array of hex string
Param | Type | Description |
---|
response | communicationData | data from device |
SecuxBTC.resolveTransaction(response, unsigned, publickeys, [coin]) ⇒ string
Serialize transaction wtih signature for broadcasting.
Returns: string
- signed raw transaction
Param | Type | Description |
---|
response | communicationData | data from device |
unsigned | string | unsigned raw transaction |
publickeys | Array.<communicationData> | secp256k1 publickey correspond to each input |
[coin] | CoinType | default: CoinType.BITCOIN |
SecuxBTC.deriveAddress(xpub, change, addressIndex, [option]) ⇒ string
Derive xpub and generate BTC address.
Returns: string
- address
Param | Type | Description |
---|
xpub | string | extended publickey (base58 encoded), depth must be 3 |
change | number | BIP44 change field |
addressIndex | number | BIP44 address_index field |
[option] | AddressOption | for address generation |
SecuxBTC.getVirtualSize(inputs, outputs) ⇒ number
Estimate virtual size of transaction.
Returns: number
- virtual size
Param | Type |
---|
inputs | Array.<ScriptType> |
outputs | Array.<ScriptType> |
txInput
Properties
Name | Type | Description |
---|
path | string | BIP32 path refer to utxo |
publickey | string | Buffer | scep256k1 publickey from path |
hash | string | referenced transaction hash |
vout | number | referenced transaction output index |
satoshis | number | referenced transaction output amount |
[script] | ScriptType | script type related to path |
[txHex] | string | referenced raw transaction for validation |
txOutput
Properties
SignOption
Properties
Name | Type | Description |
---|
[coin] | CoinType | enum |
[feeRate] | number | base fee per vbyte |
txOutputAddress
Properties
Name | Type | Description |
---|
address | string | receiving address |
satoshis | number | receiving amount |
txOutputScriptExtened
Properties
Name | Type | Description |
---|
path | string | BIP32 path |
publickey | string | Buffer | scep256k1 publickey from path |
satoshis | number | amount |
[script] | ScriptType | script type related to path |
prepared
Properties
Name | Type | Description |
---|
commandData | communicationData | data for sending to device |
rawTx | string | unsigned raw transaction |
PathObject
Properties
Name | Type | Description |
---|
coin | CoinType | enum |
script | ScriptType | enum |
AddressOption
Properties
Name | Type | Description |
---|
[coin] | CoinType | enum |
[script] | ScriptType | enum |
© 2018-21 SecuX Technology Inc.
authors:
andersonwu@secuxtech.com