Skip to content
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

A useSignIn() hook for accessing the Sign In With Solana feature #2928

Conversation

steveluscher
Copy link
Collaborator

@steveluscher steveluscher commented Jul 10, 2024

Summary

This hook returns essentially the raw sign in function from the solana:signIn feature. It upcasts the returned WalletAccount to a UiWalletAccount usable with the rest of the @solana/react API.

Callers are responsible for verifying the signed message and its signature.

Test plan

pnpm turbo test:unit:node test:unit:browser test:typecheck

Copy link

changeset-bot bot commented Jul 10, 2024

🦋 Changeset detected

Latest commit: 686539f

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 37 packages
Name Type
@solana/react Minor
@solana/accounts Minor
@solana/addresses Minor
@solana/assertions Minor
@solana/codecs-core Minor
@solana/codecs-data-structures Minor
@solana/codecs-numbers Minor
@solana/codecs-strings Minor
@solana/codecs Minor
@solana/compat Minor
@solana/errors Minor
@solana/fast-stable-stringify Minor
@solana/functional Minor
@solana/instructions Minor
@solana/keys Minor
@solana/web3.js-experimental Minor
@solana/options Minor
@solana/programs Minor
@solana/rpc-api Minor
@solana/rpc-graphql Minor
@solana/rpc-parsed-types Minor
@solana/rpc-spec-types Minor
@solana/rpc-spec Minor
@solana/rpc-subscriptions-api Minor
@solana/rpc-subscriptions-spec Minor
@solana/rpc-subscriptions-transport-websocket Minor
@solana/rpc-subscriptions Minor
@solana/rpc-transformers Minor
@solana/rpc-transport-http Minor
@solana/rpc-types Minor
@solana/rpc Minor
@solana/signers Minor
@solana/sysvars Minor
@solana/transaction-confirmation Minor
@solana/transaction-messages Minor
@solana/transactions Minor
@solana/webcrypto-ed25519-polyfill Minor

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

Copy link
Collaborator Author

steveluscher commented Jul 10, 2024

@steveluscher steveluscher force-pushed the 07-10-a_usesignin_hook_for_accessing_the_sign_in_with_solana_feature branch 3 times, most recently from 46ad8e6 to 7aaf056 Compare July 10, 2024 22:55
return useCallback(
async (...inputs) => {
const inputsWithAddressAndChainId = inputs.map(input => ({
...input,
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we default the domain to window.location.host if not supplied?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it makes sense yeah. I'm trying to think about security reasons not to do this but can't see any.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We can, but the wallet must set this anyway if it's not provided.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm going to leave it unsupplied.

Base automatically changed from 07-09-reset_errors_when_changing_account_chain to master July 11, 2024 16:59
);
}

function useSignIns(
Copy link
Collaborator

@jordaaash jordaaash Jul 11, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we can do away with this plural API, seems very unlikely to be used and certainly doesn't work today.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh I see, this isn't exported anyway, this is just internally how it's implemented because the Wallet Standard is this way. Carry on!

const resultsWithoutSignatureType = results.map(
({
account,
signatureType: _, // Solana signatures are always of type `ed25519` so drop this property.
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, this was a misguided design. The idea was that we could support secp256r1 signatures potentially later, but that's better handled through a new feature/version.

try {
const { account, signedMessage, signature } = await signIn({
domain: window.location.host,
nonce: csrfToken,
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Aside: It's extremely dumb that SIWE has request-id and nonce which are duplicative in almost all cases, and JWT-esque issued-at and expires-at timestamps when these are not intended to be and should not be used as JWTs directly. Apps should generate a random session ID from the server and use it as request-id, then verify and issue a proper JWT if needed from the server the correct way.

Ideally we can reflect the best way to do this in docs and examples and users should not touch most of the API.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: I do prefer requestId to nonce because it makes it more clear that it should come from a server, it's not using a cryptography term improperly, and it's not confused by Brits for... other meanings of the term.

onClick={async () => {
try {
const { account, signedMessage, signature } = await signIn({
domain: window.location.host,
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

domain probably not needed in the example since apps shouldn't provide it.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You're right about that! I noticed some wallets fail awkwardly when you don't supply it, but that's a bug with their implementation.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For posterity, one such error when you don't supply domain.

image.png

Comment on lines +32 to +33
export function useSignIn(uiWallet: UiWallet): (input?: Input) => Promise<Output>;
export function useSignIn(uiWalletHandle: UiWalletHandle): (input?: Input) => Promise<Output> {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice 👍

Copy link
Collaborator

@jordaaash jordaaash left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM, had some non-critical comments that apply mainly to examples/defaults

@steveluscher steveluscher force-pushed the 07-10-a_usesignin_hook_for_accessing_the_sign_in_with_solana_feature branch from 7aaf056 to d7db531 Compare July 12, 2024 17:49
Copy link
Collaborator Author

steveluscher commented Jul 12, 2024

Merge activity

  • Jul 12, 10:50 AM PDT: @steveluscher started a stack merge that includes this pull request via Graphite.
  • Jul 12, 10:51 AM PDT: Graphite rebased this pull request as part of a merge.
  • Jul 12, 10:52 AM PDT: Graphite rebased this pull request as part of a merge.
  • Jul 12, 10:54 AM PDT: Graphite rebased this pull request as part of a merge.
  • Jul 12, 10:55 AM PDT: @steveluscher merged this pull request with Graphite.

@steveluscher steveluscher force-pushed the 07-10-a_usesignin_hook_for_accessing_the_sign_in_with_solana_feature branch 2 times, most recently from 908235e to bcf481a Compare July 12, 2024 17:51
@steveluscher steveluscher force-pushed the 07-10-a_usesignin_hook_for_accessing_the_sign_in_with_solana_feature branch from bcf481a to 686539f Compare July 12, 2024 17:53
@steveluscher steveluscher merged commit bac3747 into master Jul 12, 2024
6 checks passed
@steveluscher steveluscher deleted the 07-10-a_usesignin_hook_for_accessing_the_sign_in_with_solana_feature branch July 12, 2024 17:55
@github-actions github-actions bot mentioned this pull request Jul 12, 2024
Copy link
Contributor

🎉 This PR is included in version 1.95.1 🎉

The release is available on:

Your semantic-release bot 📦🚀

Copy link
Contributor

github-actions bot commented Aug 1, 2024

Because there has been no activity on this PR for 14 days since it was merged, it has been automatically locked. Please open a new issue if it requires a follow up.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Aug 1, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants