I am interfacing with an HSM which is generating and signing with the ethereum standard (secp256k1). I am interfacing with the HSM using a package called Graphene. I pull out the public key using its "pointEC" attribute:
0xc87c1d67c1909ebf8b54c9ce3d8e0f0cde41561c8115481321e45b364a8f3334b6e826363d8e895110fc9ca2d75e84cc7c56b8e9fbcd70c726cb44f5506848fa
Which I can use to generate the address: 0x21d20b04719f25d2ba0c68e851bb64fa570a9465
But when I try to use the key to sign a personal message from a dApp, the signature always evaluates to a different address. For example, the nonce/message:
wAMqcOCD2KKz2n0Dfbu1nRYbeLw_qbLxrW1gpTBwkq
Has the signature:
0x2413f8d2ab4df2f3d87560493f21f0dfd570dc61136c53c236731bf56a9ce02cb23692e6a5cec96c62359f6eb4080d80328a567d14387f487f3c50d9ce61503b1c
But it recovers a valid address of 0xFC0561D848b0cDE5877068D94a4d803A0a933785
This is all presumably with the same private/public key. Granted, I merely appended the "1c" recovery value, but even when I attempt with other values I have no luck. Here's a couple more examples:
Nonce: WRH_ApTkfN7yFAEpbGwU9BiE2M6eKTZMklPYK50djnx
Sig: 0x70242adabfe27c12e54abced8de87b45f511a194609eb27b215b153594b5697b7fb5e7279285663f80c82c2a2f2920916f76fd845cdecb45ace19f76b0622ac41c
Address: 0x1A086eD40FF90E75764260E2Eb42fab4Db519E53
Nonce: TZV6qhplddJgcKaN7qtpcIhudFhiQ
Sig: 0x3607beb3d58ff35ca1059f3ea44f41e79e76d8ffe35a4f716e78030f0fe2ca1da51f138c31d4ec4b9fc3546c4de1185736a4c4c7030a8b1965e30cb0af6ba2ee1c
Address: 0xa61A518cf73163Fd92461942c26C67c203bda379
My code to sign the message:
let alg: graphene.MechanismType;
alg = graphene.MechanismEnum.ECDSA;
const session = get_session();
let key: graphene.Key | null = null;
//#region Find signing key
const objects = session.find({label: GEN_KEY_LABEL});
for (let i = 0; i < objects.length; i++) {
const obj = objects.items(i);
if ((obj.class === graphene.ObjectClass.PRIVATE_KEY ||
obj.class === graphene.ObjectClass.SECRET_KEY) &&
obj.handle.toString('hex') == params.handle
) {
key = obj.toType<graphene.Key>();
break;
}
}
if (!key) {
throw new Error("Cannot find signing key");
}
var sign = session.createSign(alg,key);
if (!params.data) {
console.log("No data found. Signing 'test' string");
params.data = 'test';
}
sign.update(Buffer.from(params.data.toString().trim()));
var signature = sign.final();
console.log(signature.toString('hex'));
Keep in mind, it fails with even just 1 key present.