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

@astra-js/account

v0.1.59

Published

account and wallet for astra

Downloads

7

Readme

@astra-js/account

This package provides a collection of apis to create accounts and wallets and sign using them. A wallet can hold multiple accounts and account is associated with a unique one address. This package also provides facilies to manage account keys.

Installation

npm install @astra-js/account

Usage

Creating new account and display addresses

const account = new Account(); // or const account = Account.new()
console.log(account.checksumAddress);
console.log(account.address);

Creating new account using private key

const account = Account.add('45e497bd45a9049bcb649016594489ac67b9f052a6cdf5cb74ee2427a60bf25e');

Creating account using private key and custom messenger

const account = new Account(
  '45e497bd45a9049bcb649016594489ac67b9f052a6cdf5cb74ee2427a60bf25e',
  new Messenger(
    new HttpProvider('https://rpc.s0.t.astranetwork.com'),
    ChainType.Astra,
    ChainID.AstraTestnet,
  ),
);

Creating account and setting custom messenger

// uses by default http://localhost:9500 as messenger
const account = new Account();
const customMessenger = new Messenger(
    new HttpProvider('https://rpc.s0.t.astranetwork.com'),
    ChainType.Astra,
    ChainID.AstraTestnet,
  );

account.setMessenger(customMessenger);

Storing the account data to keystore file

const passphrase = '';
const account = new Account('45e497bd45a9049bcb649016594489ac67b9f052a6cdf5cb74ee2427a60bf25e');
account.toFile(passphrase).then(keystore => {
    console.log(keystore);
});

Fetching account from keystore file

const passphrase = '';
const keystore = '{"version":3,"id":"33363566-3564-4264-a638-363531666335","address":"7c41e0668b551f4f902cfaec05b5bdca68b124ce","crypto":{"ciphertext":"9b09380afb742838b32d9afc0ec1a3df35dbd7a41e3a160d08c07a4d0e79b855","cipherparams":{"iv":"1cd0e0522260eef055b9170f4825f4a0"},"cipher":"aes-128-ctr","kdf":"scrypt","kdfparams":{"salt":"bf35e36c45cccefcef73a4c900f41c682c94c28630d94d2d1f764760d245f30b","n":8192,"r":8,"p":1,"dklen":32},"mac":"25b4442972356bea02af57eba3b87803086d90b5e7657a57b528b89b1aa25f2f"}}';
const account = new Account();
account.fromFile(keystore, passphrase).then(account => {
    console.log(account.address);
});

Get the account balance

const account = new Account(
  '45e497bd45a9049bcb649016594489ac67b9f052a6cdf5cb74ee2427a60bf25e',
  new Messenger(
    new HttpProvider('https://rpc.s0.t.astranetwork.com'),
    ChainType.Astra,
    ChainID.AstraTestnet,
  ),
);
account.getBalance().then(response => {
    console.log(response);
});

outputs { balance: '9126943763247054940484', nonce: 45, shardID: 0 }

Create a transaction and account, and sign it

const account = new Account(
  '45e497bd45a9049bcb649016594489ac67b9f052a6cdf5cb74ee2427a60bf25e',
  new Messenger(
    new HttpProvider('https://rpc.s0.t.astranetwork.com'),
    ChainType.Astra,
    ChainID.AstraTestnet,
  ),
);
const { TransactionFactory } = require('@astra-js/transaction');
const { Unit } = require('@astra-js/utils');
const factory = new TransactionFactory();

const txn = factory.newTx({
  to: '0xd6ba69DA5b45eC98b53e3258d7DE756a567B6763',
  value: new Unit(1).asOne().toWei(),
  // gas limit, you can use string
  gasLimit: '21000',
  // send token from shardID
  shardID: 0,
  // send token to toShardID
  toShardID: 0,
  // gas Price, you can use Unit class, and use Gwei, then remember to use toWei(), which will be transformed to BN
  gasPrice: new Unit('1').asGwei().toWei(),
});

account.signTransaction(txn).then((signedTxn) => {
  console.log(signedTxn);
});

Similarily staking transactions can be created and signed using account.

A wallet represents user wallet that can hold one or more user accounts.

Creating an empty wallet

const { Wallet } = require('@astra-js/account')
const wallet = new Wallet();

Setting a messenger to be used to send wallet transactions

wallet.setMessenger(
  new Messenger(
    new HttpProvider('https://rpc.s0.t.astranetwork.com'),
    ChainType.Astra,
    ChainID.AstraTestnet,
  ),
);

Create an empty wallet with messenger

const wallet = new Wallet(
  new Messenger(
    new HttpProvider('https://rpc.s0.t.astranetwork.com'),
    ChainType.Astra,
    ChainID.AstraTestnet,
  ),
);

An account could be added to a wallet using different ways. Adding account using mnemonics

const mnemonics = 'horse distance dry brother pretty manual chicken mushroom town swim prize clutch';
const account = wallet.addByMnemonic(mnemonics);

Adding account using private key

const account = wallet.addByPrivateKey('0x676cd9773dd23a4c1d7f22767c61c7b6723cc6be37b078545f6e0e91433a23dd')

Adding account using keystore file

const keystore = '{"version":3,"id":"33363566-3564-4264-a638-363531666335","address":"7c41e0668b551f4f902cfaec05b5bdca68b124ce","crypto":{"ciphertext":"9b09380afb742838b32d9afc0ec1a3df35dbd7a41e3a160d08c07a4d0e79b855","cipherparams":{"iv":"1cd0e0522260eef055b9170f4825f4a0"},"cipher":"aes-128-ctr","kdf":"scrypt","kdfparams":{"salt":"bf35e36c45cccefcef73a4c900f41c682c94c28630d94d2d1f764760d245f30b","n":8192,"r":8,"p":1,"dklen":32},"mac":"25b4442972356bea02af57eba3b87803086d90b5e7657a57b528b89b1aa25f2f"}}';
const passphrase = '';
wallet.addByKeyStore(keystore, passphrase).then(account => {
    console.log(account.address);
});

Creating a new account using passphrase

const passphrase = 'astra-net';
wallet.createAccount(passphrase).then(account => {
    console.log(account.address);
});

Get all accounts in the wallet

wallet.accounts.forEach(addr => {
    const account = wallet.getAccount(addr);
    console.log(account.address);
});

Set wallet signer when multiple accounts exists in the wallet

wallet.setSigner(signerAddr);

Sign transaction using wallet, will sign the transaction using the wallet signer

const txn = factory.newTx({
  to: '0xd6ba69DA5b45eC98b53e3258d7DE756a567B6763',
  value: new Unit(1).asOne().toWei(),
  // gas limit, you can use string
  gasLimit: '21000',
  // send token from shardID
  shardID: 0,
  // send token to toShardID
  toShardID: 0,
  // gas Price, you can use Unit class, and use Gwei, then remember to use toWei(), which will be transformed to BN
  gasPrice: new Unit('1').asGwei().toWei(),
});

wallet.signTransaction(txn).then((signedTxn) => {
  console.log(signedTxn);
});

Similarily staking transactions can be signed using signStaking api.