// import the Account classconst { Account } = require('@harmony-js/account');
// Messenger is optional, by default, we have a defaultMessenger// If you like to change, you will import related package here.const { HttpProvider, Messenger } = require('@harmony-js/network');
const { ChainType, ChainID } = require('@harmony-js/utils');
// create a custom messengerconst customMessenger = new Messenger(
new HttpProvider('http://localhost:9500'),
ChainType.Harmony, // if you are connected to Harmony's blockchain
ChainID.HmyLocal, // check if the chainId is correct
)
// setMessenger laterconst randomAccount = new Account()
randomAccount.setMessenger(customMessenger)
// or you can set messenger on `new`const randomAccountWithCustomMessenger = new Account(undefined, customMessenger)
// NOTED: Key with or without `0x` are accepted, makes no different// NOTED: DO NOT import `mnemonic phrase` using `Account` class, use `Wallet` insteadconst myPrivateKey = '0xe19d05c5452598e24caad4a0d85a49146f7be089515c905ae6a19e8a578a6930'const myAccountWithMyPrivateKey = new Account(myPrivateKey)
Parameters
Optional key: string
import an existing privateKey, or create a random one
Default value messenger: Messenger = defaultMessenger
// create a custom messengerconst customMessenger = new Messenger(
new HttpProvider('http://localhost:9500'),
ChainType.Harmony, // if you are connected to Harmony's blockchain
ChainID.HmyLocal, // check if the chainId is correct
)
// to create an Account with random privateKey// and you can setMessenger laterconst randomAccount = new Account()
randomAccount.setMessenger(customMessenger)
Generate an account object
// import the Account class const { Account } = require('@harmony-js/account'); // Messenger is optional, by default, we have a defaultMessenger // If you like to change, you will import related package here. const { HttpProvider, Messenger } = require('@harmony-js/network'); const { ChainType, ChainID } = require('@harmony-js/utils'); // create a custom messenger const customMessenger = new Messenger( new HttpProvider('http://localhost:9500'), ChainType.Harmony, // if you are connected to Harmony's blockchain ChainID.HmyLocal, // check if the chainId is correct ) // setMessenger later const randomAccount = new Account() randomAccount.setMessenger(customMessenger) // or you can set messenger on `new` const randomAccountWithCustomMessenger = new Account(undefined, customMessenger) // NOTED: Key with or without `0x` are accepted, makes no different // NOTED: DO NOT import `mnemonic phrase` using `Account` class, use `Wallet` instead const myPrivateKey = '0xe19d05c5452598e24caad4a0d85a49146f7be089515c905ae6a19e8a578a6930' const myAccountWithMyPrivateKey = new Account(myPrivateKey)