-
Notifications
You must be signed in to change notification settings - Fork 64
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
Chore/sign message clarificaitons #33
Merged
nickfrosty
merged 25 commits into
solana-developers:main
from
dialectlabs:chore/sign-message-clarificaitons
Oct 2, 2024
Merged
Changes from all commits
Commits
Show all changes
25 commits
Select commit
Hold shift + click to select a range
3ea319e
upd readme
tsmbl 5ee2a0b
add utility fns for sign message
tsmbl f003069
configure workspaces
tsmbl 2b290e3
fix contributors github
tsmbl 5705532
solana-actions: fix build errors
tsmbl 9f2639c
solana-actions: add tests for signMessageData
tsmbl 52d808d
actions-spec: update readme
tsmbl 2418e38
cleanup
tsmbl 65888c2
chains ids is optional verification param
tsmbl beb8c77
normalize domains before comparing
tsmbl 8f2d2d1
Update packages/actions-spec/README.md
tsmbl 7f5782a
Update packages/actions-spec/README.md
tsmbl b74b60a
Update packages/actions-spec/README.md
tsmbl 8722f60
Update packages/actions-spec/README.md
tsmbl 6008f87
Update packages/solana-actions/src/signMessageData.ts
tsmbl c759a25
Update packages/solana-actions/test/signMessageData.test.ts
tsmbl c338aa2
Update packages/solana-actions/test/signMessageData.test.ts
tsmbl bfd2494
Update packages/solana-actions/test/signMessageData.test.ts
tsmbl 6173eb0
Update packages/solana-actions/test/signMessageData.test.ts
tsmbl 4c6c45e
Update packages/solana-actions/test/signMessageData.test.ts
tsmbl 5ffe0b6
Update packages/solana-actions/src/signMessageData.ts
tsmbl 52a6eb2
Update packages/solana-actions/test/signMessageData.test.ts
tsmbl a24524e
Update packages/solana-actions/test/signMessageData.test.ts
tsmbl 819a19b
Update packages/solana-actions/test/signMessageData.test.ts
tsmbl ac9b005
review fixes
tsmbl 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
Large diffs are not rendered by default.
Oops, something went wrong.
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
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,147 @@ | ||||||
import type { SignMessageData } from "@solana/actions-spec"; | ||||||
|
||||||
export interface SignMessageVerificationOptions { | ||||||
expectedAddress?: string; | ||||||
expectedDomains?: string[]; | ||||||
expectedChainIds?: string[]; | ||||||
issuedAtThreshold?: number; | ||||||
} | ||||||
|
||||||
export enum SignMessageVerificationErrorType { | ||||||
ADDRESS_MISMATCH = "ADDRESS_MISMATCH", | ||||||
DOMAIN_MISMATCH = "DOMAIN_MISMATCH", | ||||||
CHAIN_ID_MISMATCH = "CHAIN_ID_MISMATCH", | ||||||
ISSUED_TOO_FAR_IN_THE_PAST = "ISSUED_TOO_FAR_IN_THE_PAST", | ||||||
ISSUED_TOO_FAR_IN_THE_FUTURE = "ISSUED_TOO_FAR_IN_THE_FUTURE", | ||||||
INVALID_DATA = "INVALID_DATA", | ||||||
} | ||||||
|
||||||
const DOMAIN = | ||||||
"(?<domain>[^\\n]+?) wants you to sign a message with your account:\\n"; | ||||||
const ADDRESS = "(?<address>[^\\n]+)(?:\\n|$)"; | ||||||
const STATEMENT = "(?:\\n(?<statement>[\\S\\s]*?)(?:\\n|$))"; | ||||||
const CHAIN_ID = "(?:\\nChain ID: (?<chainId>[^\\n]+))?"; | ||||||
const NONCE = "\\nNonce: (?<nonce>[^\\n]+)"; | ||||||
const ISSUED_AT = "\\nIssued At: (?<issuedAt>[^\\n]+)"; | ||||||
const FIELDS = `${CHAIN_ID}${NONCE}${ISSUED_AT}`; | ||||||
const MESSAGE = new RegExp(`^${DOMAIN}${ADDRESS}${STATEMENT}${FIELDS}\\n*$`); | ||||||
|
||||||
/** | ||||||
* Create a human-readable message text for the user to sign. | ||||||
* | ||||||
* @param input The data to be signed. | ||||||
* @returns The message text. | ||||||
*/ | ||||||
export function createSignMessageText(input: SignMessageData): string { | ||||||
let message = `${input.domain} wants you to sign a message with your account:\n`; | ||||||
message += `${input.address}`; | ||||||
message += `\n\n${input.statement}`; | ||||||
const fields: string[] = []; | ||||||
|
||||||
if (input.chainId) { | ||||||
fields.push(`Chain ID: ${input.chainId}`); | ||||||
} | ||||||
fields.push(`Nonce: ${input.nonce}`); | ||||||
fields.push(`Issued At: ${input.issuedAt}`); | ||||||
message += `\n\n${fields.join("\n")}`; | ||||||
|
||||||
return message; | ||||||
} | ||||||
|
||||||
/** | ||||||
* Parse the sign message text to extract the data to be signed. | ||||||
* @param text The message text to be parsed. | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think |
||||||
*/ | ||||||
export function parseSignMessageText(text: string): SignMessageData | null { | ||||||
const match = MESSAGE.exec(text); | ||||||
if (!match) return null; | ||||||
const groups = match.groups; | ||||||
if (!groups) return null; | ||||||
|
||||||
return { | ||||||
domain: groups.domain, | ||||||
address: groups.address, | ||||||
statement: groups.statement, | ||||||
nonce: groups.nonce, | ||||||
chainId: groups.chainId, | ||||||
issuedAt: groups.issuedAt, | ||||||
}; | ||||||
} | ||||||
|
||||||
/** | ||||||
* Verify the sign message data before signing. | ||||||
* @param data The data to be signed. | ||||||
* @param opts Options for verification, including the expected address, chainId, issuedAt, and domains. | ||||||
* | ||||||
* @returns An array of errors if the verification fails. | ||||||
*/ | ||||||
export function verifySignMessageData( | ||||||
data: SignMessageData, | ||||||
opts: SignMessageVerificationOptions, | ||||||
) { | ||||||
if ( | ||||||
!data.address || | ||||||
!data.domain || | ||||||
!data.issuedAt || | ||||||
!data.nonce || | ||||||
!data.statement | ||||||
) { | ||||||
return [SignMessageVerificationErrorType.INVALID_DATA]; | ||||||
} | ||||||
|
||||||
try { | ||||||
const { | ||||||
expectedAddress, | ||||||
expectedChainIds, | ||||||
issuedAtThreshold, | ||||||
expectedDomains, | ||||||
} = opts; | ||||||
const errors: SignMessageVerificationErrorType[] = []; | ||||||
const now = Date.now(); | ||||||
|
||||||
// verify if parsed address is same as the expected address | ||||||
if (expectedAddress && data.address !== expectedAddress) { | ||||||
errors.push(SignMessageVerificationErrorType.ADDRESS_MISMATCH); | ||||||
} | ||||||
|
||||||
if (expectedDomains) { | ||||||
const expectedDomainsNormalized = expectedDomains.map(normalizeDomain); | ||||||
const normalizedDomain = normalizeDomain(data.domain); | ||||||
|
||||||
if (!expectedDomainsNormalized.includes(normalizedDomain)) { | ||||||
errors.push(SignMessageVerificationErrorType.DOMAIN_MISMATCH); | ||||||
} | ||||||
} | ||||||
|
||||||
if ( | ||||||
expectedChainIds && | ||||||
data.chainId && | ||||||
!expectedChainIds.includes(data.chainId) | ||||||
) { | ||||||
errors.push(SignMessageVerificationErrorType.CHAIN_ID_MISMATCH); | ||||||
} | ||||||
|
||||||
if (issuedAtThreshold !== undefined) { | ||||||
const iat = Date.parse(data.issuedAt); | ||||||
if (Math.abs(iat - now) > issuedAtThreshold) { | ||||||
if (iat < now) { | ||||||
errors.push( | ||||||
SignMessageVerificationErrorType.ISSUED_TOO_FAR_IN_THE_PAST, | ||||||
); | ||||||
} else { | ||||||
errors.push( | ||||||
SignMessageVerificationErrorType.ISSUED_TOO_FAR_IN_THE_FUTURE, | ||||||
); | ||||||
} | ||||||
} | ||||||
} | ||||||
|
||||||
return errors; | ||||||
} catch (e) { | ||||||
return [SignMessageVerificationErrorType.INVALID_DATA]; | ||||||
} | ||||||
} | ||||||
|
||||||
function normalizeDomain(domain: string): string { | ||||||
return domain.replace(/^www\./, ""); | ||||||
} |
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.
this works for now since the
createPostResponse
does not yet support the recently added optional transactions and external links are not supported at all in the current version of@solana/actions
either way, this will also require updating the spec package inside the
@solana/actions
package.I just opened an issue to track it and will complete the update in a different PR: #34
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.
agreed, just wanted to address existing build errors
thanks for creating the issue