UNPKG

@secux/app-btc

Version:
241 lines (237 loc) 9.72 kB
/*! Copyright 2022 SecuX Technology Inc Copyright Chen Wei-En Copyright Wu Tsung-Yu Licensed under the Apache License, Version 2.0 (the License); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an AS IS BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. */ /// <reference types="node" /> /// <reference types="node" /> import { communicationData } from "@secux/utility/lib/communication"; import { CoinType, txInput, txOutput, ScriptType, txOutputAddress, txOutputScriptExtened, AddressOption, PathObject, SignOption, TransactionObject } from './interface'; import { ITransport } from "@secux/transport"; export { AddressOption, CoinType, ScriptType, txInput, txOutput, txOutputAddress, txOutputScriptExtened }; /** * BTC package for SecuX device */ export declare class SecuxBTC { /** * Convert publickey to BTC address. * @param {string | Buffer} publickey secp256k1 publickey (hex string or buffer) * @param {string | PathObject} path BIP32 path, ex: m/44'/0'/0'/0/0 * @returns {string} address */ static addressConvert(publickey: string | Buffer, path: string | PathObject): string; /** * Prepare data for address generation. * @param {string} path BIP32 path, ex: m/44'/0'/0'/0/0 * @param {AddressOption} [option] for path validation * @returns {communicationData} data for sending to device */ static prepareAddress(path: string, option?: AddressOption): communicationData; /** * Generate address from response data. * @param {communicationData} response data from device * @param {string | PathObject} path BIP32 path, ex: m/44'/0'/0'/0/0 * @returns {string} address */ static resolveAddress(response: communicationData, path: string | PathObject): string; /** * Prepare data for secp256k1 publickey. * @param {string} path BIP32 path, ex: m/44'/0'/0'/0/0 * @param {AddressOption} [option] for path validation * @returns {communicationData} data for sending to device */ static preparePublickey(path: string, option?: AddressOption): communicationData; /** * Resolve secp256k1 publickey from response data. * @param {communicationData} response data from device * @returns {string} secp256k1 publickey (hex string) */ static resolvePublickey(response: communicationData): string; /** * Prepare data for extended publickey generation. * @param {string} path BIP32 path, ex: m/44'/0'/0'/0/0 * @returns {communicationData} data for sending to device */ static prepareXPublickey(path: string): communicationData; /** * Generate extended publickey from response data. * @param {communicationData} response data from device * @param {string} path BIP32 path, ex: m/44'/0'/0'/0/0 * @returns {string} extended publickey (xpub, ypub or zpub) */ static resolveXPublickey(response: communicationData, path: string): string; /** * Prepare data for signing. * @param {txInput} inputs array of utxo object * @param {txOutput} outputs output object * @param {SignOption} [option] * @returns {prepared} */ static prepareSign(inputs: Array<txInput>, outputs: txOutput, option?: SignOption): { commands: Array<communicationData>; rawTx: string; }; /** * Reslove signature from response data. * @param {communicationData} response data from device * @returns {Array<string>} signature array of hex string */ static resolveSignatureList(response: communicationData): string[]; /** * Serialize transaction wtih signature for broadcasting. * @param {communicationData|Array<communicationData>} response data from device * @param {TransactionObject} params * @returns {string} signed raw transaction */ static resolveTransaction(response: communicationData | Array<communicationData>, params: TransactionObject): string; static getAddress(this: ITransport, path: string, option?: AddressOption): Promise<string>; static getPublickey(this: ITransport, path: string, option?: AddressOption): Promise<string>; static getXPublickey(this: ITransport, path: string): Promise<string>; static sign(this: ITransport, inputs: Array<txInput>, outputs: txOutput, option?: SignOption): Promise<{ multi_command: Array<communicationData>; } & TransactionObject>; /** * Derive public key from xpub. * @param {string} xpub extended publickey (base58 encoded), depth must be 3 * @param {number} change BIP44 change field * @param {number} addressIndex BIP44 address_index field * @returns {communicationData} publickey */ static derivePublicKey(xpub: string | any, change: number, addressIndex: number): communicationData; /** * Derive xpub and generate BTC address. * @param {string} xpub extended publickey (base58 encoded), depth must be 3 * @param {number} change BIP44 change field * @param {number} addressIndex BIP44 address_index field * @param {AddressOption} [option] for address generation * @returns {string} address */ static deriveAddress(xpub: string, change: number, addressIndex: number, option?: AddressOption): string; /** * Estimate virtual size of transaction. * @param {Array<ScriptType>} inputs * @param {Array<ScriptType>} outputs * @returns {number} virtual size */ static getVirtualSize(inputs: Array<ScriptType>, outputs: Array<ScriptType>): number; /** * Estimate dust threshold of an output. * @param {ScriptType} output script type of txout * @param {number} [dustRelayFee] satoshis/vB, default: 3 * @returns {number} dust threshold */ static getDustThreshold(output: ScriptType, dustRelayFee?: number): number; /** * Validate address. * @param {string} address * @param {CoinType} [coin] default: CoinType.BITCOIN * @returns {boolean} true if address is valid */ static validateAddress(address: string, coin?: CoinType): boolean; /** * Get script type of address. * @param {string} address * @param {CoinType} [coin] default: CoinType.BITCOIN * @returns {ScriptType} script type of address */ static getScriptType(address: string, coin?: CoinType): ScriptType; } /** * Data type for transmission. * @typedef {string|Buffer} communicationData */ /** * Script type for input/output. * @typedef {enum} ScriptType * @property {number} P2PKH 0 * @property {number} P2WPKH 1 * @property {number} P2SH_P2PKH 2 * @property {number} P2SH_P2WPKH 3 * @property {number} P2TR 4 */ /** * Coins that are nearly identical to Bitcoin. * @typedef {enum} CoinType * @property {number} BITCOIN 0 * @property {number} TESTNET 1 * @property {number} REGTEST 2 * @property {number} LITECOIN 3 * @property {number} BITCOINCASH 4 * @property {number} GROESTL 5 * @property {number} DIGIBYTE 6 * @property {number} DASH 7 * @property {number} DOGECOIN 8 */ /** * Parameters for address generation. * @typedef {object} PathObject * @property {CoinType} coin enum * @property {ScriptType} script enum */ /** * Options for path validation. * @typedef {object} AddressOption * @property {CoinType} [coin] enum * @property {ScriptType} [script] enum */ /** * UTXO. * @typedef {object} txInput * @property {string} path BIP32 path refer to utxo * @property {string | Buffer} publickey scep256k1 publickey from `path` * @property {string} hash referenced transaction hash * @property {number} vout referenced transaction output index * @property {number | string} satoshis referenced transaction output amount * @property {ScriptType} [script] script type related to `path` * @property {string} [txHex] referenced raw transaction for validation */ /** * Outputs consist of one payment and one or no return. * @typedef {object} txOutput * @property {txOutputAddress | txOutputScriptExtened} to receiving address information * @property {txOutputScriptExtened} [utxo] changes */ /** * Receiving address and payment. * @typedef {object} txOutputAddress * @property {string} address receiving address * @property {number | string} satoshis receiving amount */ /** * Payment for another held account. * @typedef {object} txOutputScriptExtened * @property {string} path BIP32 path * @property {string | Buffer} publickey scep256k1 publickey from `path` * @property {number | string} satoshis amount * @property {ScriptType} [script] script type related to `path` */ /** * Options used during the signing. * @typedef {object} SignOption * @property {CoinType} [coin] check cointype for each input * @property {number} [feeRate] calculate optimal transaction fee and replace it * @property {boolean} [isRBF] make Replace-by-Fee transaction * @property {string} [xpub] account's extended publickey */ /** * Object for the signing and validation. * @typedef {object} prepared * @property {communicationData} commandData data for sending to device * @property {string} rawTx unsigned raw transaction */ /** * Paramters for finalizing transaction. * @typedef {object} TransactionObject * @property {string} rawTx unsigned raw transaction * @property {Array<string | Buffer>} publickeys publickey correspond to each input * @property {CoinType} [coin] */