Creating and opening an account
This guide will help you create a new account and open it.
Prerequisites
- Finish the getting started section.
- Text editor or IDE.
- XPX-Chain-SDK or XPX-Chain-CLI.
Getting into some code
An account is a key pair (private and public key) associated with a mutable state stored in the Sirius Chain.
keyPair, _ := crypto.NewRandomKeyPair()
fmt.Printf("PublicKey:\t%x\n", keyPair.PublicKey.Raw)
fmt.Printf("PrivateKey:\t%x", keyPair.PrivateKey.Raw)
const account = Account.generateNewAccount(NetworkType.TEST_NET);
console.log('Address:', account.address.pretty());
console.log('PrivateKey:', account.privateKey);
console.log('PublicKey:', account.publicKey);
const account = Account.generateNewAccount(NetworkType.TEST_NET);
console.log('Address:', account.address.pretty());
console.log('PrivateKey:', account.privateKey);
console.log('PublicKey:', account.publicKey);
final Account account = Account.generateNewAccount(NetworkType.TEST_NET);
System.out.printf("Address: '%S' %n", account.getAddress());
System.out.printf("PrivateKey: '%S' %n", account.getPrivateKey());
System.out.printf("PublicKey: '%S' %n", account.getPublicKey());
xpx2-cli account generate --network TEST_NET
The private key uniquely identifies a Sirius Chain account and holds all of its power. It is a priority to ensure it is stored safely somewhere offline and not to share it with anyone.
The public key is cryptographically derived from the private key and safe to be shared. Nonetheless, it is preferable to share the address, which contains further information such as network and validity check.
If you already have a private key, it is not necessary to generate a new account:
account, _ := sdk.NewAccountFromPrivateKey("...", sdk.TEST_NET)
fmt.Printf("Address:\t%v\n", account.Address)
fmt.Printf("PrivateKey:\t%x\n", account.KeyPair.PrivateKey.Raw)
fmt.Printf("PublicKey:\t%x", account.KeyPair.PublicKey.Raw)
// Replace with a private key
const privateKey = process.env.PRIVATE_KEY as string;
const account = Account.createFromPrivateKey(privateKey, NetworkType.TEST_NET);
console.log('Address:', account.address.pretty());
console.log('PrivateKey:', account.privateKey);
console.log('PublicKey:', account.publicKey);
// Replace with a private key
const privateKey = process.env.PRIVATE_KEY;
const account = Account.createFromPrivateKey(privateKey, NetworkType.TEST_NET);
console.log('Address:', account.address.pretty());
console.log('PrivateKey:', account.privateKey);
console.log('PublicKey:', account.publicKey);
// Replace with a private key
final String privateKey = "<private_key>";
final Account account = Account.createFromPrivateKey(privateKey, NetworkType.TEST_NET);
System.out.printf("Address: '%S' %n", account.getAddress());
System.out.printf("PrivateKey: '%S' %n", account.getPrivateKey());
System.out.printf("PublicKey: '%S' %n", account.getPublicKey());
Using a Wallet
If the programming language of the SDK you are using allows client-side development, you can create a wallet.
A wallet enables you to store your account to sign transactions, encrypting your private key with a password.
const password = new Password('password');
const wallet = SimpleWallet.create('wallet-name', password, NetworkType.TEST_NET);
const account = wallet.open(password);
console.log('Your new account address is:', account.address.pretty(), 'and its private key', account.privateKey);
const password = new Password('password');
const wallet = SimpleWallet.create('wallet-name', password, NetworkType.TEST_NET);
const account = wallet.open(password);
console.log('Your new account address is:', account.address.pretty(), 'and its private key', account.privateKey);
Do you have a private key? You can create and open a wallet importing your private key.
const password = new Password('password');
// Replace with a private key
const privateKey = process.env.PRIVATE_KEY as string;
const wallet = SimpleWallet.createFromPrivateKey('wallet-name', password, privateKey, NetworkType.TEST_NET);
const account = wallet.open(password);
console.log('Your account address is:', account.address.pretty(), 'and its private key', account.privateKey);
const password = new Password('password');
// Replace with a private key
const privateKey = process.env.PRIVATE_KEY;
const wallet = SimpleWallet.createFromPrivateKey('wallet-name', password, privateKey, NetworkType.TEST_NET);
const account = wallet.open(password);
console.log('Your account address is:', account.address.pretty(), 'and its private key', account.privateKey);