This is emitted whenever the wallet finds a new transaction.
See the incomingtx and outgoingtx events if you need more fine grained control.
Example:
wallet.on('transaction', (transaction) => {
console.log(`Transaction of ${transaction.totalAmount()} received!`);
});
This is emitted whenever the wallet finds an incoming transaction.
Example:
wallet.on('incomingtx', (transaction) => {
console.log(`Incoming transaction of ${transaction.totalAmount()} received!`);
});
This is emitted whenever the wallet finds an outgoing transaction.
Example:
wallet.on('outgoingtx', (transaction) => {
console.log(`Outgoing transaction of ${transaction.totalAmount()} received!`);
});
This is emitted whenever the wallet finds a fusion transaction.
Example:
wallet.on('fusiontx', (transaction) => {
console.log('Fusion transaction found!');
});
This is emitted whenever the wallet creates and sends a transaction.
This is distinct from the outgoingtx event, as this event is fired when we send a transaction, while outgoingtx is fired when the tx is included in a block, and scanned by the wallet.
Example:
wallet.on('createdtx', (transaction) => {
console.log('Transaction created!');
});
This is emitted whenever the wallet creates and sends a fusion transaction.
Example:
wallet.on('createdfusiontx', (transaction) => {
console.log('Fusion transaction created!');
});
This is emitted whenever the wallet first syncs with the network. It will also be fired if the wallet unsyncs from the network, then resyncs.
Example:
wallet.on('sync', (walletHeight, networkHeight) => {
console.log(`Wallet synced! Wallet height: ${walletHeight}, Network height: ${networkHeight}`);
});
This is emitted whenever the wallet first desyncs with the network. It will only be fired after the wallet has initially fired the sync event.
Example:
wallet.on('desync', (walletHeight, networkHeight) => {
console.log(`Wallet is no longer synced! Wallet height: ${walletHeight}, Network height: ${networkHeight}`);
});
This is emitted whenever the wallet fails to contact the underlying daemon. This event will only be emitted on the first disconnection. It will not be emitted again, until the daemon connects, and then disconnects again.
Example:
wallet.on('disconnect', (error) => {
console.log('Possibly lost connection to daemon: ' + error.toString());
});
Note that these events will only be emitted if using the Daemon daemon type, as the other daemon types are considered legacy and are not having new features added.
This is emitted whenever the wallet previously failed to contact the underlying daemon, and has now reconnected. This event will only be emitted on the first connection. It will not be emitted again, until the daemon disconnects, and then reconnects again.
Example:
wallet.on('connect', () => {
console.log('Regained connection to daemon!');
});
Note that these events will only be emitted if using the Daemon daemon type, as the other daemon types are considered legacy and are not having new features added.
This is emitted whenever the walletBlockCount (Amount of blocks the wallet has synced), localDaemonBlockCount (Amount of blocks the daemon you're connected to has synced), or networkBlockCount (Amount of blocks the network has) changes.
This can be used in place of repeatedly polling getSyncStatus
Example:
wallet.on('heightchange', (walletBlockCount, localDaemonBlockCount, networkBlockCount) => {
console.log(`New sync status: ${walletBlockCount} / ${localDaemonBlockCount}`);
});
This is emitted when we consider the node to no longer be online. There are a few categories we use to determine this.
We have not recieved any data from /getwalletsyncdata since the configured timeout. (Default 3 mins)
The network height has not changed since the configured timeout (Default 3 mins)
The local daemon height has not changed since the configured timeout (Default 3 mins)
Example:
wallet.on('deadnode', () => {
console.log('Ruh roh, looks like the daemon is dead.. maybe you want to swapNode()?');
});
This is emitted every time we download a block from the daemon. Will only be emitted if the daemon is using /getrawblocks (All non blockchain cache daemons should support this).
This block object is an instance of the Block turtlecoin-utils class. See the Utils docs for further info on using this value.
Note that a block emitted after a previous one could potentially have a lower height, if a blockchain fork took place.
Example:
daemon.on('rawblock', (block) => {
console.log(`Downloaded new block ${block.hash}`);
});
This is emitted every time we download a transaction from the daemon. Will only be emitted if the daemon is using /getrawblocks (All non blockchain cache daemons should support this).
This transaction object is an instance of the Transaction turtlecoin-utils class. See the Utils docs for further info on using this value.
Note that a transaction emitted after a previous one could potentially have a lower height in the chain, if a blockchain fork took place.
Example:
daemon.on('rawtransaction', (block) => {
console.log(`Downloaded new transaction ${transaction.hash}`);
});
This is emitted when an error occurs during auto optimization
Example:
wallet.on('autooptimizeError', (error) => {
console.error('Error: ', error);
}
@event This is emitted when an error occurs during auto optimization
Whether we should automatically keep the wallet optimized
Are we in the middle of an optimization?
Are we in the middle of a transaction?
Interface to either a regular daemon or a blockchain cache api
Update daemon info every n seconds
External function to process a blocks outputs.
We only want to submit dead node once, then reset the flag when we swap node or the node comes back online.
Check on locked tx status every n seconds
Previously prepared transactions for later sending.
Should we perform auto optimization when next synced
Have we started the mainloop
Contains private keys, transactions, inputs, etc
Executes the main loop every n seconds for us
Whether our wallet is synced. Used for selectively firing the sync/desync event.
Wallet synchronization state
Sets or gets the default captureRejection value for all emitters.
This symbol shall be used to install a listener for only monitoring 'error'
events. Listeners installed using this symbol are called before the regular
'error'
listeners are called.
Installing a listener using this symbol does not change the behavior once an
'error'
event is emitted, therefore the process will still crash if no
regular 'error'
listener is installed.
Adds a subwallet to the wallet container. Must not be used on a view only wallet. For more information on subwallets, see https://docs.turtlecoin.lol/developer/subwallets
Example:
const [address, error] = await wallet.addSubWallet();
if (!error) {
console.log(`Created subwallet with address of ${address}`);
}
Returns the newly created address or an error.
Remove any transactions that have been cancelled
Delete a prepared transaction stored to free up RAM. Returns whether the transaction was found and has been removed, or false if it was not found.
Example:
const destinations = [
['TRTLxyz...', 1000],
['TRTLzyx...', 10000],
];
const creation = await wallet.sendTransactionAdvanced(
destinations,
undefined, // mixin
undefined, // fee
undefined, // payment ID
undefined, // subWalletsToTakeFrom
undefined, // changeAddress
false // relay to network
);
if (creation.success)
// Inspect certain transaction properties before sending if desired
if (creation.fee > 100000) {
console.log('Fee is quite high! You may wish to attempt optimizing your wallet');
return;
}
const result = await wallet.sendRawPreparedTransaction(creation.preparedTransaction);
if (result.success) {
console.log(`Sent transaction, hash ${result.transactionHash}, fee ${WB.prettyPrintAmount(result.fee)}`);
} else {
console.log(`Failed to relay transaction: ${result.error.toString()}`);
}
} else {
console.log(`Failed to send transaction: ${creation.error.toString()}`);
wallet.deletePreparedTransaction(creation.transactionHash);
}
Deletes all prepared transactions.
Example:
wallet.deletePreparedTransactions();
Removes the subwallet specified from the wallet container. If you have not backed up the private keys for this subwallet, all funds in it will be lost.
Example:
const error = await wallet.deleteSubWallet('TRTLv2txGW8daTunmAVV6dauJgEv1LezM2Hse7EUD5c11yKHsNDrzQ5UWNRmu2ToQVhDcr82ZPVXy4mU5D7w9RmfR747KeXD3UF');
if (error) {
console.log(`Failed to delete subwallet: ${error.toString()}`);
}
The subwallet address to remove
This flag will automatically send fusion transactions when needed to keep your wallet permanently optimized.
The downsides are that sometimes your wallet will 'unexpectedly' have locked funds.
The upside is that when you come to sending a large transaction, it should nearly always succeed.
This flag is ENABLED by default.
Example:
wallet.enableAutoOptimization(false);
Should we automatically keep the wallet optimized?
Encrypt the wallet using the given password. Password may be empty. Note that an empty password does not mean an unencrypted wallet - simply a wallet encrypted with the empty string.
This will take some time (Roughly a second on a modern PC) - it runs 500,000 iterations of pbkdf2.
Example:
const saved = wallet.encryptWalletToString('hunter2');
The password to encrypt the wallet with
Returns the encrypted wallet as astring.
Gets the address of every subwallet in this container.
Example:
let i = 1;
for (const address of wallet.getAddresses()) {
console.log(`Address [${i}]: ${address}`);
i++;
}
Get the unlocked and locked balance for the wallet container.
Example:
const [unlockedBalance, lockedBalance] = await wallet.getBalance();
The addresses to check the balance of. If not given, defaults to all addresses.
Returns a string indicating the type of cryptographic functions being used.
Example:
const cryptoType = wallet.getCryptoType();
console.log(`Wallet is using the ${cryptoType} cryptographic library.`);
Gets information on the currently connected daemon - It's host, port,
daemon type, and ssl presence.
This can be helpful if you are taking arbitary host/port from a user,
and wish to display the daemon type they are connecting to once we
have figured it out.
Note that the ssl
and daemonType
variables may have not been
determined yet - If you have not awaited start yet, or if the daemon
is having connection issues.
For this reason, there are two additional properties - sslDetermined
,
and daemonTypeDetermined
which let you verify that we have managed
to contact the daemon and detect its specifics.
Example:
const daemonInfo = wallet.getDaemonConnectionInfo();
console.log(`Connected to ${daemonInfo.ssl ? 'https://' : 'http://'}${daemonInfo.host}:${daemonInfo.port}`);
Get the global indexes for a range of blocks
When we get the global indexes, we pass in a range of blocks, to obscure which transactions we are interested in - the ones that belong to us. To do this, we get the global indexes for all transactions in a range.
For example, if we want the global indexes for a transaction in block 17, we get all the indexes from block 10 to block 20.
Get the primary address mnemonic seed. If the primary address isn't a deterministic wallet, it will return a WalletError.
Example:
const [seed, err] = await wallet.getMnemonicSeed();
if (err) {
console.log('Wallet is not a deterministic wallet: ' + err.toString());
}
Get the mnemonic seed for the specified address. If the specified address is invalid or the address isn't a deterministic wallet, it will return a WalletError.
Example:
const [seed, err] = await wallet.getMnemonicSeedForAddress('TRTLxyz...');
if (err) {
console.log('Address does not belong to a deterministic wallet: ' + err.toString());
}
A valid address that exists in this container
Get the node fee the daemon you are connected to is charging for
transactions. If the daemon charges no fee, this will return ['', 0]
Fees returned will be zero if you have not yet awaited start.
Example:
const [nodeFeeAddress, nodeFeeAmount] = wallet.getNodeFee();
if (nodeFeeAmount === 0) {
console.log('Yay, no fees!');
}
Get the number of transactions belonging to the given subWallet. If no subWallet is given, gets the total number of transactions in the wallet container. Can be used if you want to avoid fetching all transactions repeatedly when nothing has changed.
Note that it probably is more effective to subscribe to the transaction related events to update your UI, rather than polling for the number of transactions.
Example:
let numTransactions = 0;
while (true) {
const tmpNumTransactions = await wallet.getNumTransactions();
if (numTransactions != tmpNumTransactions) {
console.log(tmpNumTransactions - numTransactions + ' new transactions found!');
numTransactions = tmpNumTransactions;
}
}
Should we only count transactions of the specified subWallet?
Should we count fusion transactions? Defaults to true.
Gets all prepared transactions.
Example:
const preparedTransactions = wallet.getPreparedTransactions();
Gets the primary address of a wallet container. The primary address is the address that was created first in the wallet container.
Example:
const address = wallet.getPrimaryAddress();
Gets the private spend and private view for the primary address. The primary address is the first created wallet in the container.
Example:
const [privateSpendKey, privateViewKey] = wallet.getPrimaryAddressPrivateKeys();
Gets the shared private view key for this wallet container.
Example:
const privateViewKey = wallet.getPrivateViewKey();
Gets the publicSpendKey and privateSpendKey for the given address, if possible.
Note: secret key will be 00000... (64 zeros) if this wallet is a view only wallet.
Example:
const [publicSpendKey, privateSpendKey, err] = await wallet.getSpendKeys('TRTLxyz...');
if (err) {
console.log('Failed to get spend keys for address: ' + err.toString());
}
A valid address in this container, to get the spend keys of
Gets the wallet, local daemon, and network block count
Example:
const [walletBlockCount, localDaemonBlockCount, networkBlockCount] =
wallet.getSyncStatus();
Gets the specified transaction, if it exists in this wallet container.
Example:
const tx = await wallet.getTransaction('693950eeec41dc36cfc5109eba15807ce3d63eff21f1eec20a7d1bda99563b1c');
if (tx) {
console.log(`Tx ${tx.hash} is worth ${WB.prettyPrintAmount(tx.totalAmount())}`);
} else {
console.log("Couldn't find transaction! Is your wallet synced?");
}
The hash of the transaction to get
Gets all the transactions in the wallet container unless a subWallet address is specified, in which case we get only the transactions for that subWallet.
Newer transactions are at the front of the array - Unconfirmed transactions come at the very front.
Example:
for (const tx of await wallet.getTransactions()) {
console.log(`Transaction ${tx.hash} - ${WB.prettyPrintAmount(tx.totalAmount())} - ${tx.timestamp}`);
}
Index to start taking transactions from
Number of transactions to take
Should we include fusion transactions?
Should we only include transactions of the specified subWallet?
Returns the number of subwallets in this wallet.
Example:
const count = wallet.getWalletCount();
console.log(`Wallet has ${count} subwallets`);
Imports a subwallet to the wallet container. Must not be used on a view only wallet. For more information on subwallets, see https://docs.turtlecoin.lol/developer/subwallets
Example:
const [address, error] = await wallet.importSubWallet('c984628484a1a5eaab4cfb63831b2f8ac8c3a56af2102472ab35044b46742501');
if (!error) {
console.log(`Imported subwallet with address of ${address}`);
} else {
console.log(`Failed to import subwallet: ${error.toString()}`);
}
The private spend key of the subwallet to import
The scan height to start scanning this subwallet from. If the scan height is less than the wallets current height, the entire wallet will be rewound to that height, and will restart syncing. If not specified, this defaults to the current height.
Returns the newly created address or an error.
Imports a view only subwallet to the wallet container. Must not be used on a non view wallet. For more information on subwallets, see https://docs.turtlecoin.lol/developer/subwallets
Example:
const [address, error] = await wallet.importViewSubWallet('c984628484a1a5eaab4cfb63831b2f8ac8c3a56af2102472ab35044b46742501');
if (!error) {
console.log(`Imported view subwallet with address of ${address}`);
} else {
console.log(`Failed to import view subwallet: ${error.toString()}`);
}
The public spend key of the subwallet to import
The scan height to start scanning this subwallet from. If the scan height is less than the wallets current height, the entire wallet will be rewound to that height, and will restart syncing. If not specified, this defaults to the current height.
Returns the newly created address or an error.
Initialize stuff not stored in the JSON.
Exposes some internal functions for those who know what they're doing...
Example:
const syncFunc = wallet.internal().sync;
await syncFunc(true);
Returns an object with two members, sync(), and updateDaemonInfo().
Optimizes your wallet as much as possible. It will optimize every single subwallet correctly, if you have multiple subwallets. Note that this method does not wait for the funds to return to your wallet before returning, so, it is likely balances will remain locked.
Note that if you want to alert the user in real time of the hashes or
number of transactions sent, you can subscribe to the createdfusiontx
event. This will be fired every time a fusion transaction is sent.
You may also want to consider manually creating individual transactions if you want more control over the process. See sendFusionTransactionBasic.
This method may take a very long time if your wallet is not optimized at all. It is suggested to not block the UI/mainloop of your program when using this method.
Example:
const [numberOfTransactionsSent, hashesOfSentFusionTransactions] = await wallet.optimize();
console.log(`Sent ${numberOfTransactionsSent} fusion transactions, hashes: ${hashesOfSentFusionTransactions.join(', ')}`);
Since we're going to use optimize() with auto optimizing, and auto optimizing is enabled by default, we have to ensure we only optimize a single wallet at once. Otherwise, we'll end up with everyones balance in the primary wallet.
Process config.blocksPerTick stored blocks, finding transactions and inputs that belong to us
Performs the same operation as reset(), but uses the initial scan height or timestamp. For example, if you created your wallet at block 800,000, this method would start rescanning from then.
This function will return once the wallet has been successfully reset, and syncing has began again.
Example:
await wallet.rescan();
Discard all transaction data, and begin scanning the wallet again from the scanHeight or timestamp given. Defaults to a height of zero, if not given.
This function will return once the wallet has been successfully reset, and syncing has began again.
Example:
await wallet.reset(123456);
The scan height to begin scanning transactions from
The timestamp to being scanning transactions from
The difference is that while reset and rescan discard all progress before the specified height, and then continues syncing from there, rewind instead retains the information previous, and only removes information after the rewind height.
This can be helpful if you suspect a transaction has been missed by the sync process, and want to only rescan a small section of blocks.
Example:
await wallet.rewind(123456);
The scan height to rewind to
Save the wallet to the given filename. Password may be empty, but filename must not be. Note that an empty password does not mean an unencrypted wallet - simply a wallet encrypted with the empty string.
This will take some time (Roughly a second on a modern PC) - it runs 500,000 iterations of pbkdf2.
Example:
const saved = wallet.saveWalletToFile('test.wallet', 'hunter2');
if (!saved) {
console.log('Failed to save wallet!');
}
The file location to save the wallet to.
The password to encrypt the wallet with
Returns a boolean indicating success.
Most people don't mine blocks, so by default we don't scan them. If you want to scan them, flip it on/off here.
Example:
wallet.scanCoinbaseTransactions(true);
Should we scan coinbase transactions?
Sends a fusion transaction, if possible. Fusion transactions are zero fee, and optimize your wallet for sending larger amounts. You may (probably will) need to perform multiple fusion transactions.
If you want to ensure your wallet gets fully optimized, consider using optimize.
All parameters are optional.
Example:
const result = await wallet.sendFusionTransactionAdvanced(3, undefined, 'TRTLxyz..');
if (result.success) {
console.log(`Sent transaction, hash ${result.transactionHash}, fee ${WB.prettyPrintAmount(result.fee)}`);
} else {
console.log(`Failed to send transaction: ${result.error.toString()}`);
}
The amount of input keys to hide your input with. Your network may enforce a static mixin.
The addresses of the subwallets to draw funds from.
The destination for the fusion transaction to be sent to. Must be an address existing in this container.
Extra arbitrary data to include in the transaction
Sends a fusion transaction, if possible. Fusion transactions are zero fee, and optimize your wallet for sending larger amounts. You may (probably will) need to perform multiple fusion transactions.
If you want to ensure your wallet gets fully optimized, consider using optimize.
Example:
const result = await wallet.sendFusionTransactionBasic();
if (result.success) {
console.log(`Sent transaction, hash ${result.transactionHash}`);
} else {
console.log(`Failed to send transaction: ${result.error.toString()}`);
}
Relays a previously prepared transaction to the network.
Example:
const destinations = [
['TRTLxyz...', 1000],
['TRTLzyx...', 10000],
];
const creation = await wallet.sendTransactionAdvanced(
destinations,
undefined, // mixin
undefined, // fee
undefined, // payment ID
undefined, // subWalletsToTakeFrom
undefined, // changeAddress
false // relay to network
);
if (creation.success)
// Inspect certain transaction properties before sending if desired
if (creation.fee > 100000) {
console.log('Fee is quite high! You may wish to attempt optimizing your wallet');
return;
}
const result = await wallet.sendPreparedTransaction(creation.transactionHash);
if (result.success) {
console.log(`Sent transaction, hash ${result.transactionHash}, fee ${WB.prettyPrintAmount(result.fee)}`);
} else {
console.log(`Failed to relay transaction: ${result.error.toString()}`);
}
} else {
wallet.deletePreparedTransaction(creation.transactionHash);
console.log(`Failed to send transaction: ${creation.error.toString()}`);
}
Relays a previously prepared transaction to the network. Data can be stored client side if you wish for prepared transactions to still be usable after restarting the wallet app, for example.
Example:
const destinations = [
['TRTLxyz...', 1000],
['TRTLzyx...', 10000],
];
const creation = await wallet.sendTransactionAdvanced(
destinations,
undefined, // mixin
undefined, // fee
undefined, // payment ID
undefined, // subWalletsToTakeFrom
undefined, // changeAddress
false // relay to network
);
if (creation.success)
// Inspect certain transaction properties before sending if desired
if (creation.fee > 100000) {
console.log('Fee is quite high! You may wish to attempt optimizing your wallet');
return;
}
const result = await wallet.sendRawPreparedTransaction(creation.preparedTransaction);
if (result.success) {
console.log(`Sent transaction, hash ${result.transactionHash}, fee ${WB.prettyPrintAmount(result.fee)}`);
} else {
console.log(`Failed to relay transaction: ${result.error.toString()}`);
}
} else {
console.log(`Failed to send transaction: ${creation.error.toString()}`);
wallet.deletePreparedTransaction(creation.transactionHash);
}
Sends a transaction, which permits multiple amounts to different destinations, specifying the mixin, fee, subwallets to draw funds from, and change address.
All parameters are optional aside from destinations.
Example:
const destinations = [
['TRTLxyz...', 1000],
['TRTLzyx...', 10000],
];
const result = await wallet.sendTransactionAdvanced(
destinations,
undefined,
undefined,
'c59d157d1d96f280ece0816a8925cae8232432b7235d1fa92c70faf3064434b3'
);
if (result.success) {
console.log(`Sent transaction, hash ${result.transactionHash}, fee ${WB.prettyPrintAmount(result.fee)}`);
} else {
console.log(`Failed to send transaction: ${result.error.toString()}`);
}
An array of destinations, and amounts to send to that destination. Amounts are in ATOMIC units.
The amount of input keys to hide your input with. Your network may enforce a static mixin.
The network fee, fee per byte, or minimum fee to use with this transaction. Defaults to minimum fee.
The payment ID to include with this transaction. Defaults to none.
The addresses of the subwallets to draw funds from. Defaults to all addresses.
The address to send any returned change to. Defaults to the primary address.
Whether we should submit the transaction to the network or not. If set to false, allows you to review the transaction fee before sending it. Use sendPreparedTransaction to send a transaction that you have not relayed to the network. Defaults to true.
Whether we should send the entire balance available. Since fee per
byte means estimating fees is difficult, we can handle that process
on your behalf. The entire balance minus fees will be sent to the
first destination address. The amount given in the first destination
address will be ignored. Any following destinations will have
the given amount sent. For example, if your destinations array was
[['address1', 0], ['address2', 50], ['address3', 100]]
Then address2 would be sent 50, address3 would be sent 100,
and address1 would get whatever remains of the balance
after paying node/network fees.
Defaults to false.
Extra arbitrary data to include in the transaction
Sends a transaction of amount to the address destination, using the given payment ID, if specified.
Network fee is set to default, mixin is set to default, all subwallets are taken from, primary address is used as change address.
If you need more control, use sendTransactionAdvanced.
Example:
const result = await wallet.sendTransactionBasic('TRTLxyz...', 1234);
if (result.success) {
console.log(`Sent transaction, hash ${result.transactionHash}, fee ${WB.prettyPrintAmount(result.fee)}`);
} else {
console.log(`Failed to send transaction: ${result.error.toString()}`);
}
The address to send the funds to
The amount to send, in ATOMIC units
The payment ID to include with this transaction. Optional.
Returns either an error, or the transaction hash.
Provide a function to process blocks instead of the inbuilt one. The only use for this is to leverage native code to provide quicker cryptography functions - the default JavaScript is not that speedy.
Note that if you're in a node environment, this library will use C++ code with node-gyp, so it will be nearly as fast as C++ implementations. You only need to worry about this in less conventional environments, like react-native, or possibly the web.
If you don't know what you're doing, DO NOT TOUCH THIS - YOU WILL BREAK WALLET SYNCING
Note you don't have to set the globalIndex properties on returned inputs. We will fetch them from the daemon if needed. However, if you have them, return them, to save us a daemon call.
Your function should return an array of [publicSpendKey, TransactionInput]
.
The public spend key is the corresponding subwallet that the transaction input
belongs to.
Return an empty array if no inputs are found that belong to the user.
Example:
wallet.setBlockOutputProcessFunc(mySuperSpeedyFunction);
The function to process block outputs.
Sets the log level. Log messages below this level are not shown.
Logging by default occurs to stdout. See setLoggerCallback to modify this, or gain more control over what is logged.
Example:
wallet.setLogLevel(WB.LogLevel.DEBUG);
The level to log messages at.
Sets a callback to be used instead of console.log for more fined control of the logging output.
Ensure that you have enabled logging for this function to take effect. See setLogLevel for more details.
Example:
wallet.setLoggerCallback((prettyMessage, message, level, categories) => {
if (categories.includes(WB.LogCategory.SYNC)) {
console.log(prettyMessage);
}
});
The callback to use for log messages
Initializes and starts the wallet sync process. You should call this function before enquiring about daemon info or fee info. The wallet will not process blocks until you call this method.
Example:
await wallet.start();
The inverse of the start method, this pauses the blockchain sync process.
If you want the node process to close cleanly (i.e, without using process.exit()
),
you need to call this function. Otherwise, the library will keep firing
callbacks, and so your script will hang.
Example:
wallet.stop();
Stores any transactions, inputs, and spend keys images
Swaps the currently connected daemon with a different one. If the wallet is currently started, it will remain started after the node is swapped, if it is currently stopped, it will remain stopped.
Example:
const daemon = new WB.Daemon('blockapi.turtlepay.io', 443);
await wallet.swapNode(daemon);
const daemonInfo = wallet.getDaemonConnectionInfo();
console.log(`Connected to ${daemonInfo.ssl ? 'https://' : 'http://'}${daemonInfo.host}:${daemonInfo.port}`);
Main loop. Download blocks, process them.
Converts recursively to JSON. Should be used in conjuction with JSON.stringify. Example:
JSON.stringify(wallet, null, 4);
Converts the wallet into a JSON string. This can be used to later restore the wallet with loadWalletFromJSON.
Example:
const walletData = wallet.toJSONString();
Update daemon status
Returns a boolean indicating whether or not the wallet is using native crypto
Example:
const native = wallet.usingNativeCrypto();
if (native) {
console.log('Wallet is using native cryptographic code.');
}
This method creates a new wallet instance with a random key pair.
Example:
const WB = require('turtlecoin-wallet-backend');
const daemon = new WB.Daemon('127.0.0.1', 11898);
const wallet = await WB.WalletBackend.createWallet(daemon);
This method imports a wallet you have previously created, in a 'watch only' state. This wallet can view incoming transactions, but cannot send transactions. It also cannot view outgoing transactions, so balances may appear incorrect. This is useful for viewing your balance whilst not risking your funds or private keys being stolen.
Example:
const WB = require('turtlecoin-wallet-backend');
const daemon = new WB.Daemon('127.0.0.1', 11898);
const privateViewKey = 'ce4c27d5b135dc5310669b35e53efc9d50d92438f00c76442adf8c85f73f1a01';
const address = 'TRTLv2Fyavy8CXG8BPEbNeCHFZ1fuDCYCZ3vW5H5LXN4K2M2MHUpTENip9bbavpHvvPwb4NDkBWrNgURAd5DB38FHXWZyoBh4wW';
const [wallet, err] = await WB.WalletBackend.importViewWallet(daemon, 100000, privateViewKey, address);
if (err) {
console.log('Failed to load wallet: ' + err.toString());
}
An implementation of the Daemon interface.
The height to begin scanning the blockchain from. This can greatly increase sync speeds if given. Defaults to zero.
The private view key of this view wallet. Should be a 64 char hex string.
The public address of this view wallet.
Imports a wallet from a pair of private keys.
Example:
const WB = require('turtlecoin-wallet-backend');
const daemon = new WB.Daemon('127.0.0.1', 11898);
const privateViewKey = 'ce4c27d5b135dc5310669b35e53efc9d50d92438f00c76442adf8c85f73f1a01';
const privateSpendKey = 'f1b1e9a6f56241594ddabb243cdb39355a8b4a1a1c0343dde36f3b57835fe607';
const [wallet, err] = await WB.WalletBackend.importWalletFromSeed(daemon, 100000, privateViewKey, privateSpendKey);
if (err) {
console.log('Failed to load wallet: ' + err.toString());
}
An implementation of the Daemon interface.
The height to begin scanning the blockchain from. This can greatly increase sync speeds if given. Defaults to zero.
The private view key to import. Should be a 64 char hex string.
The private spend key to import. Should be a 64 char hex string.
Imports a wallet from a 25 word mnemonic seed.
Example:
const WB = require('turtlecoin-wallet-backend');
const daemon = new WB.Daemon('127.0.0.1', 11898);
const seed = 'necklace went vials phone both haunted either eskimos ' +
'dialect civilian western dabbing snout rustled balding ' +
'puddle looking orbit rest agenda jukebox opened sarcasm ' +
'solved eskimos';
const [wallet, err] = await WB.WalletBackend.importWalletFromSeed(daemon, 100000, seed);
if (err) {
console.log('Failed to load wallet: ' + err.toString());
}
An implementation of the Daemon interface.
The height to begin scanning the blockchain from. This can greatly increase sync speeds if given. Defaults to zero if not given.
The mnemonic seed to import. Should be a 25 word string.
The height to begin scanning the blockchain from.
This can greatly increase sync speeds if given.
Set to zero if newWallet
is true
.
Are we creating a new wallet? If so, it will start syncing from the current time.
Omit this parameter to create a view wallet.
Loads a wallet from a JSON encoded string. For the correct format for the JSON to use, see https://github.com/turtlecoin/wallet-file-interaction
You can obtain this JSON using toJSONString.
Example:
const WB = require('turtlecoin-wallet-backend');
const daemon = new WB.Daemon('127.0.0.1', 11898);
const [wallet, err] = await WB.WalletBackend.loadWalletFromJSON(daemon, json);
if (err) {
console.log('Failed to load wallet: ' + err.toString());
}
An implementation of the Daemon interface.
Wallet info encoded as a JSON encoded string. Note
that this should be a string, NOT a JSON object.
This function will call JSON.parse()
, so you should
not do that yourself.
This method opens a password protected wallet from an encrypted string. The password protection follows the same format as wallet-api, zedwallet-beta, and WalletBackend. It does NOT follow the same format as turtle-service or zedwallet, and will be unable to open wallets created with this program.
Example:
const WB = require('turtlecoin-wallet-backend');
const daemon = new WB.Daemon('127.0.0.1', 11898);
const data = 'ENCRYPTED_WALLET_STRING';
const [wallet, error] = await WB.WalletBackend.openWalletFromEncryptedString(daemon, data, 'hunter2');
if (err) {
console.log('Failed to open wallet: ' + err.toString());
}
The encrypted string representing the wallet data
The password to use to decrypt the wallet. May be blank.
This method opens a password protected wallet from a filepath. The password protection follows the same format as wallet-api, zedwallet-beta, and WalletBackend. It does NOT follow the same format as turtle-service or zedwallet, and will be unable to open wallets created with this program.
Example:
const WB = require('turtlecoin-wallet-backend');
const daemon = new WB.Daemon('127.0.0.1', 11898);
const [wallet, error] = await WB.WalletBackend.openWalletFromFile(daemon, 'mywallet.wallet', 'hunter2');
if (err) {
console.log('Failed to open wallet: ' + err.toString());
}
The location of the wallet file on disk
The password to use to decrypt the wallet. May be blank.
Generated using TypeDoc
The WalletBackend provides an interface that allows you to synchronize with a daemon, download blocks, process them, and pick out transactions that belong to you. It also allows you to inspect these transactions, view your balance, send transactions, and more.