1.1.6 • Published 7 months ago

@coolwallet/trx v1.1.6

Weekly downloads
18
License
Apache-2.0
Repository
github
Last release
7 months ago

CoolWallet Tron (TRX) SDK

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

Install

npm install @coolwallet/trx

Usage

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

const trx = new TRX();

const transport = await createTransport();

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

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

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

const normalTransaction = {
    refBlockBytes: "c75b",
    refBlockHash: "7cfc890c6e4d7a15",
    expiration: 1583304414000,
    timestamp: 1583268416080,
    contract: {
        ownerAddress: "41bf97a54f4b829c4e9253b26024b1829e1a3b1120",
        toAddress: "41859009fd225692b11237a6ffd8fdba2eb7140cca",
        amount: 100000000
    }
};

const signTxData = {
  transport,
  appPrivateKey,
  appId,
  addressIndex: 0,
  transaction: normalTransaction
}

const normalTx = await trx.signTransaction(signTxData);

const trc20Transaction = {
    refBlockBytes: "5a40",
    refBlockHash: "12cd29483be4900a",
    expiration: 1638339660000,
    timestamp: 1638339602030,
    contract: {
        ownerAddress: "411634b4103bd6feb3a9f9e9eb937f484c3c8b0046",
        contractAddress: "41a614f803b6fd780986a42c78ec9c7f77e6ded13c",
        receiverAddress: "4139e6a453301f6d5d228966dd8d742f1f861bd40f",
        amount: 2000000
    }
    feeLimit: 150000000,
    option: {
        symbol: "USDT",
        decimals: 6
    }
}

const trc20SignTxData = {
  transport,
  appPrivateKey,
  appId,
  addressIndex: 0,
  transaction: trc20Transaction
}

const trc20Tx = await trx.signTRC20Transfer(trc20SignTxData);

Methods

getAddress

Description

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

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

In the design of current hardware, we only support path m/44'/195'/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: type.Transport,
    appPrivateKey: string,
    appId: string,
    addressIndex: number
): Promise<string>

Arguments

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

Sign Transaction

Description

CoolWallet currently support various signing methods and contracts based on the type of the transaction. The SignTxData and Transaction arguments are the essentail data fields for TRX transactions. Depends on the type of transaction, the extended arguments may vary.

SignTxData Arguments

ArgDescriptionTypeRequired
transportObject to communicate with CoolWallet deviceTransportTrue
appPrivateKeyPrivate key for the connected applicationstringTrue
appIdID for the connected applicationstringTrue
addressIndexThe from address index in BIP44 derivationnumberTrue
confirmCBCallback of confirmation data to the connected applicationFunctionFalse
authorizedCBCallback of authorized transaction data to the connected applicationFunctionFalse

Transaction Arguments

ArgDescriptionTypeRequired
refBlockBytesReference to the bytes of the hash of the latest blockstringTrue
refBlockHashReference to the bytes of the height of the latest blockstringTrue
expirationThe time period between transaction creation and broadcastnumberTrue
timestampTimestamps for the transactionnumberTrue

The extended arguments may vary depend on the type of the transaction. The transaction can be for transfering (signTransaction, signTRC20Transfer) or staking (signFreeze, signUnfreeze, signVoteWitness, signWithdrawBalance) purposes.

signTransaction

Description

Sign TRX Transaction.

async signTransaction(signTxData: type.NormalTradeData): Promise<string> 

NormalContract Arguments

ArgDescriptionTypeRequired
ownerAddressThe from addressstringTrue
toAddressThe to addressstringTrue
amountThe amount to transfernumber | stringTrue

signTRC20Transfer

Description

Sign TRC20 Transfering Transaction

async signTRC20Transfer(signTxData: type.TRC20TransferData): Promise<string>

TRC20TransferContract Arguments

ArgDescriptionTypeRequired
ownerAddressThe from addressstringTrue
contractAddressAddress of the TRC-20 contractstringTrue
receiverAddressThe to addressstringTrue
amountThe amount to transfernumber | stringTrue
1.1.6

7 months ago

1.1.5

8 months ago

1.1.6-beta.1

8 months ago

1.1.6-beta.0

8 months ago

1.1.4

2 years ago

1.1.3

2 years ago

1.1.1

2 years ago

1.0.7

3 years ago

1.0.7-beta.0

3 years ago

1.0.6

3 years ago

1.0.6-beta.0

3 years ago

1.0.5

3 years ago

1.0.5-beta.0

3 years ago

1.0.0

3 years ago

1.0.4-beta.0

3 years ago

1.0.4-beta.1

3 years ago

1.0.4-beta.2

3 years ago

1.0.2-beta.0

3 years ago

1.0.1-beta.0

3 years ago

1.0.2-beta.1

3 years ago

1.0.4

3 years ago

1.0.3

3 years ago

0.1.0

3 years ago

0.1.0-beta.11

3 years ago

0.1.0-beta.10

3 years ago

0.1.0-beta.9

3 years ago

0.1.0-beta.8

3 years ago

0.1.0-beta.7

3 years ago

0.1.0-beta.6

3 years ago

0.1.0-beta.5

3 years ago

0.1.0-beta.4

3 years ago

0.1.0-beta.3

3 years ago

0.1.0-beta.2

3 years ago

0.1.0-beta.1

3 years ago