How to use @zilliqa-js/crypto - 10 common examples

To help you get started, we’ve selected a few @zilliqa-js/crypto examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github Zilliqa / nucleus-wallet / src / redux / zil / sagas.ts View on Github external
export function* accessWalletSaga(action) {
  // debounce by 500ms
  yield delay(500);
  try {
    const { payload } = action;
    const privateKey: string = payload.privateKey;
    const address = getAddressFromPrivateKey(privateKey);
    const publicKey = getPubKeyFromPrivateKey(privateKey);

    const { zilliqa } = yield select(getZilState);
    zilliqa.wallet.addByPrivateKey(privateKey);

    yield put({
      type: consts.ACCESS_WALLET_SUCCEEDED,
      payload: {
        address,
        publicKey,
        privateKey
      }
    });
  } catch (error) {
    console.log(error);
    yield put({ type: consts.ACCESS_WALLET_FAILED });
github Zilliqa / Zilliqa-JavaScript-Library / examples / helloWord.js View on Github external
// These are set by the core protocol, and may vary per-chain.
// You can manually pack the bytes according to chain id and msg version.
// For more information: https://apidocs.zilliqa.com/?shell#getnetworkid

const chainId = 333; // chainId of the developer testnet
const msgVersion = 1; // current msgVersion
const VERSION = bytes.pack(chainId, msgVersion);

// Populate the wallet with an account
const privateKey =
  '3375F915F3F9AE35E6B301B7670F53AD1A5BE15D8221EC7FD5E503F21D3450C8';

zilliqa.wallet.addByPrivateKey(privateKey);

const address = getAddressFromPrivateKey(privateKey);
console.log(`My account address is: ${address}`);
console.log(`My account bech32 address is: ${toBech32Address(address)}`);

async function testBlockchain() {
  try {
    // Get Balance
    const balance = await zilliqa.blockchain.getBalance(address);
    // Get Minimum Gas Price from blockchain
    const minGasPrice = await zilliqa.blockchain.getMinimumGasPrice();

    // Account balance (See note 1)
    console.log(`Your account balance is:`);
    console.log(balance.result);
    console.log(`Current Minimum Gas Price: ${minGasPrice.result}`);
    const myGasPrice = units.toQa('1000', units.Units.Li); // Gas Price that will be used by all transactions
    console.log(`My Gas Price ${myGasPrice.toString()}`);
github Zilliqa / nucleus-wallet / src / redux / zil / sagas.ts View on Github external
export function* accessWalletSaga(action) {
  // debounce by 500ms
  yield delay(500);
  try {
    const { payload } = action;
    const privateKey: string = payload.privateKey;
    const address = getAddressFromPrivateKey(privateKey);
    const publicKey = getPubKeyFromPrivateKey(privateKey);

    const { zilliqa } = yield select(getZilState);
    zilliqa.wallet.addByPrivateKey(privateKey);

    yield put({
      type: consts.ACCESS_WALLET_SUCCEEDED,
      payload: {
        address,
        publicKey,
        privateKey
      }
    });
  } catch (error) {
    console.log(error);
    yield put({ type: consts.ACCESS_WALLET_FAILED });
  }
github Zilliqa / nucleus-wallet / src / decrypt.worker.ts View on Github external
const decrypt = async (event) => {
  try {
    const { passphrase, keystoreV3 } = event.data;
    const privateKey = await decryptPrivateKey(passphrase, keystoreV3);
    // @ts-ignore
    self.postMessage({ privateKey });
  } catch (error) {
    console.log(error);
    // @ts-ignore
    self.postMessage({ privateKey: undefined });
  }
};
github Zilliqa / nucleus-wallet / src / encrypt.worker.ts View on Github external
const encrypt = async (event) => {
  try {
    const { passphrase } = event.data;
    const privateKey = schnorr.generatePrivateKey();
    const keystoreJSON = await encryptPrivateKey('pbkdf2', privateKey, passphrase);
    // @ts-ignore
    self.postMessage({ keystoreJSON, privateKey });
  } catch (error) {
    console.log(error);
    // @ts-ignore
    self.postMessage({ keystoreJSON: undefined, privateKey: undefined });
  }
};
github Zilliqa / nucleus-wallet / src / encrypt.worker.ts View on Github external
const encrypt = async (event) => {
  try {
    const { passphrase } = event.data;
    const privateKey = schnorr.generatePrivateKey();
    const keystoreJSON = await encryptPrivateKey('pbkdf2', privateKey, passphrase);
    // @ts-ignore
    self.postMessage({ keystoreJSON, privateKey });
  } catch (error) {
    console.log(error);
    // @ts-ignore
    self.postMessage({ keystoreJSON: undefined, privateKey: undefined });
  }
};
github Zilliqa / Zilliqa-JavaScript-Library / packages / zilliqa-js-contract / src / contract.ts View on Github external
constructor(
    factory: Contracts,
    code?: string,
    abi?: ABI,
    address?: string,
    init?: any,
    state?: any,
  ) {
    this.factory = factory;
    this.provider = factory.provider;
    this.signer = factory.signer;
    // assume that we are accessing an existing contract
    if (address) {
      this.abi = abi;
      this.address = normaliseAddress(address);
      this.init = init;
      this.state = state;
      this.status = ContractStatus.Deployed;
    } else {
      // assume we're deploying
      this.abi = abi;
      this.code = code;
      this.init = init;
      this.status = ContractStatus.Initialised;
    }
  }
github Zilliqa / Zilliqa-JavaScript-Library / packages / zilliqa-js-account / src / transaction.ts View on Github external
constructor(
    params: TxParams,
    provider: Provider,
    status: TxStatus = TxStatus.Initialised,
    toDS: boolean = false,
  ) {
    // private members
    this.version = params.version;
    this.toAddr = normaliseAddress(params.toAddr);
    this.nonce = params.nonce;
    this.pubKey = params.pubKey;
    this.amount = params.amount;
    this.code = params.code || '';
    this.data = params.data || '';
    this.signature = params.signature;
    this.gasPrice = params.gasPrice;
    this.gasLimit = params.gasLimit;
    this.receipt = params.receipt;
    // public members
    this.provider = provider;
    this.status = status;
    this.toDS = toDS;
    this.blockConfirmation = 0;
    this.eventEmitter = new EventEmitter();
  }
github Zilliqa / Zilliqa-JavaScript-Library / packages / zilliqa-js-account / src / wallet.ts View on Github external
sign(tx: Transaction): Promise {
    if (tx.txParams && tx.txParams.pubKey) {
      // attempt to find the address
      const senderAddress = zcrypto.getAddressFromPublicKey(tx.txParams.pubKey);

      if (!this.accounts[senderAddress]) {
        throw new Error(
          `Could not sign the transaction with ${senderAddress} as it does not exist`,
        );
      }

      return this.signWith(tx, senderAddress);
    }

    if (!this.defaultAccount) {
      throw new Error('This wallet has no default account.');
    }

    return this.signWith(tx, this.defaultAccount.address);
  }
github zilpay / zil-pay / tests / unit / packages / background / services / account.spec.js View on Github external
it('Should be able init wallet', async() => {
    await accountControl.auth.setPassword(password)

    currentAccount = await accountControl.initWallet(SEED)

    expect(currentAccount.index).toBe(0)
    expect(currentAccount.publicKey).toBeTruthy()
    expect(verifyPrivateKey(currentAccount.privateKey)).toBeTruthy()
    expect(currentAccount.balance).not.toBeNull()
    expect(toChecksumAddress(currentAccount.address)).toBeTruthy()
  })