How to install
npm i --save cardano-wallet
How to use
You can seek documentation here regarding how to use this package in your project.
Now remember, with great power comes great responsibility. You can now write a cardano wallet, redeem your certificates, create and sign transactions.
Example on how to retrieve a wallet from mnemonics
The example below shows you how to create/retrieve a wallet from the mnemonics and the password.
;const MNEMONICS = "crowd captain hungry tray powder motor coast oppose month shed parent mystery torch resemble index";const PASSWORD = "Cardano Rust for the winners!";// to connect the wallet to mainnetlet settings = CardanoBlockchainSettings;// recover the entropylet entropy = CardanoEntropy;// recover the walletlet wallet = CardanoBip44RootPrivateKey;// create a wallet accountlet account = wallet;let account_public = account;// create an addresslet chain_pub = account_public;let key_pub = chain_pub;let address = key_pub;console;
Create a transaction:
The example below shows how to create a transaction, this transaction is not ready to be sent through the network. It shows that there is separation of concerns between the transaction you build/prepare and signing the transaction in the last example.
// assuming the xprv and the settings from the example above are available in this scopeconst inputs =pointer: id: "0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef" index: 1 value: 1pointer: id: "fedcba9876543210fedcba9876543210fedcba9876543210fedcba9876543210" index: 0 value: 1;const outputs =address: "Ae2tdPwUPEZCEhYAUVU7evPfQCJjyuwM6n81x6hSjU9TBMSy2YwZEVydssL" value: "1826205";// the fee algorithm (i.e. the function to compute the fees of a transaction)const fee_algorithm = WalletLinearFeeAlgorithm;let transaction_builder = ;for let index = 0; index < inputslength; index++const pointer = WalletTxoPointer;const value = WalletCoin;transaction_builder;for let index = 0; index < outputslength; index++const txout = WalletTxOut;transaction_builder;// verify the balance and the fees:const balance = transaction_builder;if balanceconsole;throw Error"Not enough inputs";elseif balanceconsole;elseconsole;// Warning: this function does not throw exception if the transaction is not// balanced. This is your job to make sure your transaction's inputs and outputs// and fees are balanced.let transaction = transaction_builder;
Signing a transaction
This function shows how to sign a transaction so it can be accepted by the network.
You need to make sure:
- the key_prv correspond to the private key associated to the address for the given input (see UTxO based crypto-currency model/documentations);
- the signatures are added in the same order the inputs of this transaction were added.
// retrieve the prepared transaction from the previous examplelet transaction_finalizer = transaction;for let index = 0; index < inputslength; index++const witness = WalletWitness;transaction_finalizer;// at this stage the transaction is ready to be sentconst signed_transaction = transaction_finalizer;console;console;
How to build (locally)
wasm-pack build --target browser npm install npm run serve