Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add support for more starknet signature format #1040

Merged
merged 13 commits into from
Jul 25, 2024
71 changes: 39 additions & 32 deletions src/verify/starknet.spec.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { test, expect, describe } from 'vitest';
import starknetMessage from '../../test/fixtures/starknet/message-alias.json';
import starknetMessageRsv from '../../test/fixtures/starknet/message-alias-rsv.json';
import verify, { getHash } from './starknet';
import { validateAndParseAddress } from 'starknet';

Expand All @@ -21,47 +22,53 @@ describe('verify/starknet', () => {
});

describe('verify()', () => {
test('should return true if the signature is valid', () => {
expect(
verify(
starknetMessage.address,
starknetMessage.sig,
starknetMessage.data,
'SN_SEPOLIA'
)
).resolves.toBe(true);
});
describe.each([
['2', starknetMessage],
['3', starknetMessageRsv]
])('with a %s items signature', (title, message) => {
test('should return true if the signature is valid', () => {
expect(
verify(message.address, message.sig, message.data, 'SN_MAIN')
).resolves.toBe(true);
});

test('should return true if the signature is valid with a padded address', () => {
expect(
verify(
validateAndParseAddress(starknetMessage.address),
starknetMessage.sig,
starknetMessage.data,
'SN_SEPOLIA'
)
).resolves.toBe(true);
test('should return true if the signature is valid with a padded address', () => {
expect(
verify(
validateAndParseAddress(message.address),
message.sig,
message.data,
'SN_MAIN'
)
).resolves.toBe(true);
});

test('should return true when verifying on a different network', () => {
expect(
verify(message.address, message.sig, message.data, 'SN_SEPOLIA')
).resolves.toBe(true);
});

test('should throw an error if the signature is invalid', () => {
expect(
verify(
'0x7667469b8e93faa642573078b6bf8c790d3a6184b2a1bb39c5c923a732862e1',
message.sig,
message.data
)
).rejects.toThrow();
});
});

test('should return true when verifying on a different network', () => {
test('should throw an error when the contract is not deployed', () => {
expect(
verify(
starknetMessage.address,
'0x07f71118e351c02f6EC7099C8CDf93AED66CEd8406E94631cC91637f7D7F203A',
starknetMessage.sig,
starknetMessage.data,
'SN_MAIN'
)
).resolves.toBe(true);
});

test('should throw an error if the signature is invalid', () => {
expect(
verify(
'0x7667469b8e93faa642573078b6bf8c790d3a6184b2a1bb39c5c923a732862e1',
starknetMessage.sig,
starknetMessage.data
)
).rejects.toThrow();
).rejects.toThrowError('Contract not deployed');
});
});
});
34 changes: 21 additions & 13 deletions src/verify/starknet.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,19 +11,19 @@ const RPC_URLS: Record<NetworkType, string> = {

const ABI = [
{
name: 'argent::account::interface::IDeprecatedArgentAccount',
name: 'argent::common::account::IAccount',
type: 'interface',
items: [
{
name: 'isValidSignature',
name: 'is_valid_signature',
type: 'function',
inputs: [
{
name: 'hash',
type: 'core::felt252'
},
{
name: 'signatures',
name: 'signature',
type: 'core::array::Array::<core::felt252>'
}
],
Expand Down Expand Up @@ -67,16 +67,24 @@ export default async function verify(
network: NetworkType = 'SN_MAIN',
options: ProviderOptions = {}
): Promise<boolean> {
const contractAccount = new Contract(
ABI,
address,
getProvider(network, options)
);
try {
const contractAccount = new Contract(
ABI,
address,
getProvider(network, options)
);

await contractAccount.is_valid_signature(
getHash(data, address),
sig.slice(-2)
);

await contractAccount.isValidSignature(getHash(data, address), [
sig[0],
sig[1]
]);
return true;
wa0x6e marked this conversation as resolved.
Show resolved Hide resolved
} catch (e: any) {
if (e.message.includes('Contract not found')) {
throw new Error('Contract not deployed');
}

return true;
throw e;
ChaituVR marked this conversation as resolved.
Show resolved Hide resolved
ChaituVR marked this conversation as resolved.
Show resolved Hide resolved
}
}
56 changes: 56 additions & 0 deletions test/fixtures/starknet/message-alias-rsv.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
{
"address": "0x008cf64fc19a22c94c4d751ebd39ff897dbe2c8646b2900d646558e293b0a2e5",
"sig": [
"0x1",
"0xa0f72077d0b928be15ddfc21c481af27a9c79d7b67e049bb97a9919cb9218e",
"0x895bcbd404d7374850219753d98a397c962985ffa3f36d5bd06aab41a01709"
],
"data": {
"domain": {
"name": "sx-starknet",
"version": "0.1.0",
"chainId": "0x534e5f4d41494e",
"verifyingContract": ""
},
"types": {
"StarkNetDomain": [
{
"name": "name",
"type": "felt252"
},
{
"name": "version",
"type": "felt252"
},
{
"name": "chainId",
"type": "felt252"
},
{
"name": "verifyingContract",
"type": "ContractAddress"
}
],
"SetAlias": [
{
"name": "from",
"type": "ContractAddress"
},
{
"name": "alias",
"type": "string"
},
{
"name": "timestamp",
"type": "felt"
}
]
},
"message": {
"from": "0x008cf64fc19a22c94c4d751ebd39ff897dbe2c8646b2900d646558e293b0a2e5",
"timestamp": 1721836843,
"alias": "0x6ceddb030f3ef6dBD04B8b3691CaB101ECe226f6"
},
"primaryType": "SetAlias"
}
}
Loading