Skip to content
This repository has been archived by the owner on Jan 22, 2025. It is now read-only.

Add signers example #3048

Merged
merged 1 commit into from
Aug 7, 2024
Merged

Add signers example #3048

merged 1 commit into from
Aug 7, 2024

Conversation

lorisleiva
Copy link
Contributor

@lorisleiva lorisleiva commented Aug 2, 2024

This PR adds an example to the examples folder that show-cases how to create and use various signers. It offers 4 different signer options:

  1. Signing using a generated key pair.
  2. Signing using a key pair loaded from a local JSON file.
  3. Signing using a key pair derived from a seed. (Note that this currently requires a helper method getPublicKeyFromPrivateKey that should soon be available in @solana/keys).
  4. Signing using a no-op signer.

Each of these options go through the same 3 signing methods that:

  • Sign a message using the signer.
  • Sign a transaction using the signer.
  • Sign a transaction by automatically extracting the signers in the transaction message.

Here is an example of the logs we see when running pnpm start on that example.

INFO (Signers): [option 1] Using a generated key pair signer
    address: "EkYtTyNcw9USEjKcQX23puoUUgiEbbiqi32uiSjyTZgs"
INFO (Signers): >>  Signing the message "Hello, World!"
    signature: "5hbhCAsFvV1HcQQ3YVDzQJeAbNDJpjwKo1YxR8TvpXTJCA2vAmeGeyVXRCqtyCGuV4vHKpXzoTgn5QxpJ5HjMyq7"
INFO (Signers): >>  Signing a transfer SOL transaction
    signature: "ztaVZHbN39DGh52XjXa6xrVL1ect53Qa9pmHsvUbNWEX96oApqMzEJFAxNSGoi2fEg1sY3iAFs8S45xXqddpsuV"
INFO (Signers): >>  Signing a transfer SOL transaction using its registered signers
    signature: "ztaVZHbN39DGh52XjXa6xrVL1ect53Qa9pmHsvUbNWEX96oApqMzEJFAxNSGoi2fEg1sY3iAFs8S45xXqddpsuV"

INFO (Signers): [option 2] Using a key pair signer from a JSON file
    address: "BoK4mWeYVU6LdgNfo8QF7zxmTRiHw7VKMhodToZgrRup"
INFO (Signers): >>  Signing the message "Hello, World!"
    signature: "5tZ5zqXGcjrt6QHpUbNsdff59CTyQqfY8UYRpB5baxJXvokmwKEau1UknvURjmMTyZvhbYfaXC6A9iuWzU6rsCdS"
INFO (Signers): >>  Signing a transfer SOL transaction
    signature: "Jnnos7xeUJnv2BSVqjYb4Vs6AviHdMcFRCbYYRVrsXGsHnZTBBoh3b7BV1APTniWeP2mNG2DvXW4U4G8L8K8Dso"
INFO (Signers): >>  Signing a transfer SOL transaction using its registered signers
    signature: "Jnnos7xeUJnv2BSVqjYb4Vs6AviHdMcFRCbYYRVrsXGsHnZTBBoh3b7BV1APTniWeP2mNG2DvXW4U4G8L8K8Dso"

INFO (Signers): [option 3] Using a key pair signer from a seed
    address: "A4fhR7TUeaj8qNS7PnBHdU4BnyZzuegK27AvHbSx5bhB"
INFO (Signers): >>  Signing the message "Hello, World!"
    signature: "wL5M1pSx1eyzKhqzSwP4jyTwTQNihFsAGEx52C9PeqNrQpjEQNoyvYkFcPLZWCSJi4bbwgNHR14awse5GrWn9Cu"
INFO (Signers): >>  Signing a transfer SOL transaction
    signature: "3EYnSrL7136Rca3YGbSYfHP3w52AZEv8nMBLofhu25r6XNfmduMWhj3vLemZkRNSu9QAzbAV5tCZXWAidZ4oPfis"
INFO (Signers): >>  Signing a transfer SOL transaction using its registered signers
    signature: "3EYnSrL7136Rca3YGbSYfHP3w52AZEv8nMBLofhu25r6XNfmduMWhj3vLemZkRNSu9QAzbAV5tCZXWAidZ4oPfis"

INFO (Signers): [option 4] Using a no-op signer
    address: "HpgzmkFMyHxKAZ8ym6prLdxQ8H3GmQaSBcyimW31dQoe"
INFO (Signers): >>  Signing the message "Hello, World!"
    signature: null
INFO (Signers): >>  Signing a transfer SOL transaction
    signature: null
INFO (Signers): >>  Signing a transfer SOL transaction using its registered signers
    signature: null

Copy link

changeset-bot bot commented Aug 2, 2024

⚠️ No Changeset found

Latest commit: df4c052

Merging this PR will not cause a version bump for any packages. If these changes should not result in a new version, you're good to go. If these changes should result in a version bump, you need to add a changeset.

Click here to learn what changesets are, and how to add one.

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

@lorisleiva lorisleiva marked this pull request as ready for review August 2, 2024 12:37
@lorisleiva lorisleiva force-pushed the loris/signers-example branch 2 times, most recently from a37e453 to b557367 Compare August 2, 2024 14:53
@lorisleiva lorisleiva changed the base branch from master to loris/create-keypair-signer-from-private-key-bytes August 2, 2024 14:53
@lorisleiva lorisleiva force-pushed the loris/signers-example branch from b557367 to d5bd3ce Compare August 2, 2024 14:57
Copy link
Contributor

@steveluscher steveluscher left a comment

Choose a reason for hiding this comment

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

Would this example land harder if the imported signers had the same public key address? I feel like the string -> seed -> keypair example might be confusing to some (most people probably just think ‘my private key exists, it has a seed, and it's these exact 256 bits’) and is sort of orthogonal to just trying to demonstrate the signers API.

Comment on lines 40 to 43
/**
* SETUP: INSTALL WEB CRYPTO POLYFILL
* This polyfill may be required to access the Web Crypto API in your environment.
*/
// @ts-expect-error FIXME(https://github.com/solana-labs/solana-web3.js/pull/3047)
install();
Copy link
Contributor

Choose a reason for hiding this comment

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

I opted not to do this in the examples, since they just run in Node where Ed25519 is always supported.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I'm not too sure why but I'm getting No key generation implementation could be found if I don't import the polyfill. Weird as I'm using node v18.19.0. Any idea how that could be?

Copy link
Contributor

Choose a reason for hiding this comment

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

That doesn't seem right. Says it was implemented in v18.4.0. Let's investigate this.

Copy link
Contributor

Choose a reason for hiding this comment

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

K, so in Node 18, crypto is not injected into the global scope by default. You have to add --experimental-global-webcrypto after tsx.

You can do this (to all the examples) or not, but I think we're saying that the new web3.js supports Node LTS and up, which at this point is >20.16.0.

Copy link
Contributor

Choose a reason for hiding this comment

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

Either way I'd prefer not to have the polyfill in the example, lest people copy and paste it blindly.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Thank you for looking into this. I thought we were still supporting Node 18 which is why I was using this version but I've bumped it to 20+ and everything works without the polyfill now so I've removed it. Good to know it still works with 18 with an experimental flag though.

examples/signers/src/example.ts Outdated Show resolved Hide resolved
@lorisleiva lorisleiva force-pushed the loris/create-keypair-signer-from-private-key-bytes branch from 993119b to e8a2dd9 Compare August 5, 2024 11:32
@lorisleiva lorisleiva force-pushed the loris/signers-example branch from d5bd3ce to 78fc903 Compare August 5, 2024 11:32
@lorisleiva lorisleiva force-pushed the loris/create-keypair-signer-from-private-key-bytes branch from e8a2dd9 to 4872e7a Compare August 5, 2024 11:36
@lorisleiva lorisleiva force-pushed the loris/signers-example branch from 78fc903 to 5e9ffad Compare August 5, 2024 11:36
@lorisleiva lorisleiva force-pushed the loris/create-keypair-signer-from-private-key-bytes branch from 4872e7a to aa40be3 Compare August 5, 2024 11:45
@lorisleiva lorisleiva force-pushed the loris/signers-example branch from 5e9ffad to df588e1 Compare August 5, 2024 11:45
@lorisleiva lorisleiva force-pushed the loris/create-keypair-signer-from-private-key-bytes branch from aa40be3 to 2b7d98a Compare August 5, 2024 11:51
@lorisleiva lorisleiva force-pushed the loris/signers-example branch from df588e1 to 737fb19 Compare August 5, 2024 11:51
@lorisleiva lorisleiva force-pushed the loris/create-keypair-signer-from-private-key-bytes branch from 2b7d98a to b678f4f Compare August 5, 2024 12:02
@lorisleiva lorisleiva force-pushed the loris/signers-example branch 5 times, most recently from 5009333 to f2b5bf4 Compare August 5, 2024 12:23
@lorisleiva
Copy link
Contributor Author

@steveluscher

I feel like the string -> seed -> keypair example might be confusing to some [...] and is sort of orthogonal to just trying to demonstrate the signers API.

Yeah that's a fair point. I've changed this logic to simply use the first 32 bytes of the example-keypair.json file. Which brings us to the next point.

Would this example land harder if the imported signers had the same public key address?

By making the change mentioned above, we know have option 2 and 3 which uses the same signer. I've also changed the no-op signer to use the same address so now option 2, 3 and 4 are the same signer. I believe option 1 cannot be included in this though since we're trying to showcase generated signers.

@lorisleiva lorisleiva requested a review from steveluscher August 5, 2024 12:27
Copy link
Contributor

@steveluscher steveluscher left a comment

Choose a reason for hiding this comment

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

Added via Giphy

@lorisleiva lorisleiva force-pushed the loris/create-keypair-signer-from-private-key-bytes branch from b678f4f to 1f4938d Compare August 7, 2024 08:09
@lorisleiva lorisleiva force-pushed the loris/signers-example branch from f2b5bf4 to e06db6a Compare August 7, 2024 08:09
Copy link

socket-security bot commented Aug 7, 2024

New and removed dependencies detected. Learn more about Socket for GitHub ↗︎

Package New capabilities Transitives Size Publisher

🚮 Removed packages: npm/whatwg-fetch@3.6.20

View full report↗︎

@lorisleiva lorisleiva force-pushed the loris/signers-example branch 2 times, most recently from d352179 to 4de03f4 Compare August 7, 2024 08:27
Copy link
Contributor Author

lorisleiva commented Aug 7, 2024

Merge activity

  • Aug 7, 4:34 AM EDT: @lorisleiva started a stack merge that includes this pull request via Graphite.
  • Aug 7, 4:48 AM EDT: Graphite rebased this pull request as part of a merge.
  • Aug 7, 4:50 AM EDT: Graphite rebased this pull request as part of a merge.
  • Aug 7, 4:50 AM EDT: @lorisleiva merged this pull request with Graphite.

@lorisleiva lorisleiva changed the base branch from loris/create-keypair-signer-from-private-key-bytes to graphite-base/3048 August 7, 2024 08:44
@lorisleiva lorisleiva changed the base branch from graphite-base/3048 to master August 7, 2024 08:46
@lorisleiva lorisleiva force-pushed the loris/signers-example branch from 4de03f4 to 476d388 Compare August 7, 2024 08:47
@lorisleiva lorisleiva force-pushed the loris/signers-example branch from 476d388 to df4c052 Compare August 7, 2024 08:49
@lorisleiva lorisleiva merged commit 889d0eb into master Aug 7, 2024
5 of 6 checks passed
@lorisleiva lorisleiva deleted the loris/signers-example branch August 7, 2024 08:50
Copy link
Contributor

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 23, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants