verifyTonSignature function
Recompute the exact digest the device signs — the BoC ROOT CELL's
representation hash for a transaction, or the TON Connect proof digest
sha256(0xFFFF || "ton-connect" || sha256(payload)) — and verify the
Ed25519 signature against the linked key.
Implementation
VerifyResult verifyTonSignature(VerifyTonSignatureArgs args) {
if (args.signature.length != 64) return failed('signature must be 64 bytes');
if (args.publicKey.length != 32) return failed('public key must be 32 bytes');
Uint8List digest;
if (args.dataType == TonDataType.tonProof) {
digest = sha256(concatBytes([
Uint8List.fromList([0xff, 0xff]),
utf8Encode('ton-connect'),
sha256(args.signData),
]));
} else if (args.dataType == TonDataType.transaction) {
try {
digest = bocRootHash(args.signData);
} on EraSdkError catch (e) {
return failed('signData is not a readable BoC: ${e.message}');
}
} else {
return failed('unknown dataType ${args.dataType}');
}
final ok = ed25519Verify(args.publicKey, digest, args.signature);
return ok
? verified
: failed('the signature does not belong to this account');
}