Connect Existing Chain Account

Existing blockchain accounts can be imported through the library in an Account object to allow for the use of the private key to sign messages.

Different options are available to make the connection:

  • the chain account private key

  • the chain account secret phrase

  • the use of a third-party browser web3 provider like metamask

  • the chain account details to stub an account object

Not all options are available on each chain module. Please refer below to see the connecting methods available for the needed chain.

1. From Private key or Secret Phrase

You can connect with any existing blockchain account by using the import_account function and passing in either the private key or the mnemonics of the account depending on the chain used.

Besides the private_key or mnemonics, other parameters like name, path, prefix or format can be passed in depending on the chain but are optional.

In the eventuality that both mnemonics and private_key are passed in, private_key will be ignored and mnemonics will be used to import the account.

// FUNCTION SIGNATURE
await avalanche.import_account({
  private_key: null,
  name: null
})

------------------------------------------------------------------------

// EXAMPLE
import { avalanche } from 'aleph-js'

// Import account from private key
account = await avalanche.import_account({
  private_key: '<YOUR PRIVATE KEY>'
})

// RETURNS THE ACCOUNT OBJECT

{
  'private_key': '<PRIVATE KEY>',
  'public_key': '<PUBLIC KEY>',
  'address': '<ADDRESS>',
  'type': 'AVAX',
  'source': 'integrated',
  'signer': '<KEYPAIR>',
  'name': '<PASSED IN NAME OR ACCOUNT ADDRESS>'
}

2. From Wallet Provider

Gets account from wallet provider and returns an account object containing the details of the account that was found in the wallet.

Note how private_keys are not returned with this connection type. Messages will be signed with the provider instead.

// FUNCTION SIGNATURE
// Most likely provider is web3.currentProvider
// provider = web3.currentProvider

ethereum.from_provider(provider)

----------------------------------------------------------------------

// EXAMPLE
import { ethereum } from 'aleph-js'

let account = null

if (window.ethereum) {
  try {
    // Request account access if needed
    await window.ethereum.enable()
    account = await ethereum.from_provider(
      window['ethereum'] || window.web3.currentProvider
    )
  } catch (error) {
      // User denied account access...
  }
}

// RETURNS ACCOUNT OBJECT
{
  'private_key': null,
  'mnemonics': null,
  'address': '<ACCOUNT ADDRESS>',
  'name': '<ACCOUNT ADDRESS>',
  'type': 'ETH',
  'source': 'provider',
  'provider': <WEB3 PROVIDER>,
  'signer': signer
}

3. From External Signer

Will return a stub account object with the passed in attributes. In this case, a message signature will be done by the signer.

await cosmos.from_external_signer({
    address: null, 
    name: null, 
    signe: null, 
    public_key: null
})

---------------------------------------------------------------------
// EXAMPLE

import { cosmos } from 'aleph-js'
await cosmos.from_external_signer({public_key: '<PUBLIC KEY HERE>'})

// RETURNS AN ACCOUNT OBJECT
{
    'public_key': '<PUBLIC KEY PASSED IN>',
    'address': '<ADDRESS PASSED IN>',
    'type': 'CSDK',
    'source': 'function',
    'signer': '<SIGNER PASSED IN>',
    'name': '<NAME PASSED IN>'
  }

Last updated