@makkii/makkii-core
v0.1.2
Published
Generic interfaces that helping crypto currency wallet manage digital currency.
Downloads
1
Readme
makkii-core
Generic interfaces
Installation
$ npm install @makkii/makkii-core
Usage
import { ApiClient, KeystoreClient } from '@makkii/makkii-core';
import { AionApiClient, AionKeystoreClient, AionLedger} from '@makkii/app-aion';
import { BtcApiClient, BtcKeystoreClient } from '@makkii/app-btc';
// api client usage
const api_client = new ApiClient();
api_client.addCoin('aion', new AionApiClient({
network: 'mainnet',
jsonrpc: '***'
}));
api_client.addCoin('btc', new BtcApiClient({
network: 'BTC',
insight_api: '***'
}));
api_client.getBalance('aion', '0x...')
.then(console.log)
.catch(error=>console.log(error));
// keystore client usage
const keystore_client = new KeystoreClient();
keystore_client.addCoin('aion', new AionKeystoreClient());
keystore_client.addCoin('btc', new BtcKeystoreClient('BTC'));
api_client.buildTransaction(
'aion',
'0x...', // from address
'0x...', // to address
0, // amount
{
gasPrice: 10,
gasLimit: 21000,
isTokenTransfer: false
}
).then(function(unsignedTx) {
keystore_client.signTransaction('aion', unsignedTx, new AionLedger(), {
index: 0
}).then(function(signedTx) {
console.log(signedTx);
});
});
API
Table of Contents
- IHardware
- CoinPrice
- IApiClient
- addCoin
- removeCoin
- getBlockByNumber
- getBlockNumber
- getBalance
- getTransactionStatus
- getTransactionExplorerUrl
- getTransactionsByAddress
- buildTransaction
- sendTransaction
- sameAddress
- getTokenIconUrl
- getTokenDetail
- getAccountTokenTransferHistory
- getAccountTokens
- getAccountTokenBalance
- getTopTokens
- searchTokens
- getCoinPrices
- IsingleKeystoreClient
- Token
- Keypair
- LedgerKeypair
- IkeystoreSigner
- IkeystoreClient
- IsingleApiClient
- IsingleApiTokenClient
- IsingleApiFullClient
IHardware
Extends IkeystoreSigner
Hardware wallet interface
getAccount
Get account from hardware wallet.
Parameters
index
number index path in hd wallet
Returns Promise<LedgerKeypair> key pair
getHardwareStatus
Get hardware wallet status.
Parameters
params
additional parameters that may affect hardware status
Returns Promise<boolean> boolean status of connect/app status.
CoinPrice
Examples
{"crypto":"AION","fiat":"USD","price":0.06133475939999999}
IApiClient
Api client interface that manages multiple chains' api client and expose all functions.
addCoin
Register an api client.
Parameters
coinType
string a key name for the added api client, you will specify the coinType for other operations.client
(IsingleApiClient | IsingleApiFullClient) api client that implements IsingleApiClient or IsingleApiFullClient
Returns void
removeCoin
Remove a registered api client
Parameters
coinType
string api client key name
Returns boolean if remove api client successfully
getBlockByNumber
Get block information by the given block number
Parameters
coinType
string coin type name you specified in addCoinblockNumber
string block number. integer or hex string depends on kernel rpc implementation.
Returns Promise<any> depends on different chains' block structure
getBlockNumber
Get latest block number of the given chain
Parameters
coinType
string coin type name you specified in addCoin
Returns Promise<any> latest block number whose type should be biginteger or hex string
getBalance
Get balance of the given account
Parameters
Returns Promise<any> balance in hex string or biginteger
getTransactionStatus
Get transation status
Parameters
Returns Promise<any> transaction status, depends on different api client's implementation
getTransactionExplorerUrl
Get web page url that can display transaction details.
The url should be able to access from web browser by plain HTTP GET request.
Parameters
coinType
string coin type name you specified in addCoinhash
any transaction hash
Returns string web page url that display transaction details
getTransactionsByAddress
Get the given account's recent transactions by page.
Parameters
coinType
string coin type name you specified in addCoinaddress
string account's public addresspage
number page numbersize
number how mnay transactions to get in this pagetimestamp
number?
Returns Promise<any>
buildTransaction
Build up transaction object to sign. transaction nonce should be encapsulated into transaction object.
Parameters
coinType
string coin type name you specified in addCoinfrom
string transaction senderto
string amount receiver. this field isn't alway transaction's to field. if the transaction is a token transfer, transaction to field is token contract address, this field is encoded in contract method parameters.value
BigNumber amount value. this field isn't alwasy transaction's value field. if the transaction is a token transfer, transaction value field is zero, this field is encoded in contract method parameters.options
any common options could be: gas limit, gas price, contract address, is token transfer, data, etc.
Returns Promise<any>
sendTransaction
Broadcast transaction.
Parameters
coinType
string coin type name you specified in addCoinunsignedTx
any unsigned transaction object. User can call buildTransaction to get unsigned transaction.signer
T implementation of IkeystoreSignersignerParams
any sign parameters for differernt implementation IkeystoreSigner
Returns Promise<any> transaction hash
sameAddress
Check if two address are the same account.
Parameters
coinType
string coin type name you specified in addCoinaddress1
string first address to compareaddress2
string second address to compare
Returns boolean if two address are the same account.
getTokenIconUrl
Get token icon url.
Parameters
coinType
string coin type name you specified in addCointokenSymbol
string token symbolcontractAddress
string token creation contract address
Returns string token icon url
getTokenDetail
Get token details. In general token details contains: contractAddress, symbol, name, tokenDecimals
Parameters
coinType
string coin type name you specified in addCoincontractAddress
string token contract address
Returns Promise<any> toke details
getAccountTokenTransferHistory
Get token transfer history of the given account. For pagination, user could use combination of page + size or timestamp + size.
Parameters
coinType
string coin type name you specified in addCoinaddress
string account addresssymbolAddress
string token contract addresspage
number? page numbersize
number? number of transfer records in this pagetimestamp
number? get transfer records earlier than this timestamp
Returns Promise<any>
getAccountTokens
Get tokens whose balance > 0 for the given account.
Parameters
Returns Promise<any>
getAccountTokenBalance
Get token balance of the given account
Parameters
coinType
string coin type name you specified in addCoincontractAddress
string token contract addressaddress
string account address
Returns Promise<any>
getTopTokens
Get top tokens.
Parameters
Returns Promise<any>
searchTokens
Search token by keyword.
Parameters
Returns Promise<any>
getCoinPrices
Get coin prices
Parameters
currency
string fiat currency
Returns Promise<Array<CoinPrice>> array of coin price
IsingleKeystoreClient
Interface that handle key store operations.
signTransaction
Sign transaction.
Parameters
tx
any transaction objectsigner
T singer that implement [[IkeystoreSigner]] interfacesignerParams
any sign parameters
Returns Promise<any> signed transaction
generateMnemonic
Randomly generate 12 words mnemonic.
Returns string 12 words mnemonic
getAccountFromMnemonic
Get specified account from mnemonic phrase
Parameters
Returns Promise<any> account
getAccountFromHardware
Get account from hardware wallet.
Parameters
index
number indexPath in hardwallet wallethardware
IHardware harware wallet that implements IHardware interface
Returns Promise<any> account
recoverKeyPairByPrivateKey
Recover account from private key.
Parameters
priKey
string private keyoptions
any? options that affect recovery algorithm
Returns Promise<any> key pair
validatePrivateKey
Check if private key is valid.
Parameters
Returns boolean if private key is valid
validateAddress
Check if address is valid.
Parameters
address
string address to validate
Returns boolean if address is valid.
Token
token
Keypair
Key pair
LedgerKeypair
Ledger key pair
IkeystoreSigner
Interface for sign transaction.
signTransaction
Sign transaction.
Parameters
tx
any transaction object to signparams
any sign parameters such as private key or index path
Returns Promise<SignedTx> signed transaction
IkeystoreClient
Keystore interface that manages multiple keystore clients and expose all functions.
addCoin
Register keystore client.
Parameters
coinType
string a key name for the added keystore client, you will specify the coinType for other operations.client
IsingleKeystoreClient
Returns void
removeCoin
Remove a registered keystore client
Parameters
coinType
string keystore client key name
Returns boolean if remoe keystore client successfully.
signTransaction
Sign transaction.
Parameters
coinType
string coin type name you specified in addCointx
any transaction object to signsigner
T signer that implement [[IkeystoreSigner]] interfacesignerParams
any sign parameters
Returns Promise<any> signed transaction
generateMnemonic
Randomly generate 12 words mnemonic phrases.
Parameters
coinType
string coin type name you specified in addCoin
Returns string 12 words mnemonic phrases
recoverKeyPairByPrivateKey
Recover key pair from private key
Parameters
coinType
string coin type name you specified in addCoinpriKey
string private keyoptions
any? options required by recovery algorithm
Returns Promise<any> recovered account
validatePrivateKey
Check if private key is valid.
Parameters
Returns boolean if private key is valid.
validateAddress
Check if an address is valid
Parameters
Returns boolean if an address is valid.
getAccountFromMnemonic
Get specified account from mnemonic phrase
Parameters
coinType
string coin type name you specified in addCoinddress_index
numbermnemonic
string mnemonic phraseaddress_index
address index in hierachy determinist wallet
Returns Promise<any> account
getAccountFromHardware
Get account from hardware wallet.
Parameters
coinType
string coin type name you specified in addCoinindex
number indexPath in hardwallet wallethardware
IHardware harware wallet that implements IHardware interface
Returns Promise<any> account
IsingleApiClient
Interface that defines basic api client functions: network configuration, block, transaction, balance, etc.
config
Configuration property.
Type: any
symbol
Coin symbol eg: 'AION', 'BTC' ...
Type: string
updateConfiguration
Update configuration.
Parameters
config
any configuration
Returns void
getNetwork
get network name.
Returns string network name
getBlockByNumber
Get block information by the given block number
Parameters
blockNumber
string block number. integer or hex string depends on kernel rpc implementation.
Returns Promise<any> depends on different chains' block structure
getBlockNumber
Get latest block number of the given chain
Returns Promise<any> latest block number whose type should be biginteger or hex string
getTransactionStatus
Get transation status
Parameters
hash
string transaction hash
Returns Promise<any> transaction status, depends on different api client's implementation
getTransactionExplorerUrl
Get web page url that can display transaction details.
The url should be able to access from web browser by plain HTTP GET request.
Parameters
hash
any transaction hash
Returns string web page url that display transaction details
getBalance
Get balance of the given account
Parameters
address
string account's public address
Returns Promise<any> balance in hex string or biginteger
getTransactionsByAddress
Get the given account's recent transactions by page.
Parameters
address
string account's public addresspage
number page numbersize
number how mnay transactions to get in this pagetimestamp
number?
Returns Promise<any>
buildTransaction
Build up transaction object to sign. transaction nonce should be encapsulated into transaction object.
Parameters
from
string transaction senderto
string amount receiver. this field isn't alway transaction's to field. if the transaction is a token transfer, transaction to field is token contract address, this field is encoded in contract method parameters.value
BigNumber amount value. this field isn't alwasy transaction's value field. if the transaction is a token transfer, transaction value field is zero, this field is encoded in contract method parameters.options
any common options could be: gas limit, gas price, contract address, is token transfer, data, etc.
Returns Promise<Transaction>
sendTransaction
Broadcast transaction.
Parameters
unsignedTx
any unsigned transaction object. User can call [[buildTransaction]] to get unsigned transaction.signer
T implementation of IkeystoreSignersignerParams
any sign parameters for differernt implementation IkeystoreSigner
Returns Promise<any> transaction hash
sameAddress
Check if two address are the same account.
Parameters
Returns boolean if two address are the same account.
IsingleApiTokenClient
Interface that defines token related functions.
tokenSupport
is token supported
Type: boolean
getTokenIconUrl
Get token icon url.
Parameters
Returns string token icon url
getTokenDetail
Get token details. In general token details contains: contractAddress, symbol, name, tokenDecimals
Parameters
contractAddress
string token contract address
Returns Promise<any> toke details
getAccountTokenTransferHistory
Get token transfer history of the given account. For pagination, user could use combination of page + size or timestamp + size.
Parameters
address
string account addresscointractAddress
stringpage
number? optional, page numbersize
number? optional, number of transfer records in this pagetimestamp
number? optional, get transfer records earlier than this timestampsymbolAddress
token contract address
Returns Promise<any>
getAccountTokens
Get tokens whose balance > 0 for the given account.
Parameters
address
string account address
Returns Promise<any>
getAccountTokenBalance
Get token balance of the given account
Parameters
Returns Promise<any>
getTopTokens
Get top tokens.
Parameters
topN
number? number of tokens to get
Returns Promise<any>
searchTokens
Search token by keyword.
Parameters
keyword
string keyword
Returns Promise<any>
IsingleApiFullClient
Extends IsingleApiClient, IsingleApiTokenClient
Interface that defines basic api client and token functions.