-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #2 from argentlabs/update-example
Update to SNIP12 version 1
- Loading branch information
Showing
29 changed files
with
1,242 additions
and
442 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1 @@ | ||
scarb 0.7.0 | ||
scarb 2.8.3 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 ." |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)};`); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)};`); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)};`); |
Oops, something went wrong.