-
Notifications
You must be signed in to change notification settings - Fork 111
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* General semantic updates and EncodeType adjustment * Improvements and bug fixes * Implements typedData unmarshal * Adds Message support on Unmarshal typedData * Rename files and create types file * Creates TestGeneral_CreateMessageWithTypes * Clears some curve methods and adds Poseidon method * Adds revision.go file, new Revision field of TypedData * Basic implementation working with a rev 0 typedData * Rename files * Some code adjustments * Adds new examples from starknet.js and implements new mock logic * init function on revision package * Adds first version of Validate feature * Adds revision 1 support to encodeType * Adds 'selector' support in encodeData * restructs encodeData * adds handleStandardTypes and handleArrays functions * support to arrays, new types and new Domain unmarshal * fixed support to bool, new example being tested * fixes errors in encodeType, 'example_presetTypes' supported * implements merkletree encode * adds support to 'mail_StructArray' json example * Fixes error inStringToByteArrFelt func, 'v1Nested' example passing * Started to refactor 'encodeType' func * Fixes bug with merkletree * Creates the verifyType func * implements enum encoding * Removes the validation method, it will be added later * creates big example for testing purpose * removes the types file * adds code comments and descriptions * rename folder and file names * creates typedData example and change READMEs * Update utils/Felt.go Co-authored-by: Rian Hughes <[email protected]> * Update utils/keccak.go Co-authored-by: Rian Hughes <[email protected]> * addresses Rian's comment about GetMessageHash * Revert enum wrong encode as it was fixed by starknet.js * Creates ValidationSignature helper * Removes the StrToFelt utility * Improves 'chainId' validation * Changes private fields of TypedData to public --------- Co-authored-by: Rian Hughes <[email protected]>
- Loading branch information
1 parent
1c399f4
commit 8ecf779
Showing
27 changed files
with
2,253 additions
and
532 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 |
---|---|---|
|
@@ -9,7 +9,6 @@ rpc*/.env.testnet | |
|
||
tmp/ | ||
|
||
examples/**/*.json | ||
examples/**/*.sum | ||
|
||
*/**/*abi.json | ||
|
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,12 @@ | ||
This example shows how to sign and verify a typed data. | ||
|
||
Steps: | ||
1. Rename the ".env.template" file located at the root of the "examples" folder to ".env" | ||
1. Uncomment, and assign your Sepolia testnet endpoint to the `RPC_PROVIDER_URL` variable in the ".env" file | ||
1. Uncomment, and assign your account address to the `ACCOUNT_ADDRESS` variable in the ".env" file (make sure to have a few ETH in it) | ||
1. Uncomment, and assign your starknet public key to the `PUBLIC_KEY` variable in the ".env" file | ||
1. Uncomment, and assign your private key to the `PRIVATE_KEY` variable in the ".env" file | ||
1. Make sure you are in the "typedData" directory | ||
1. Execute `go run main.go` | ||
|
||
The message hash, signature and the verification result will be printed at the end of the execution. |
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,35 @@ | ||
{ | ||
"types": { | ||
"StarkNetDomain": [ | ||
{ "name": "name", "type": "felt" }, | ||
{ "name": "version", "type": "felt" }, | ||
{ "name": "chainId", "type": "felt" } | ||
], | ||
"Person": [ | ||
{ "name": "name", "type": "felt" }, | ||
{ "name": "wallet", "type": "felt" } | ||
], | ||
"Mail": [ | ||
{ "name": "from", "type": "Person" }, | ||
{ "name": "to", "type": "Person" }, | ||
{ "name": "contents", "type": "felt" } | ||
] | ||
}, | ||
"primaryType": "Mail", | ||
"domain": { | ||
"name": "StarkNet Mail", | ||
"version": "1", | ||
"chainId": 1 | ||
}, | ||
"message": { | ||
"from": { | ||
"name": "Cow", | ||
"wallet": "0xCD2a3d9F938E13CD947Ec05AbC7FE734Df8DD826" | ||
}, | ||
"to": { | ||
"name": "Bob", | ||
"wallet": "0xbBbBBBBbbBBBbbbBbbBbbbbBBbBbbbbBbBbbBBbB" | ||
}, | ||
"contents": "Hello, Bob!" | ||
} | ||
} |
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,91 @@ | ||
package main | ||
|
||
import ( | ||
"context" | ||
"encoding/json" | ||
"fmt" | ||
"math/big" | ||
"os" | ||
|
||
"github.com/NethermindEth/starknet.go/account" | ||
"github.com/NethermindEth/starknet.go/curve" | ||
"github.com/NethermindEth/starknet.go/rpc" | ||
"github.com/NethermindEth/starknet.go/typedData" | ||
"github.com/NethermindEth/starknet.go/utils" | ||
|
||
setup "github.com/NethermindEth/starknet.go/examples/internal" | ||
) | ||
|
||
// NOTE : Please add in your keys only for testing purposes, in case of a leak you would potentially lose your funds. | ||
|
||
func main() { | ||
// Setup the account | ||
accnt := localSetup() | ||
fmt.Println("Account address:", accnt.AccountAddress) | ||
|
||
// This is how you can initialize a typed data from a JSON file | ||
var ttd typedData.TypedData | ||
content, err := os.ReadFile("./baseExample.json") | ||
if err != nil { | ||
panic(fmt.Errorf("fail to read file: %w", err)) | ||
} | ||
err = json.Unmarshal(content, &ttd) | ||
if err != nil { | ||
panic(fmt.Errorf("fail to unmarshal TypedData: %w", err)) | ||
} | ||
|
||
// This is how you can get the message hash linked to your account address | ||
messageHash, err := ttd.GetMessageHash(accnt.AccountAddress.String()) | ||
if err != nil { | ||
panic(fmt.Errorf("fail to get message hash: %w", err)) | ||
} | ||
fmt.Println("Message hash:", messageHash) | ||
|
||
// This is how you can sign the message hash | ||
signature, err := accnt.Sign(context.Background(), messageHash) | ||
if err != nil { | ||
panic(fmt.Errorf("fail to sign message: %w", err)) | ||
} | ||
fmt.Println("Signature:", signature) | ||
|
||
// This is how you can verify the signature | ||
isValid := curve.VerifySignature(messageHash.String(), signature[0].String(), signature[1].String(), setup.GetPublicKey()) | ||
fmt.Println("Verification result:", isValid) | ||
} | ||
|
||
func localSetup() *account.Account { | ||
// Load variables from '.env' file | ||
rpcProviderUrl := setup.GetRpcProviderUrl() | ||
accountAddress := setup.GetAccountAddress() | ||
accountCairoVersion := setup.GetAccountCairoVersion() | ||
privateKey := setup.GetPrivateKey() | ||
publicKey := setup.GetPublicKey() | ||
|
||
// Initialize connection to RPC provider | ||
client, err := rpc.NewProvider(rpcProviderUrl) | ||
if err != nil { | ||
panic(fmt.Sprintf("Error dialing the RPC provider: %s", err)) | ||
} | ||
|
||
// Initialize the account memkeyStore (set public and private keys) | ||
ks := account.NewMemKeystore() | ||
privKeyBI, ok := new(big.Int).SetString(privateKey, 0) | ||
if !ok { | ||
panic("Fail to convert privKey to bitInt") | ||
} | ||
ks.Put(publicKey, privKeyBI) | ||
|
||
// Here we are converting the account address to felt | ||
accountAddressInFelt, err := utils.HexToFelt(accountAddress) | ||
if err != nil { | ||
fmt.Println("Failed to transform the account address, did you give the hex address?") | ||
panic(err) | ||
} | ||
// Initialize the account | ||
accnt, err := account.NewAccount(client, accountAddressInFelt, publicKey, ks, accountCairoVersion) | ||
if err != nil { | ||
panic(err) | ||
} | ||
|
||
return accnt | ||
} |
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
Oops, something went wrong.