Skip to content

Basic Functions

Wei Hau Lim edited this page May 12, 2022 · 7 revisions

Generates new Keypair

import 'package:convert/convert.dart';
import 'package:xpx_crypto/xpx_crypto.dart';

void main() async{
  /// New KeyPair from PrivateKey Hex String
  const String skHex = 'BB2B97D428832EFBA9816C62CC4911296EE3EE65DB19316D4AC1191028FE976C';
  final KeyPair kp = await KeyPair.fromPrivateKey(skHex);

  print('publicKey: \"${hex.encode(kp.publicKey)}\"\n');
  print('privateKey: \"${hex.encode(kp.secretKey)}\"\n');

  /// Creates a random key pair
  final KeyPair kp2 = await KeyPair.random();

  print('privateKey2: \"${hex.encode(kp2.secretKey)}\"\n');
  print('publicKey2: \"${hex.encode(kp2.publicKey)}\"\n');
}

Create an Address from a given public key

  • The Address object describes an account's public address and network type.
  • first param - A public key in hex.
  • second param - A NetworkType:
    • NetworkType.PUBLIC = Public net network.
    • NetworkType.PUBLIC_TEST = Public Test net network.
    • NetworkType.PRIVATE = Private network.
    • NetworkType.PRIVATE_TEST = Private test network.
  • Return - An Address Object
import 'package:xpx_chain_sdk/xpx_sdk.dart';

final networkType = NetworkType.PUBLIC_TEST;

const publicKey = '37E95F604301545103E8AC3EFA5A40319DC05587B6192E9658052753356CDBB9';

void main() async {
  final address = await Address.fromPublicKey(publicKey, networkType);
  print('Address: $address\n');
}

Create an Account from a given private key

  • The Account object describes an account's private key, public key and address.
  • first param - A private key in hex.
  • second param - A NetworkType:
    • NetworkType.PUBLIC = Public net network.
    • NetworkType.PUBLIC_TEST = Public Test net network.
    • NetworkType.PRIVATE = Private network.
    • NetworkType.PRIVATE_TEST = Private test network.
  • Return - An Account Object
import 'package:xpx_chain_sdk/xpx_sdk.dart';

final networkType = NetworkType.PUBLIC_TEST;

const privateKey = '1ACE45EAD3C2F0811D9F4355F35BF78483324975083BE4E503EA49DFFEA691A0';

void main() async {
  final account = await Account.fromPrivateKey(privateKey, networkType);

  print('Address: ${account.publicAccount.address}\n');
  print('PublicKey: ${account.publicKey}\n');
  print('PrivateKey: ${account.secretKey}\n');
}