Skip to content

Commit

Permalink
feat: minor tweaks to starknet signers
Browse files Browse the repository at this point in the history
  • Loading branch information
JesseTheRobot committed Oct 28, 2024
1 parent 458224b commit 4b0dfcb
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 31 deletions.
28 changes: 12 additions & 16 deletions src/signing/chains/StarknetSigner.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,15 +22,11 @@ export default class StarknetSigner implements Signer {
}

public async init(): Promise<void> {
try {
const pubKey = encode.addHexPrefix(encode.buf2hex(ec.starkCurve.getPublicKey(this.privateKey, true)));
const hexKey = pubKey.startsWith("0x") ? pubKey.slice(2) : pubKey;

this.publicKey = Buffer.from(hexKey, "hex");
this.chainId = await this.provider.getChainId();
} catch (error) {
console.error("Error setting public key or chain ID:", error);
}
const pubKey = encode.addHexPrefix(encode.buf2hex(ec.starkCurve.getPublicKey(this.privateKey, true)));
const hexKey = pubKey.startsWith("0x") ? pubKey.slice(2) : pubKey;

this.publicKey = Buffer.from(hexKey, "hex");
this.chainId = await this.provider.getChainId();
}

async sign(message: Uint8Array, _opts?: any): Promise<Uint8Array> {
Expand Down Expand Up @@ -62,36 +58,36 @@ export default class StarknetSigner implements Signer {
result.set(chainIdToArray, rArray.length + sArray.length + addressToArray.length);

// check signature is of required length
if (result.length != 128) throw new Error("Signature length must be 128 bytes!");
if (result.length !== 128) throw new Error("Signature length must be 128 bytes!");

return result;
}

static async verify(_pk: Buffer, message: Uint8Array, _signature: Uint8Array, _opts?: any): Promise<boolean> {
static async verify(pubkey: Buffer, message: Uint8Array, signature: Uint8Array, _opts?: any): Promise<boolean> {
const rLength = 32;
const sLength = 32;
const addressLength = 32;
const chainIdLength = 32;

// retrieve address from signature
const addressArrayRetrieved = _signature.slice(rLength + sLength, rLength + sLength + addressLength);
const addressArrayRetrieved = signature.slice(rLength + sLength, rLength + sLength + addressLength);
const originalAddress = "0x" + Buffer.from(addressArrayRetrieved).toString("hex");

// retrieve chainId from signature
const chainIdArrayRetrieved = _signature.slice(rLength + sLength + addressLength, rLength + sLength + addressLength + chainIdLength);
const chainIdArrayRetrieved = signature.slice(rLength + sLength + addressLength, rLength + sLength + addressLength + chainIdLength);
const originalChainId = "0x" + Buffer.from(chainIdArrayRetrieved).toString("hex");

// calculate full public key
const fullPubKey = encode.addHexPrefix(encode.buf2hex(_pk));
const fullPubKey = encode.addHexPrefix(encode.buf2hex(pubkey));

// generate message hash and signature
const msg = hash.computeHashOnElements(uint8ArrayToBigNumberishArray(message));
const data: TypedData = getTypedData(msg, originalChainId);
const msgHash = typedData.getMessageHash(data, originalAddress);
const signature = _signature.slice(0, -64);
const trimmedSignature = signature.slice(0, -64);

// verify
return ec.starkCurve.verify(signature, msgHash, fullPubKey);
return ec.starkCurve.verify(trimmedSignature, msgHash, fullPubKey);
}
}

Expand Down
26 changes: 11 additions & 15 deletions src/signing/chains/injectedStarknetSigner.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,7 @@ export default class InjectedStarknetSigner implements Signer {
}

public async init(): Promise<void> {
try {
this.chainId = await this.provider.getChainId();
} catch (error) {
console.error("Error setting chain ID:", error);
}
this.chainId = await this.provider.getChainId();
}

async sign(message: Uint8Array, _opts?: any): Promise<Uint8Array> {
Expand All @@ -37,9 +33,9 @@ export default class InjectedStarknetSigner implements Signer {

// due to account abstraction different wallets, return different signature types.
// the last two components in the array are mostly the r and s components
const rs_components = (Array.from(signature)).slice(-2);
const r = BigInt(rs_components[0]).toString(16).padStart(64, "0");
const s = BigInt(rs_components[1]).toString(16).padStart(64, "0");
const rsComponents = Array.from(signature).slice(-2);
const r = BigInt(rsComponents[0]).toString(16).padStart(64, "0");
const s = BigInt(rsComponents[1]).toString(16).padStart(64, "0");
const address = this.walletAccount.address.replace(/^0x0?|^0x/, "").padStart(64, "0");

const rArray = Uint8Array.from(Buffer.from(r, "hex"));
Expand All @@ -55,35 +51,35 @@ export default class InjectedStarknetSigner implements Signer {
result.set(chainIdToArray, rArray.length + sArray.length + addressToArray.length);

// check signature is of required length
if (result.length != 128) throw new Error("Signature length must be 128 bytes!");
if (result.length !== 128) throw new Error("Signature length must be 128 bytes!");

return result;
}

static async verify(_pk: Buffer, message: Uint8Array, _signature: Uint8Array, _opts?: any): Promise<boolean> {
static async verify(pubkey: Buffer, message: Uint8Array, signature: Uint8Array, _opts?: any): Promise<boolean> {
const rLength = 32;
const sLength = 32;
const addressLength = 32;
const chainIdLength = 32;

// retrieve address from signature
const addressArrayRetrieved = _signature.slice(rLength + sLength, rLength + sLength + addressLength);
const addressArrayRetrieved = signature.slice(rLength + sLength, rLength + sLength + addressLength);
const originalAddress = "0x" + Buffer.from(addressArrayRetrieved).toString("hex");

// retrieve chainId from signature
const chainIdArrayRetrieved = _signature.slice(rLength + sLength + addressLength, rLength + sLength + addressLength + chainIdLength);
const chainIdArrayRetrieved = signature.slice(rLength + sLength + addressLength, rLength + sLength + addressLength + chainIdLength);
const originalChainId = "0x" + Buffer.from(chainIdArrayRetrieved).toString("hex");

// calculate full public key
const fullPubKey = encode.addHexPrefix(encode.buf2hex(_pk));
const fullPubKey = encode.addHexPrefix(encode.buf2hex(pubkey));

// generate message hash and signature
const msg = hash.computeHashOnElements(uint8ArrayToBigNumberishArray(message));
const data: TypedData = getTypedData(msg, originalChainId);
const msgHash = typedData.getMessageHash(data, originalAddress);
const signature = _signature.slice(0, -64);
const trimmedSignature = signature.slice(0, -64);

// verify
return ec.starkCurve.verify(signature, msgHash, fullPubKey);
return ec.starkCurve.verify(trimmedSignature, msgHash, fullPubKey);
}
}

0 comments on commit 4b0dfcb

Please sign in to comment.