About This Package
@harmony-js/account
is dealing with account related features.
Developers can use this packages to:
- create
Account
instance - create
Wallet
instance - sign
Transaction
- convert address format
- manage
privateKey
or mnemonic phrases
and do the encrypt
and decrypt
job
There are 2 main classes in this package, Account
and Wallet
.
The Account
class is basic instance that contains most features mentioned above.
The Wallet
class is class that stores all Account
instance, you can do CRUD on it.
Usage of Account
Dependencies
- "@harmony-js/network",
- "@harmony-js/staking",
- "@harmony-js/transaction",
- "@harmony-js/utils"
Examples
Create a random account
import {Account} from '@harmony-js/account'
import { HttpProvider, Messenger } from '@harmony-js/network';
import { ChainType, ChainID } from '@harmony-js/utils';
const customMessenger = new Messenger(
new HttpProvider('http://localhost:9500'),
ChainType.Harmony,
ChainID.HmyLocal,
)
const randomAccount = new Account()
randomAccount.setMessenger(customMessenger)
const randomAccountWithCustomMessenger = new Account(undefined, customMessenger)
console.log({randomAccount,randomAccountWithCustomMessenger})
const staticCreatedAccount = Account.new()
staticCreatedAccount.setMessenger(customMessenger)
console.log({staticCreatedAccount})
Import an existing privateKey to create Account
import {Account} from '@harmony-js/account'
const myPrivateKey = '0xe19d05c5452598e24caad4a0d85a49146f7be089515c905ae6a19e8a578a6930'
const myAccountWithMyPrivateKey = new Account(myPrivateKey)
const myAccountWithMyPrivateKeyUsingStatic = Account.add(myPrivateKey)
console.log({ myAccountWithMyPrivateKey, myAccountWithMyPrivateKeyUsingStatic })
Encrypt/Export keyStore file, Decrypt/Import keyStore file
import {Account} from '@harmony-js/account'
const myPrivateKey = '0xe19d05c5452598e24caad4a0d85a49146f7be089515c905ae6a19e8a578a6930'
const myAccountWithMyPrivateKey = new Account(myPrivateKey)
const myStrongPassword = '123'
async function encryptAndDecrypt(password) {
const unencryptedPrivateKey = myAccountWithMyPrivateKey.privateKey
const keyStoreFile = await myAccountWithMyPrivateKey.toFile(password)
console.log({ keyStoreFile })
console.log(`Is this account encrypted? \n ${myAccountWithMyPrivateKey.encrypted}`)
console.log(
`Is privateKey equal to keyStore string? \n ${keyStoreFile ===
myAccountWithMyPrivateKey.privateKey}`,
)
}
encryptAndDecrypt(myStrongPassword)
const someKeyStoreFile =
'{"version":3,"id":"62326332-3139-4839-b534-656134623066","address":"1fe3da351d9fc0c4f02de5412ad7def8aee956c5","Crypto":{"ciphertext":"b86ab81682c9f5a35738ad9bd38cd9b46c1b852ef33f16dd83068f79e20d5531","cipherparams":{"iv":"44efb5a514f34968e92cafad80566487"},"cipher":"aes-128-ctr","kdf":"scrypt","kdfparams":{"salt":"d70ae1f311601562113f98c8ebe382f52a332dca1588886e5ea91e2f8a647134","n":8192,"r":8,"p":1,"dklen":32},"mac":"7b63e4e31a75a22b7091291bb58302655b738539ef3e30b30a7a7f170f6163ef"}}'
async function importKeyStoreFileAndDecrypt(keyStoreFile, password) {
const importedAccount = await Account.new().fromFile(keyStoreFile, password)
console.log(`Is this account encrypted? \n ${importedAccount.encrypted}`)
console.log(
`Is the account recovered from keystore? \n ${importedAccount.privateKey === myPrivateKey}`,
)
}
importKeyStoreFileAndDecrypt(someKeyStoreFile, myStrongPassword)
Address format getter
import {Account} from '@harmony-js/account'
const myPrivateKey = '0xe19d05c5452598e24caad4a0d85a49146f7be089515c905ae6a19e8a578a6930'
const myAccountWithMyPrivateKey = new Account(myPrivateKey)
console.log(myAccountWithMyPrivateKey.address)
console.log(myAccountWithMyPrivateKey.bech32Address)
console.log(myAccountWithMyPrivateKey.bech32TestNetAddress)
console.log(myAccountWithMyPrivateKey.checksumAddress)
Sign a transaction
import {Account} from '@harmony-js/account'
import {Transaction} from '@harmony-js/transaction'
import { HttpProvider, Messenger } from '@harmony-js/network';
import { ChainType, ChainID, Unit } from '@harmony-js/utils';
const customMessenger = new Messenger(
new HttpProvider('http://localhost:9500'),
ChainType.Harmony,
ChainID.HmyLocal,
)
const myPrivateKey = '0xe19d05c5452598e24caad4a0d85a49146f7be089515c905ae6a19e8a578a6930'
const myAccountWithMyPrivateKey = new Account(myPrivateKey)
const txnObject = {
to: 'one166axnkjmghkf3df7xfvd0hn4dft8kemrza4cd2',
value: '1000000000000000000000',
gasLimit: '210000',
shardID: 0,
toShardID: 0,
gasPrice: new Unit('100').asGwei().toWei(),
nonce: 0,
};
const txn = new Transaction(txnObject, customMessenger);
async function signTheTxn() {
const signedTxn = await myAccountWithMyPrivateKey.signTransaction(txn, false, 'rlp', 'latest');
console.log(`\n see if transaction is signed: \n ${signedTxn.isSigned()} \n`);
console.log(`\n the signed bytes is: \n ${signedTxn.getRawTransaction()} \n`);
return signTheTxn;
}
signTheTxn();
Usage of Wallet
Dependencies
- "@harmony-js/crypto",
- "@harmony-js/network",
- "@harmony-js/staking",
- "@harmony-js/transaction",
- "@harmony-js/utils"
import {Wallet} from '@harmony-js/account'
const wallet=new Wallet()