Skip to content

Working with ADM key pairs

martiliones edited this page Dec 16, 2023 · 7 revisions

The library offers to create new ADAMANT account — a passphrase, key pair (public key & private key), and ADAMANT address. Read more about ADAMANT account and key pair.

createNewPassphrase()

Generates a new mnemonic passphrase consisting of English words.

  • Typing

    function createNewPassphrase(): string;
  • Parameters

    None.

  • Returns

    A new, randomly generated passphrase, string.

  • Example

    import { createNewPassphrase } from 'adamant-api'
    
    const passphrase = createNewPassphrase()
    console.log(passphrase) // 'apple banana...'

makeKeypairFromHash()

Creates a pair of public and private keys from the provided hash, using Sodium (NaCl).

  • Typing

    function makeKeypairFromHash(hash: Buffer): {
      publicKey: Buffer,
      privateKey: Buffer
    };
  • Parameters

  • Returns

    An object containing publicKey and privateKey of the key pair.

  • Example

    import { createHashFromPassphrase, makeKeypairFromHash } from 'adamant-api'
    
    const hash = createHashFromPassphrase('apple banana...')
    const keypair = makeKeypairFromHash(hash)
    
    console.log('The key pair:')
    console.log(keypair)

createHashFromPassphrase()

Generates an SHA-256 hash from a given passphrase.

  • Typing

    function createHashFromPassphrase(passphrase: string): Buffer;
  • Parameters

    • passphrase — A 12-word mnemonic passphrase, string.
  • Returns

    The generated hash using the crypto library, Buffer.

  • Example

    import { createNewPassphrase, createHashFromPassphrase } from 'adamant-api'
    
    const passphrase = createNewPassphrase()
    const hash = createHashFromPassphrase(passphrase)
    
    console.log(`hash: ${hash.toString('hex')}`)

createKeypairFromPassphrase()

Creates public and private keys based on a provided passphrase using Sodium (NaCl).

  • Typing

    function createKeypairFromPassphrase(passphrase: string): {
      publicKey: string,
      privateKey: string
    };
  • Parameters

    • passphrase — A 12-word mnemonic passphrase, string.
  • Returns

    An object containing the publicKey and privateKey of the key pair.

  • Example

    import { createKeypairFromPassphrase } from 'adamant-api'
    
    let keyPair
    
    try {
      keyPair = createKeypairFromPassphrase(process.env.PASSPHRASE)
    } catch (error) {
      console.log(`❌ Invalid passphrase. Error: ${error}`)
      process.exit(-1)
    }
    
    console.log('✅ The given passphrase is valid')

createAddressFromPublicKey()

Derives an ADAMANT address from the given public key.

  • Typing

    function createAddressFromPublicKey(publicKey: Buffer | string): `U${string}`;
  • Parameters

    • publicKey — The public key of an account.
  • Returns

    The derived ADAMANT address, string in format U123....

  • Example

    import { createKeypairFromPassphrase, createAddressFromPublicKey } from 'adamant-api'
    
    const { publicKey } = createKeypairFromPassphrase(process.env.PASSPHRASE)
    const address = createAddressFromPublicKey(publicKey)
    
    console.log(`Your ADAMANT address: ${address}`)
Clone this wiki locally