1.1.9 • Published 4 months ago

@coolwallet/btc v1.1.9

Weekly downloads
21
License
ISC
Repository
github
Last release
4 months ago

CoolWallet Bitcoin SDK

Typescript library with support for the integration of Bitcoin for third party application, include the functionalities of generation of addresses and signed transactions.

Install

npm i @coolwallet/btc

Usage

import BTC from '@coolwallet/btc';
import { crypto } from '@coolwallet/core';
import { createTransport } from '@coolwallet/transport-web-ble';

const btc = new BTC();

const transport = await createTransport();

const { privateKey: appPrivateKey } = crypto.key.generateKeyPair();

const appId = 'appId that had been registered by wallet';

const addressIndex = 0;

const address = await btc.getAddress(transport, appPrivateKey, appId, scriptType, 0);

const signTxData = {
    transport,
    appPrivateKey,
    appId,
    scriptType: 1,
    inputs: [{
        preTxHash: "735153f57da91462a01c17937a397aa67f7b5faf9ab74ebcdd4c8f485aba26f5",
        preIndex: 1,
        preValue: "87302",
        sequence: 0xFFFFFFFF,
        addressIndex: 0,
        pubkeyBuf: Uint8Array(33)
    }],
    output: {
        value: "10000",
        address: "3442qW39131y5Q8qR963ehjmxXPAXUWn7Q",
    },
    change: {
        value: "77301.99983500001",
        addressIndex: 0,
        pubkeyBuf: Uint8Array(33)
    }
}

const normalTx = await btc.signTransaction(signTxData)

const usdtSignTxData = {
    transport,
    appPrivateKey,
    appId,
    scriptType: 1,
    inputs: [{
        preTxHash: "735153f57da91462a01c17937a397aa67f7b5faf9ab74ebcdd4c8f485aba26f5",
        preIndex: 1,
        preValue: 87302,
        sequence: 0xFFFFFFFF,
        addressIndex: 0,
        pubkeyBuf: Uint8Array(33)
    }],
    output: {
        value: "546",
        address: "3442qW39131y5Q8qR963ehjmxXPAXUWn7Q",
    },
    change: {
        value: "69926",
        addressIndex: 0,
        pubkeyBuf: Uint8Array(33)
    }
}

const usdtTx = await btc.signUSDTTransaction(usdtSignTxData)

Methods

getAddress

Description

Get address by address index.

The BTC address generated is compatible to BIP141 (SegWit Proposal) and BIP44 with account and change set to 0, which means calling getAddress with addressIndex = i will get the address of folllowing path:

m/44'/0'/0'/0/{i}

In the design of current hardware, we only support path m/44'/0'/0'/0/{i} for speed optimization. This might change in the future and we will then open a more general interface to deal with custom path.

async getAddress(
    transport: Transport, 
    appPrivateKey: string, 
    appId: string, 
    scriptType: ScriptType, 
    addressIndex: number
): Promise<string> 

Arguments

ArgDescriptionTypeRequired
transportObject to communicate with CoolWallet deviceTransportTrue
appPrivateKeyPrivate key for the connected applicationstringTrue
appIdID for the connected applicationstringTrue
scriptTypeDefine the type of script of the addressScriptTypeTrue
addressIndexThe from address index in BIP44 derivationnumberTrue

signTransaction

Description

Sign Bitcoin Transaction.

async signTransaction(signTxData: signTxType):Promise<string>

signTxType Arguments

ArgDescriptionTypeRequired
transportObject to communicate with CoolWallet deviceTransportTrue
appPrivateKeyPrivate key for the connected applicationstringTrue
appIdID for the connected applicationstringTrue
scriptTypeDefine the type of script of the addressScriptTypeTrue
inputsArray of inputs of previous transactions (UTXO)InputsTrue
outputOutput of the transactionOutputTrue
changeAddress to receive changesChangeTrue
confirmCBCallback of confirmation data to the connected applicationFunctionFalse
authorizedCBCallback of authorized transaction data to the connected applicationFunctionFalse

signUSDTTransaction

Description

Sign USDT Transaction

async signUSDTTransaction(signUSDTTxData: signUSDTTxType): Promise<string>

signUSDTTxType Arguments

ArgDescriptionTypeRequired
transportObject to communicate with CoolWallet deviceTransportTrue
appPrivateKeyPrivate key for the connected applicationstringTrue
appIdID for the connected applicationstringTrue
scriptTypeDefine the type of script of the addressScriptTypeTrue
inputsArray of inputs of previous transactions (UTXO)InputsTrue
outputOutput of the transactionOutputTrue
valueAmount of USDT to transferstringTrue
changeAddress to receive changesChangeTrue
confirmCBCallback of confirmation data to the connected applicationFunctionFalse
authorizedCBCallback of authorized transaction data to the connected applicationFunctionFalse
2.0.0-beta.0

4 months ago

1.1.9

10 months ago

1.1.9-beta.0

10 months ago

1.1.8

11 months ago

1.1.7

11 months ago

1.1.6

1 year ago

1.1.6-beta.7

1 year ago

1.1.6-beta.8

1 year ago

1.1.5

1 year ago

1.1.4

1 year ago

1.1.6-beta.1

1 year ago

1.1.6-beta.2

1 year ago

1.1.6-beta.3

1 year ago

1.1.6-beta.4

1 year ago

1.1.6-beta.0

1 year ago

1.1.6-beta.5

1 year ago

1.1.6-beta.6

1 year ago

1.1.3

3 years ago

1.1.1

3 years ago

1.0.1

3 years ago

1.0.1-beta.1

3 years ago

1.0.0

4 years ago

1.0.1-beta.0

4 years ago

0.1.0

4 years ago

0.1.0-beta.19

4 years ago

0.1.0-beta.18

4 years ago

0.1.0-beta.17

4 years ago

0.1.0-beta.16

4 years ago

0.1.0-beta.15

4 years ago

0.1.0-beta.14

4 years ago

0.1.0-beta.13

4 years ago

0.1.0-beta.12

4 years ago

0.1.0-beta.11

4 years ago

0.1.0-beta.10

4 years ago

0.1.0-beta.9

4 years ago

0.1.0-beta.8

4 years ago

0.1.0-beta.7

4 years ago

0.1.0-beta.6

4 years ago

0.1.0-beta.3

4 years ago

0.1.0-beta.4

4 years ago

0.1.0-beta.2

4 years ago

0.1.0-beta.1

4 years ago

0.1.0-beta.0

5 years ago