AMAXPrivateKey.fromString constructor

AMAXPrivateKey.fromString(
  1. String keyStr
)

Construct the private key from string It can come from WIF format for PVT format

Implementation

AMAXPrivateKey.fromString(String keyStr) {
  RegExp privateRegex = RegExp(r"^PVT_([A-Za-z0-9]+)_([A-Za-z0-9]+)", caseSensitive: true, multiLine: false);
  Iterable<Match> match = privateRegex.allMatches(keyStr);

  if (match.isEmpty) {
    format = 'WIF';
    keyType = 'K1';
    // WIF
    Uint8List keyWLeadingVersion = AMAXKey.decodeKey(keyStr, AMAXKey.SHA256X2);
    int version = keyWLeadingVersion.first;
    if (AMAXKey.VERSION != version) {
      throw InvalidKey("version mismatch");
    }

    d = keyWLeadingVersion.sublist(1, keyWLeadingVersion.length);
    if (d!.lengthInBytes == 33 && d!.elementAt(32) == 1) {
      // remove compression flag
      d = d!.sublist(0, 32);
    }

    if (d!.lengthInBytes != 32) {
      throw InvalidKey('Expecting 32 bytes, got ${d!.length}');
    }
  } else if (match.length == 1) {
    format = 'PVT';
    Match m = match.first;
    keyType = m.group(1);
    d = AMAXKey.decodeKey(m.group(2)!, keyType);
  } else {
    throw InvalidKey('Invalid Private Key format');
  }
}