signCreateAccountAuthorization method

Future<String> signCreateAccountAuthorization(
  1. String privateKey
)

Generates the signature necessary for /create-account-authorization endpoint @param {String} privateKey - private key used to create the wallet @returns {String} The generated signature

Implementation

Future<String> signCreateAccountAuthorization(String privateKey) async {
  final signer = EthPrivateKey.fromHex(privateKey);

  final bJJ = this.publicKeyCompressedHex!.startsWith('0x')
      ? this.publicKeyCompressedHex!
      : '0x${this.publicKeyCompressedHex}';

  final Map<String, dynamic> domain = {
    'name': EIP_712_PROVIDER,
    'version': EIP_712_VERSION,
    'chainId':
        BigInt.from(getCurrentEnvironment()!.chainId).toRadixString(16),
    'verifyingContract':
        EthereumAddress.fromHex(getCurrentEnvironment()!.contracts['Hermez']!)
  };

  final Map<String, dynamic> message = {
    'Provider': EIP_712_PROVIDER,
    'Authorisation': CREATE_ACCOUNT_AUTH_MESSAGE,
    'BJJKey': hexToBytes(bJJ)
  };

  final String primaryType = 'Authorise';

  final Map<String, List<TypedDataArgument>> types = {
    'Authorise': [
      TypedDataArgument('Provider', 'string'),
      TypedDataArgument('Authorisation', 'string'),
      TypedDataArgument('BJJKey', 'bytes32')
    ],
    'EIP712Domain': [
      TypedDataArgument('name', 'string'),
      TypedDataArgument('version', 'string'),
      TypedDataArgument('chainId', 'uint256'),
      TypedDataArgument('verifyingContract', 'address')
    ]
  };

  final typedData = TypedData(types, primaryType, domain, message);

  final messageHash = eip712.encodeDigest(typedData);
  final signature = await signer.sign(messageHash);
  final signatureHex = bytesToHex(signature, include0x: true);
  return signatureHex;
}