@test-xchain/xchain-client
v0.10.8
Published
A specification for a generalised interface for crypto wallets clients, to be used by XChainJS implementations. The client should not have any functionality to generate a key, instead, the `asgardex-crypto` library should be used to ensure cross-chain com
Downloads
4
Readme
XChainJS Wallet Client Interface
A specification for a generalised interface for crypto wallets clients, to be used by XChainJS implementations. The client should not have any functionality to generate a key, instead, the asgardex-crypto
library should be used to ensure cross-chain compatible keystores are handled. The client is only ever passed a master BIP39 phrase, from which a temporary key and address is decoded.
Configuration
Initialise and set up the client to connect to its necessary third-party services to fulfil basic functionality. The third-party services used must be at a minimum to fulfil the wallet functionality, such as displaying balances and sending transactions.
During configuration, the following can be passed in:
- Network choice (default is MAINNET)
- Phrase (mandatory)
- Service Keys (optional, if null, client will use config defaults or free service limits.)
Querying
Querying the client for balances and transaction history. Transaction history is optional.
Optional blockchain-specific queries can be added here, such as Binance Chain market information.
Transactions
Making transfers.
Optional blockchain-specific transactions can be added here, such as Binance Chain freeze/unfreeze.
Class Variables
Client
Public variable that returns the current client
public client: XChainClient
Network
Public variable that returns the current network
public network: Network
PrivateKey
Private variable that retains the private key that was extracted from the phrase during initialisation. The phrase should not be retained.
private privkey: PrivKey | null = null
Address
Public variable that returns the address decoded from the private key during initialisation.
public address: Address
Config and Setup
Set Network
Used to set a type of Network
, which is either 'mainnet'
or 'testnet'
.
setNetwork(net: Network): XChainClient
Returns the client.
Set Phrase
Used to set the master BIP39 phrase, from which the private key is extracted and the address decoded.
setPhrase(phrase: string): address
The function should store the private key and address, then return the address generated by the phrase.
Set Service Keys
The client is expected to know which services it needs, handle errors and have hard-coded sensible defaults if errors. The client should export a type allowing devs to know which services it needs, as well as documentation to explain how to get keys.
If no key is set, either the service has no API key or the client is expected to stay within node rate limits.
Example:
- Blockchair (for querying and broadcasting)
- Ethplorer (for getting address balances)
- EthGasStation (for getting gas prices)
- Binance Chain node for Binance queries
- etc
Service keys will be passed into constructor by extending XChainClientParams
interface.
Example BitcoinClient:
// extending `XChainClientParams` to provide url and key of API service
type BitcoinClientParams = XChainClientParams & {
nodeUrl?: string
nodeApiKey?: string
}
class Client implements BitcoinClient, XChainClient {
// passing url and key of API service into constructor
constructor({ network = Network.Testnet, nodeUrl = '', nodeApiKey = '', phrase }: BitcoinClientParams) {
...
}
}
Querying
Get Explorer URLs
Returns the correctly formatted url string with paths for:
- Addresses
- Transactions
The default Explorer URL can be hard-coded, or passed in as a service. It will be provided by getExplorerUrl
getExplorerUrl(): string
To get explorer's URL for an address, use getExplorerAddressUrl
by passing an address
.
getExplorerAddressUrl = (address: Address): string
To get explorer's URL for a transaction, use getExplorerTxUrl
by passing a transaction ID.
getExplorerTxUrl = (txID: string): string
All functions should return the correctly formatted url string.
Example
https://blockchair.com/bitcoin/transaction/d11ff3352c50b1f5c8e2030711702a2071ca0e65457b40e6e0bcbea99e5dc82e
https://blockchair.com/bitcoin/address/19iqYbeATe4RxghQZJnYVFU4mjUUu76EA6
https://explorer.binance.org/tx/94F3A6257337052B04F9CC09F657966BFBD88546CA5C23F47AB0A601D29D8979
https://explorer.binance.org/address/bnb1z35wusfv8twfele77vddclka9z84ugywug48gn
https://etherscan.io/tx/0x87a4fa498cc48874631eaa776e84a49d28f42f01e22c51ff7cdfe1f2f6772f67
https://etherscan.io/address/0x8eb68e8f207be3dd1ec4baedf0b5c22245cda463
Get Balance
Returns the balance of an address.
- If address is not passed, gets the balance of the current client address.
- Optional asset can be passed, in which the query will be specific to that asset, such as ERC-20 token.
- Returns an array of assets and amounts, with assets in chain notation
CHAIN.SYMBOL-ID
getBalance(address?: Address, asset?: string): Promise<Balances>
Example of third-party service queries to get balances:
https://api.blockchair.com/bitcoin/addresses/balances?addresses=34xp4vRoCGJym3xR7yCVPFHoCNxv4Twseo
https://api.ethplorer.io/getAddressInfo/0xb00E81207bcDA63c9E290E0b748252418818c869?apiKey=freekey
https://dex.binance.org/api/v1/account/bnb1jxfh2g85q3v0tdq56fnevx6xcxtcnhtsmcu64m
Example of returned array:
[
{
"asset" : "BTC.BTC"
"amount" : 100000000
}
]
Get Transactions
Gets a simplied array of recent transactions for an address.
// Defined in xchain-client/src/types.ts
type TxHistoryParams = {
address: Address // Address to get history for
offset?: number // Optional Offset
limit?: number // Optional Limit of transactions
startTime?: Date // Optional start time
asset?: string // Optional asset. Result transactions will be filtered by this asset
}
getTransactions(params?: TxHistoryParams): Promise<TxPage>
Example of third party services to help:
// get UTXOS for address
https://api.blockchair.com/bitcoin/outputs?recipient=34xp4vRoCGJym3xR7yCVPFHoCNxv4Twseo
// get tx details for each UTXO
https://api.blockchair.com/bitcoin/dashboards/transactions/ff0bd969cce99b8d8086e452d7b63167fc178680fee796fc742cb14a9a6ef929
https://api.ethplorer.io/getAddressTransactions/0xb297cacf0f91c86dd9d2fb47c6d12783121ab780?apiKey=freekey
https://dex.binance.org/api/v1/transactions?address=bnb1jxfh2g85q3v0tdq56fnevx6xcxtcnhtsmcu64m
Example of return:
[
{
"hash" : "980D9519CCB39DC02F8B0208A4D181125EE8A2678B280AF70666288B62957DAE",
"from" : "34xp4vRoCGJym3xR7yCVPFHoCNxv4Twseo",
"to" : 34vRoCGJym3xR7yCVPFHoCNxv4Twseoxp4,
"amount": 100000000,
"asset" : "BTC.BTC",
"fee" : 2500,
"memo" : "transfer"
"date" : "2020-10-04T06:24:36.548Z"
},
{
"hash" : "0D9519CCB39DC02F8B0208A4D181125EE8A2678B280AF70666288B62957DAE98",
"from" : "34xp4vRoCGJym3xR7yCVPFHoCNxv4Twseo",
"to" : 34vRoCGJym3xR7yCVPFHoCNxv4Twseoxp4,
"amount": 200000000,
"asset" : "BTC.BTC",
"fee" : 2500,
"memo" : "transfer"
"date" : "2020-10-04T06:24:36.548Z"
},
]
Due to the complexity of this function and dependence of third-party services, this function can be omitted in early versions of the client.
Transactions
Get Fees
This function calculates and returns the fee object in a generalised way for a simple transfer function.
Since this depends on optional third-party services, sensible defaults should be hardcoded if there are errors.
The fastest fee rate should be guaranteed next block (1.5x Fast), fast should be 1-2 blocks (1x next block fee rate), average should be 2-3 blocks (0.5x Fast). Don't over-complicate this. PoW blockchains have no guarantees.
- Type should specify the units to display, or if flat fees, simply "flat". The client should interpret and display this, such as showing the user the fee rates and their units.
- Fastest (target of next block)
- Fast (target of 1-2 blocks)
- Average (target of 2-3 blocks)
Third party services: Bitcoin - returns next block feeRate (fast). Use multiples of this to extrapolate to Fastest/Average. https://api.blockchair.com/bitcoin/stats
Ethereum - returns fastest/fast/average https://ethgasstation.info/api/ethgasAPI.json?api-key=XXAPI_Key_HereXXX
getFees(): Promise<Fees>
Examples
// Bitcoin (sats/byte)
{
"type" : "byte"
"fastest" : 100
"fast" : 50
"average" : 20
}
// Ethereum (gwei)
{
"type" : "base"
"fastest" : 70
"fast" : 50
"average" : 40
}
// Binance Chain (flat rate)
{
"type" : "base"
"fastest" : 37500
"fast" : 37500
"average" : 37500
}
Transfer
General transfer function that should be signed and broadcast using a third party service. The fee should always be rate, which is units per transaction size. The size should be calculated on the fly or hardcoded:
- Bitcoin: 250 bytes is typical, so feeRate of 10 is 10 sats per byte, eg, 2500 sats
- Ethereum: gwei is standard, so a feeRate of 20 would be interpreted as 20 GWEI
- Binance Chain: fixed size, so the feeRate is ignored.
Broadcast URLs
https://api.blockchair.com/{:chain}/push/transaction
https://dex.binance.org/api/v1/broadcast
export type TxParams = {
asset: string // BTC.BTC
amount: number // in base format (10**8)
recipient: address // address
feeRate: number // optional feeRate
memo: string // optional memo to pass
}
transfer(params: TxParams): Promise<TransferResult>
The function should return the hash of the finalised transaction.
Chain Specific
Chain-specific transactions can be added as well, such as Ethereum approve()
or Binance Chain freeze()/unfreeze()
type ApproveParams = {
asset: Asset
amount: BaseAmount
spender: Address
}
approve(params: ApproveParams): Promise<TransferResult>
The function should return the hash of the finalised transaction.
Purge
When a wallet is "locked" the private key should be purged in each client by setting it back to null. Also the phrase has to be cleared this.phrase = ''
purgeClient()