Create Supra Accounts
How to create new and import existing accounts.
The SupraAccount class is re-exported by supra-ts-sdk↗, so you import it from that single entry point.
SupraAccount class constructor
new SupraAccount()
new SupraAccount(new HexString('YOUR_PRIVATE_KEY').toUint8Array())Create a new supra account or create one from the provided private key.
If no privateKeyBytes parameter is provided, a new account will be generated.
Parameters
| Name | Type | Optional |
|---|---|---|
| privateKeyBytes | Uint8Array | ☑ |
| address | MaybeHexString | ☑ |
Returns SupraAccount
fromDerivePath function
Create a new SupraAccount from mnemonic.
SupraAccount.fromDerivePath(path, mnemonic)Parameters
| Name | Type | Optional |
|---|---|---|
| path | string | ☐ |
| mnemonics | string | ☐ |
Returns SupraAccount
Example
The SupraAccount class allows you to generate new accounts, import private key, or derive from mnemonics.
Initialize a new project
mkdir supra-accounts && cd supra-accounts
npm init -y
npm install supra-ts-sdk@devnet
npm install --save-dev tsxtsx runs TypeScript files directly. The TypeScript SDK page lists
the npm dist-tags.
Create supra_account_example.ts and import dependencies
supra_account_example.ts
typescript import { HexString, SupraAccount } from 'supra-ts-sdk';
Generate a new account
const newAccount = new SupraAccount()
console.log('newAccount: ', newAccount.address().toString())address() returns a HexString; toString() gives the 0x-prefixed address.
Export the private key
const privateKey = newAccount.toPrivateKeyObject().privateKeyHexprivateKeyHex is a 0x-prefixed hex string of the 32-byte private key. Store it where only you can read it. Anyone
with the key controls the account.
Generate an account from a private key
const accountFromPk = new SupraAccount(new HexString(privateKey).toUint8Array())
console.log('accountFromPk Address: ', accountFromPk.address().toString())new HexString(...).toUint8Array() accepts the key with or without the 0x prefix. The constructor throws
bad seed size for a value that is not a 32-byte key.
Generate an account from mnemonics
const path = "m/44'/637'/0'/0'/0'"
const mnemonic = 'test test test test test test test test test test test junk'
const accountFromMnemonics = SupraAccount.fromDerivePath(path, mnemonic)
console.log('accountFromMnemonics: ', accountFromMnemonics.address().toString())fromDerivePath throws Invalid mnemonic when the phrase is not a valid BIP-39 phrase in the English word list: a word
outside the list, a number of words other than 12, 15, 18, 21 or 24, or a failed checksum. Surrounding and repeated
whitespace and upper-case letters are normalised before the check. A phrase from another BIP-39 word list is rejected.
supra-l1-sdk-core 2.0.2 and later, the package that provides SupraAccount, validates the phrase; earlier versions
derive an account from a mistyped phrase. The phrase above is a public test phrase. Use your own for an account that
holds funds.
Complete code
supra_account_example.ts
import { HexString, SupraAccount } from 'supra-ts-sdk'
async function main() {
const newAccount = new SupraAccount()
console.log('newAccount: ', newAccount.address().toString())
const privateKey = newAccount.toPrivateKeyObject().privateKeyHex
const accountFromPk = new SupraAccount(new HexString(privateKey).toUint8Array())
console.log('accountFromPk Address: ', accountFromPk.address().toString())
const path = "m/44'/637'/0'/0'/0'"
const mnemonic = 'test test test test test test test test test test test junk'
const accountFromMnemonics = SupraAccount.fromDerivePath(path, mnemonic)
console.log('accountFromMnemonics: ', accountFromMnemonics.address().toString())
}
main()