XchainJS
  • Overview
  • Installation
  • Examples instructions
  • Clients
    • xchain-evm
      • xchain-avax
        • How to Use
      • xchain-arbitrum
        • How to Use
      • xchain-bsc
        • How to Use
      • xchain-ethereum
        • How to Use
    • xchain-utxo
      • xchain-bitcoin
        • How to use
      • xchain-bitcoincash
        • How to Use
      • xchain-dash
      • xchain-doge
        • How to Use
      • xchain-litecoin
        • How to Use
    • xchain-cosmos-sdk
      • xchain-cosmos
        • How to Use
      • xchain-kujira
        • How to Use
      • xchain-mayachain
        • How to Use
      • xchain-thorchain
        • How to Use
    • xchain-binance
      • How to Use
    • xchain-solana
      • How to use
      • Examples
        • Generate address
        • Get balances
        • Get token balance
        • Prepare transaction
        • Make transaction
        • Make token transaction
  • Wallet
  • Protocols
    • THORChain
      • xchain-thorchain-amm
        • How to Use
        • Make swap using THORChain
        • Handle liquidity and savers
        • Open and close loans
      • xchain-thorchain-query
        • How to Use
        • Check transaction status
        • Estimate a swap
      • xchain-midgard
        • How to Use
      • xchain-thornode
        • How to Use
      • xchain-midgard-query
        • How to Use
    • MAYAProtocol
      • xchain-mayachain-amm
        • How to Use
        • Make swap using MAYAChain
      • xchain-mayachain-query
        • How to Use
      • xchain-mayamidgard
        • How to Use
      • xchain-mayanode
        • How to Use
      • xchain-mayamidgard-query
        • How to Use
  • Aggregator
  • Providers
    • xchain-utxo-providers
      • How it Works
    • xchain-evm-providers
      • How it Works
  • Others
    • xchain-crypto
      • How it Works
      • How to Use
    • xchain-util
      • How it Works
      • How to Use
  • Contributors
  • Documentation maintenance
Powered by GitBook
On this page
  • Installation
  • Development
  • Example
  1. Others
  2. xchain-crypto

How to Use

Installation

Install @xchainjs/xchain-crypto from npm

yarn add @xchainjs/xchain-crypto

Development

Build

yarn build

Tests

yarn test

Example

Generate New Phrases and Encrypt

By default, it will generate a 12-word phrase. Create a new phrase using generatePhrase() Check phrase validity using validatePhrase() Encrypt to keystore using encryptToKeyStore() > takes two arguements (phrase, password)\

// Imports
import { generatePhrase, validatePhrase, encryptToKeyStore, decryptFromKeystore } from "@xchainjs/xchain-crypto"

require('dotenv').config();

const keystore1 = JSON.parse(fs.readFileSync('keystore.json', 'utf8'))
const password = process.env.PASSWORD

// Generate Keystore and save it to a keystore file
const GenerateKeystore = async () => {
    const phrase = generatePhrase()
    console.log(`phrase ${phrase}`)
    const isCorrect = validatePhrase(phrase) //validate phrase if needed returns Boolean
    console.log(`Phrase valid?: ${isCorrect}`)
    const keystore = await encryptToKeyStore(phrase, password)
    fs.writeFileSync(`./keystore.json`, JSON.stringify(keystore, null, 4), 'utf8')
}

Decrypt Keystore to Retrieve a Phrase

Retrieve the phrase by calling decryptFromKeystore()\

const connectWallet = async () => {
    let phrase = await decryptFromKeystore(keystore1, password)
    console.log(`Phrase: ${phrase}`)
}

Retrieve Seed

Retrieve phrase by calling decryptFromKeystore()

import { decryptFromKeystore, getSeed } from '@xchainjs/xchain-crypto'

const connectWallet = async () => {
    let phrase = await decryptFromKeystore(keystore1, password)
    console.log(`Phrase: ${phrase}`)
}

Keystore Type

type Keystore = {
  crypto: {
    cipher: string
    ciphertext: string
    cipherparams: {
      iv: string
    }
    kdf: string
    kdfparams: {
      prf: string
      dklen: number
      salt: string
      c: number
    }
    mac: string
  }
  id: string
  version: number
  meta: string
}
encryptToKeyStore(phrase: string, password: string): Promise<Keystore>

Constants

// Crypto Constants for xchain
const cipher = 'aes-128-ctr'
const kdf = 'pbkdf2'
const prf = 'hmac-sha256'
const dklen = 32
const c = 262144
const hashFunction = 'sha256'
const meta = 'xchain-keystore'
PreviousHow it WorksNextxchain-util

Last updated 1 year ago