npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

cardano-wallet

v1.2.2

Published

Cardano Wallet, from rust to JS via Wasm

Downloads

246

Readme

Cardano Wallet

This is a new version of the Cardano Wasm binding for Icarus/Yoroi. It exposes everything one needs to be able to build a cardano wallet in javascript.

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.

import * as Cardano from "cardano-wallet";

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 mainnet
let settings = Cardano.BlockchainSettings.mainnet();

// recover the entropy
let entropy = Cardano.Entropy.from_english_mnemonics(MNEMONICS);
// recover the wallet
let wallet = Cardano.Bip44RootPrivateKey.recover(entropy, PASSWORD);

// create a wallet account
let account = wallet.bip44_account(Cardano.AccountIndex.new(0 | 0x80000000));
let account_public = account.public();

// create an address
let chain_pub = account_public.bip44_chain(false);
let key_pub = chain_pub.address_key(Cardano.AddressKeyIndex.new(0));
let address = key_pub.bootstrap_era_address(settings);

console.log("Address m/bip44/ada/'0/0/0", address.to_base58());

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 scope

const inputs = [
    { pointer: { id: "0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef", index: 1 }, value: 1 },
    { pointer: { 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 = Wallet.LinearFeeAlgorithm.default();

let transaction_builder = new Wallet.TransactionBuilder();

for (let index = 0; index < inputs.length; index++) {
    const pointer = Wallet.TxoPointer.from_json(inputs[index].pointer);
    const value = Wallet.Coin.from(inputs[index].value, 0);
    transaction_builder.add_input(pointer, value);
}

for (let index = 0; index < outputs.length; index++) {
    const txout = Wallet.TxOut.from_json(outputs[index]);
    transaction_builder.add_output(txout);
}

// verify the balance and the fees:
const balance = transaction_builder.get_balance(fee_algorithm);
if (balance.is_negative()) {
    console.error("not enough inputs, ", balance.value().to_str());
    throw Error("Not enough inputs");
} else {
    if (balance.is_zero()) {
    console.info("Perfect balance no dust");
    } else {
    console.warn("Loosing some coins in extra fees: ", balance.value().to_str());
    }
}

// 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.make_transaction();

Signing a transaction

This function shows how to sign a transaction so it can be accepted by the network.

You need to make sure:

  1. the key_prv correspond to the private key associated to the address for the given input (see UTxO based crypto-currency model/documentations);
  2. the signatures are added in the same order the inputs of this transaction were added.
// retrieve the prepared transaction from the previous example
let transaction_finalizer = new Wallet.TransactionFinalized(transaction);

for (let index = 0; index < inputs.length; index++) {
    const witness = Wallet.Witness.new_extended_key(
        settings,
        key_prv,
        transaction_finalizer.id()
    );
    transaction_finalizer.add_witness(witness);
}

// at this stage the transaction is ready to be sent
const signed_transaction = transaction_finalizer.finalize();
console.log("ready to send transaction: ", signed_transaction.to_hex());
console.log(signed_transaction.to_json());

How to build (locally)

wasm-pack build --target browser
npm install
npm run serve