Skip to content

Commit

Permalink
Merge pull request #2 from argentlabs/update-example
Browse files Browse the repository at this point in the history
Update to SNIP12 version 1
  • Loading branch information
gaetbout authored Dec 4, 2024
2 parents 19ad573 + de15dd1 commit a53f444
Show file tree
Hide file tree
Showing 29 changed files with 1,242 additions and 442 deletions.
2 changes: 1 addition & 1 deletion .tool-versions
Original file line number Diff line number Diff line change
@@ -1 +1 @@
scarb 0.7.0
scarb 2.8.3
6 changes: 6 additions & 0 deletions Scarb.lock
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
# Code generated by scarb DO NOT EDIT.
version = 1

[[package]]
name = "off_chain_signature"
version = "0.3.0"
20 changes: 14 additions & 6 deletions Scarb.toml
Original file line number Diff line number Diff line change
@@ -1,16 +1,24 @@
[package]
name = "off_chain_signature"
version = "0.2.0"
version = "0.3.0"

[[target.starknet-contract]]
sierra = true

[dependencies]
starknet = "=2.2.0"
starknet = "2.8.2"

[dev-dependencies]
cairo_test = "2.8.2"

[scripts]
SimpleStruct= "npx ts-node scripts/SimpleStruct.ts"
StructWithArray= "npx ts-node scripts/StructWithArray.ts"
StructWithMerkletree= "npx ts-node scripts/StructWithMerkletree.ts"
StructWithU256= "npx ts-node scripts/StructWithU256.ts"
SimpleStruct = "npx ts-node scripts/v0/SimpleStruct.ts"
SimpleStructV1 = "npx ts-node scripts/v1/SimpleStruct.ts"
StructWithArray = "npx ts-node scripts/v0/StructWithArray.ts"
StructWithArrayV1 = "npx ts-node scripts/v1/StructWithArray.ts"
StructWithMerkletree = "npx ts-node scripts/v0/StructWithMerkletree.ts"
StructWithMerkletreeV1 = "npx ts-node scripts/v1/StructWithMerkletree.ts"
StructWithU256 = "npx ts-node scripts/v0/StructWithU256.ts"
StructWithU256V1 = "npx ts-node scripts/v1/StructWithU256.ts"
StructWithString = "npx ts-node scripts/v1/StructWithString.ts"
format = "scarb fmt && npx prettier --write ."
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
"author": "",
"license": "ISC",
"dependencies": {
"starknet": "5.17.0"
"starknet": "6.12.0"
},
"devDependencies": {
"dotenv": "^16.0.3",
Expand Down
16 changes: 8 additions & 8 deletions scripts/SimpleStruct.ts → scripts/v0/SimpleStruct.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { typedData } from "starknet";
import { StarknetDomain, TypedData, typedData } from "starknet";

const types = {
StarkNetDomain: [
Expand All @@ -13,11 +13,11 @@ const types = {
};

interface SimpleStruct {
some_felt252: string;
some_u128: string;
someFelt252: string;
someU128: string;
}

function getDomain(chainId: string): typedData.StarkNetDomain {
function getDomain(chainId: string): StarknetDomain {
return {
name: "dappName",
version: "1",
Expand All @@ -31,18 +31,18 @@ function getTypedDataHash(myStruct: SimpleStruct, chainId: string, owner: bigint

// Needed to reproduce the same structure as:
// https://github.com/0xs34n/starknet.js/blob/1a63522ef71eed2ff70f82a886e503adc32d4df9/__mocks__/typedDataStructArrayExample.json
function getTypedData(myStruct: SimpleStruct, chainId: string): typedData.TypedData {
function getTypedData(myStruct: SimpleStruct, chainId: string): TypedData {
return {
types,
primaryType: "SimpleStruct",
domain: getDomain(chainId),
message: { ...myStruct },
message: { som_felt252: myStruct.someFelt252, some_u128: myStruct.someU128 },
};
}

const simpleStruct: SimpleStruct = {
some_felt252: "712",
some_u128: "42",
someFelt252: "712",
someU128: "42",
};

console.log(`test test_valid_hash ${getTypedDataHash(simpleStruct, "0", 420n)};`);
16 changes: 8 additions & 8 deletions scripts/StructWithArray.ts → scripts/v0/StructWithArray.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { typedData } from "starknet";
import { StarknetDomain, TypedData, typedData } from "starknet";

const types = {
StarkNetDomain: [
Expand All @@ -13,11 +13,11 @@ const types = {
};

interface StructWithArray {
some_felt252: string;
some_array: string[];
someFelt252: string;
someArray: string[];
}

function getDomain(chainId: string): typedData.StarkNetDomain {
function getDomain(chainId: string): StarknetDomain {
return {
name: "dappName",
version: "1",
Expand All @@ -31,18 +31,18 @@ function getTypedDataHash(myStruct: StructWithArray, chainId: string, owner: big

// Needed to reproduce the same structure as:
// https://github.com/0xs34n/starknet.js/blob/1a63522ef71eed2ff70f82a886e503adc32d4df9/__mocks__/typedDataStructArrayExample.json
function getTypedData(myStruct: StructWithArray, chainId: string): typedData.TypedData {
function getTypedData(myStruct: StructWithArray, chainId: string): TypedData {
return {
types,
primaryType: "StructWithArray",
domain: getDomain(chainId),
message: { ...myStruct },
message: { some_felt252: myStruct.someFelt252, some_array: myStruct.someArray },
};
}

const structWithArray: StructWithArray = {
some_felt252: "712",
some_array: ["4", "2"],
someFelt252: "712",
someArray: ["4", "2"],
};

console.log(`test test_valid_hash ${getTypedDataHash(structWithArray, "0", 420n)};`);
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { typedData } from "starknet";
import { StarknetDomain, TypedData, typedData } from "starknet";

const types = {
StarkNetDomain: [
Expand All @@ -14,15 +14,15 @@ const types = {
};

interface StructWithMerkletree {
some_felt252: string;
some_merkletree_root: SomeLeaf[];
someFelt252: string;
someMerkletreeRoot: SomeLeaf[];
}

export interface SomeLeaf {
contract_address: string;
}

function getDomain(chainId: string): typedData.StarkNetDomain {
function getDomain(chainId: string): StarknetDomain {
return {
name: "dappName",
version: "1",
Expand All @@ -36,18 +36,18 @@ function getTypedDataHash(myStruct: StructWithMerkletree, chainId: string, owner

// Needed to reproduce the same structure as:
// https://github.com/0xs34n/starknet.js/blob/1a63522ef71eed2ff70f82a886e503adc32d4df9/__mocks__/typedDataStructArrayExample.json
function getTypedData(myStruct: StructWithMerkletree, chainId: string): typedData.TypedData {
function getTypedData(myStruct: StructWithMerkletree, chainId: string): TypedData {
return {
types,
primaryType: "StructWithMerkletree",
domain: getDomain(chainId),
message: { ...myStruct },
message: { some_felt252: myStruct.someFelt252, some_merkletree_root: myStruct.someMerkletreeRoot },
};
}

const structWithMerkletree: StructWithMerkletree = {
some_felt252: "712",
some_merkletree_root: [{ contract_address: "0x1" }, { contract_address: "0x2" }],
someFelt252: "712",
someMerkletreeRoot: [{ contract_address: "0x1" }, { contract_address: "0x2" }],
};

console.log(`test test_valid_hash ${getTypedDataHash(structWithMerkletree, "0", 420n)};`);
16 changes: 8 additions & 8 deletions scripts/StructWithU256.ts → scripts/v0/StructWithU256.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { typedData, uint256 } from "starknet";
import { StarknetDomain, TypedData, typedData, Uint256, uint256 } from "starknet";

const types = {
StarkNetDomain: [
Expand All @@ -17,11 +17,11 @@ const types = {
};

interface StructWithU256 {
some_felt252: string;
some_u256: uint256.Uint256;
someFelt252: string;
someU256: Uint256;
}

function getDomain(chainId: string): typedData.StarkNetDomain {
function getDomain(chainId: string): StarknetDomain {
return {
name: "dappName",
version: "1",
Expand All @@ -35,18 +35,18 @@ function getTypedDataHash(myStruct: StructWithU256, chainId: string, owner: bigi

// Needed to reproduce the same structure as:
// https://github.com/0xs34n/starknet.js/blob/1a63522ef71eed2ff70f82a886e503adc32d4df9/__mocks__/typedDataStructArrayExample.json
function getTypedData(myStruct: StructWithU256, chainId: string): typedData.TypedData {
function getTypedData(myStruct: StructWithU256, chainId: string): TypedData {
return {
types,
primaryType: "StructWithU256",
domain: getDomain(chainId),
message: { ...myStruct },
message: { some_felt252: myStruct.someFelt252, some_u256: myStruct.someU256 },
};
}

const structWithU256: StructWithU256 = {
some_felt252: "712",
some_u256: uint256.bnToUint256(42),
someFelt252: "712",
someU256: uint256.bnToUint256(42),
};

console.log(`test test_valid_hash ${getTypedDataHash(structWithU256, "0", 420n)};`);
51 changes: 51 additions & 0 deletions scripts/v1/SimpleStruct.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
import { shortString, StarknetDomain, TypedData, typedData, TypedDataRevision } from "starknet";

const types = {
StarknetDomain: [
{ name: "name", type: "shortstring" },
{ name: "version", type: "shortstring" },
{ name: "chainId", type: "shortstring" },
{ name: "revision", type: "shortstring" },
],
// In V1 we privilege user friendly names
SimpleStruct: [
{ name: "Some felt252", type: "felt" },
{ name: "Some u128", type: "u128" },
],
};

interface SimpleStruct {
someFelt252: string;
someU128: string;
}

function getDomain(chainId: string): StarknetDomain {
return {
name: "dappName",
version: shortString.encodeShortString("1"),
chainId,
revision: TypedDataRevision.ACTIVE,
};
}

function getTypedDataHash(myStruct: SimpleStruct, chainId: string, owner: bigint): string {
return typedData.getMessageHash(getTypedData(myStruct, chainId), owner);
}

// Needed to reproduce the same structure as:
// https://github.com/0xs34n/starknet.js/blob/1a63522ef71eed2ff70f82a886e503adc32d4df9/__mocks__/typedDataStructArrayExample.json
function getTypedData(myStruct: SimpleStruct, chainId: string): TypedData {
return {
types,
primaryType: "SimpleStruct",
domain: getDomain(chainId),
message: { "Some felt252": myStruct.someFelt252, "Some u128": myStruct.someU128 },
};
}

const simpleStruct: SimpleStruct = {
someFelt252: "712",
someU128: "42",
};

console.log(`test test_valid_hash ${getTypedDataHash(simpleStruct, "0", 420n)};`);
51 changes: 51 additions & 0 deletions scripts/v1/StructWithArray.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
import { shortString, StarknetDomain, TypedData, typedData, TypedDataRevision } from "starknet";

const types = {
StarknetDomain: [
{ name: "name", type: "shortstring" },
{ name: "version", type: "shortstring" },
{ name: "chainId", type: "shortstring" },
{ name: "revision", type: "shortstring" },
],
// In V1 we privilege user friendly names
StructWithArray: [
{ name: "Some felt252", type: "felt" },
{ name: "Some array", type: "felt*" },
],
};

interface StructWithArray {
someFelt252: string;
someArray: string[];
}

function getDomain(chainId: string): StarknetDomain {
return {
name: "dappName",
version: shortString.encodeShortString("1"),
chainId,
revision: TypedDataRevision.ACTIVE,
};
}

function getTypedDataHash(myStruct: StructWithArray, chainId: string, owner: bigint): string {
return typedData.getMessageHash(getTypedData(myStruct, chainId), owner);
}

// Needed to reproduce the same structure as:
// https://github.com/0xs34n/starknet.js/blob/1a63522ef71eed2ff70f82a886e503adc32d4df9/__mocks__/typedDataStructArrayExample.json
function getTypedData(myStruct: StructWithArray, chainId: string): TypedData {
return {
types,
primaryType: "StructWithArray",
domain: getDomain(chainId),
message: { "Some felt252": myStruct.someFelt252, "Some array": myStruct.someArray },
};
}

const structWithArray: StructWithArray = {
someFelt252: "712",
someArray: ["4", "2"],
};

console.log(`test test_valid_hash ${getTypedDataHash(structWithArray, "0", 420n)};`);
56 changes: 56 additions & 0 deletions scripts/v1/StructWithMerkletree.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
import { shortString, StarknetDomain, TypedData, typedData, TypedDataRevision } from "starknet";

const types = {
StarknetDomain: [
{ name: "name", type: "shortstring" },
{ name: "version", type: "shortstring" },
{ name: "chainId", type: "shortstring" },
{ name: "revision", type: "shortstring" },
],
// In V1 we privilege user friendly names
StructWithMerkletree: [
{ name: "Some felt252", type: "felt" },
{ name: "Some merkletree root", type: "merkletree", contains: "SomeLeaf" },
],
SomeLeaf: [{ name: "contract_address", type: "ContractAddress" }],
};

interface StructWithMerkletree {
someFelt252: string;
someMerkletreeRoot: SomeLeaf[];
}

export interface SomeLeaf {
contract_address: string;
}

function getDomain(chainId: string): StarknetDomain {
return {
name: "dappName",
version: shortString.encodeShortString("1"),
chainId,
revision: TypedDataRevision.ACTIVE,
};
}

function getTypedDataHash(myStruct: StructWithMerkletree, chainId: string, owner: bigint): string {
return typedData.getMessageHash(getTypedData(myStruct, chainId), owner);
}

// Needed to reproduce the same structure as:
// https://github.com/0xs34n/starknet.js/blob/1a63522ef71eed2ff70f82a886e503adc32d4df9/__mocks__/typedDataStructArrayExample.json
function getTypedData(myStruct: StructWithMerkletree, chainId: string): TypedData {
return {
types,
primaryType: "StructWithMerkletree",
domain: getDomain(chainId),
message: { "Some felt252": myStruct.someFelt252, "Some merkletree root": myStruct.someMerkletreeRoot },
};
}

const structWithMerkletree: StructWithMerkletree = {
someFelt252: "712",
someMerkletreeRoot: [{ contract_address: "0x1" }, { contract_address: "0x2" }],
};

console.log(`test test_valid_hash ${getTypedDataHash(structWithMerkletree, "0", 420n)};`);
Loading

0 comments on commit a53f444

Please sign in to comment.