-
Notifications
You must be signed in to change notification settings - Fork 1
Adds signing for eip712 txs #14
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
Open
netbonus
wants to merge
1
commit into
master
Choose a base branch
from
nb/sign-eip712
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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,4 +1,5 @@ | ||
| export * from './buildDepositData'; | ||
| export * from './changeBLSCredentials'; | ||
| export * from './getAddress'; | ||
| export * from './getPubkey'; | ||
| export * from './getPubkey'; | ||
| export * from './signEIP712'; |
This file contains hidden or 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,185 @@ | ||
| import { Client } from "gridplus-sdk"; | ||
| import { | ||
| clearPrintedLines, | ||
| closeSpinner, | ||
| printColor, | ||
| startNewSpinner, | ||
| pathStrToInt, | ||
| } from '../utils'; | ||
| import { | ||
| promptForSelect, | ||
| promptForString, | ||
| promptForBool, | ||
| promptGetPath | ||
| } from '../prompts'; | ||
| import { validateEIP712Data } from '../utils/eip712Validator'; | ||
| import { formatEIP712ForDisplay, formatSignature } from '../utils/eip712Formatter'; | ||
| import { readFileSync, writeFileSync } from 'fs'; | ||
|
|
||
| /** | ||
| * Sign EIP-712 typed data using the Lattice hardware wallet. | ||
| * Supports multiple input methods and output formats. | ||
| */ | ||
| export async function cmdSignEIP712(client: Client) { | ||
| try { | ||
| // Get input method | ||
| const inputMethod = await promptForSelect( | ||
| "How would you like to provide the EIP-712 data?", | ||
| ["From file (JSON)", "Direct input (JSON string)", "Cancel"] | ||
| ); | ||
|
|
||
| if (inputMethod === "Cancel") { | ||
| return; | ||
| } | ||
|
|
||
| let typedDataStr: string; | ||
|
|
||
| // Get the typed data based on input method | ||
| if (inputMethod === "From file (JSON)") { | ||
| const filePath = await promptForString("Enter path to JSON file: "); | ||
| try { | ||
| typedDataStr = readFileSync(filePath, 'utf-8'); | ||
| } catch (err) { | ||
| printColor(`Failed to read file: ${err}`, "red"); | ||
| return; | ||
| } | ||
| } else { | ||
| typedDataStr = await promptForString( | ||
| "Enter EIP-712 JSON (or paste and press Enter): " | ||
| ); | ||
| } | ||
|
|
||
| // Parse and validate the typed data | ||
| let typedData: any; | ||
| try { | ||
| typedData = JSON.parse(typedDataStr); | ||
| } catch (err) { | ||
| printColor("Invalid JSON format", "red"); | ||
| return; | ||
| } | ||
|
|
||
| // Validate EIP-712 structure | ||
| const validationResult = validateEIP712Data(typedData); | ||
| if (!validationResult.isValid) { | ||
| printColor(`Invalid EIP-712 structure: ${validationResult.error}`, "red"); | ||
| return; | ||
| } | ||
|
|
||
| // Display formatted preview | ||
| console.log("\n📋 EIP-712 Data Preview:"); | ||
| console.log("========================"); | ||
| const preview = formatEIP712ForDisplay(typedData); | ||
| console.log(preview); | ||
| console.log("========================\n"); | ||
|
|
||
| // Confirm before signing | ||
| const shouldSign = await promptForBool( | ||
| "Do you want to sign this data? " | ||
| ); | ||
|
|
||
| if (!shouldSign) { | ||
| printColor("Signing cancelled", "yellow"); | ||
| return; | ||
| } | ||
|
|
||
| // Ask for derivation path (optional, use default ETH path) | ||
| const useDefaultPath = await promptForBool( | ||
| "Use default Ethereum signing path (m/44'/60'/0'/0/0)? " | ||
| ); | ||
|
|
||
| let signerPath: number[]; | ||
| if (useDefaultPath) { | ||
| signerPath = pathStrToInt("m/44'/60'/0'/0/0"); | ||
| } else { | ||
| const pathStr = await promptGetPath("m/44'/60'/0'/0/0"); | ||
| signerPath = pathStrToInt(pathStr); | ||
| } | ||
|
|
||
| // Prepare the signing request | ||
| const spinner = startNewSpinner("Requesting signature from Lattice..."); | ||
| let spinnerClosed = false; | ||
|
|
||
| try { | ||
| // Sign the typed data using GridPlus SDK format | ||
| // According to the SDK docs, EIP-712 uses protocol: 'eip712' with ETH_MSG currency | ||
| const signatureResult = await client.sign({ | ||
| currency: 'ETH_MSG', | ||
| data: { | ||
| protocol: 'eip712', | ||
| payload: typedData, | ||
| signerPath: signerPath | ||
| } | ||
| } as any); | ||
|
|
||
| closeSpinner(spinner, "Signature received from Lattice"); | ||
| spinnerClosed = true; | ||
|
|
||
| // Format the signature - GridPlus SDK returns {sig: {v, r, s}, signer: Buffer} | ||
| const formattedSig = formatSignature(signatureResult.sig); | ||
|
|
||
| // Display signature | ||
| console.log("\n✅ Signature Generated:"); | ||
| console.log("========================"); | ||
| console.log(`Full signature: ${formattedSig.full}`); | ||
| console.log(`\nComponents:`); | ||
| console.log(` v: ${formattedSig.v}`); | ||
| console.log(` r: ${formattedSig.r}`); | ||
| console.log(` s: ${formattedSig.s}`); | ||
|
|
||
| // Show signer address if available | ||
| if (signatureResult.signer) { | ||
| const signerAddress = '0x' + signatureResult.signer.toString('hex'); | ||
| console.log(`\nSigner address: ${signerAddress}`); | ||
| } | ||
| console.log("========================\n"); | ||
|
|
||
| // Ask if user wants to save | ||
| const shouldSave = await promptForBool( | ||
| "Save signature to file? " | ||
| ); | ||
|
|
||
| if (shouldSave) { | ||
| const outputPath = await promptForString( | ||
| "Enter output file path: ", | ||
| "signature.json" | ||
| ); | ||
|
|
||
| const output = { | ||
| signature: formattedSig.full, | ||
| components: { | ||
| v: formattedSig.v, | ||
| r: formattedSig.r, | ||
| s: formattedSig.s | ||
| }, | ||
| signer: signatureResult.signer ? '0x' + signatureResult.signer.toString('hex') : undefined, | ||
| typedData: typedData, | ||
| timestamp: new Date().toISOString() | ||
| }; | ||
|
|
||
| try { | ||
| writeFileSync(outputPath, JSON.stringify(output, null, 2)); | ||
| printColor(`Signature saved to ${outputPath}`, "green"); | ||
| } catch (err) { | ||
| printColor(`Failed to save signature: ${err}`, "red"); | ||
| } | ||
| } | ||
|
|
||
| } catch (err) { | ||
| if (!spinnerClosed) { | ||
| closeSpinner( | ||
| spinner, | ||
| `Failed to sign data: ${err instanceof Error ? err.message : 'Unknown error'}`, | ||
| false | ||
| ); | ||
| } else { | ||
| printColor(`Error after signing: ${err instanceof Error ? err.message : 'Unknown error'}`, "red"); | ||
| } | ||
| } | ||
|
|
||
| } catch (err) { | ||
| printColor( | ||
| `Error in EIP-712 signing: ${err instanceof Error ? err.message : 'Unknown error'}`, | ||
| "red" | ||
| ); | ||
| } | ||
| } | ||
This file contains hidden or 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 hidden or 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.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
A minor: It's a best practice to end files with newline character that's why GH is showing the ⛔ icon.