Repository navigation
feat(wallets): createServerSigner returns a sign(message) function #2276
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
albertoelias-crossmint
wants to merge
3
commits into
main
Choose a base branch
from
albertoe/server-signer-sign
base: main
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
Show all changes
3 commits
Select commit
Hold shift + click to select a range
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| --- | ||
| "@crossmint/wallets-sdk": minor | ||
| --- | ||
|
|
||
| `createServerSigner` now returns a `sign(message)` function. It signs a message the API asks the server signer to approve (a transaction or signature approval, or a quorum member's approval message) exactly as the SDK's own server signer does on that chain: raw EIP-191 over 0x-hex on EVM, ed25519 over base58 on Solana, ed25519 over base64 on Stellar. Callers no longer need to rebuild per-chain signing from `keyBytes`. |
81 changes: 81 additions & 0 deletions
81
packages/wallets/src/utils/server-signers/create-server-signer.test.ts
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,81 @@ | ||
| import { describe, it, expect } from "vitest"; | ||
| import base58 from "bs58"; | ||
| import { ed25519 } from "@noble/curves/ed25519"; | ||
| import { recoverMessageAddress } from "viem"; | ||
|
|
||
| import { encodeStellarPublicKey } from "../stellar"; | ||
| import { createServerSigner } from "./createServerSigner"; | ||
|
|
||
| const params = { secret: "a".repeat(64), projectId: "project-123", environment: "staging" }; | ||
|
|
||
| describe("createServerSigner().sign", () => { | ||
| it("EVM: signs the raw hex message so it recovers to the signer's address", async () => { | ||
| const signer = createServerSigner({ ...params, chain: "base-sepolia" }); | ||
| const message = "0x7b2a0c1d9e"; | ||
|
|
||
| const signature = await signer.sign(message); | ||
|
|
||
| expect(signature).toMatch(/^0x[0-9a-f]{130}$/); | ||
| await expect( | ||
| recoverMessageAddress({ message: { raw: message }, signature: signature as `0x${string}` }) | ||
| ).resolves.toBe(signer.address); | ||
| }); | ||
|
|
||
| it("EVM: rejects a message that is not 0x-prefixed hex", async () => { | ||
| const signer = createServerSigner({ ...params, chain: "base-sepolia" }); | ||
|
|
||
| await expect(signer.sign("7b2a")).rejects.toThrow("0x-prefixed hex"); | ||
| }); | ||
|
|
||
| it("Solana: signs the base58-decoded message with the address's ed25519 key, base58 out", async () => { | ||
| const signer = createServerSigner({ ...params, chain: "solana" }); | ||
| const messageBytes = new Uint8Array([1, 2, 3, 250, 251, 252]); | ||
|
|
||
| const signature = await signer.sign(base58.encode(messageBytes)); | ||
|
|
||
| expect(ed25519.verify(base58.decode(signature), messageBytes, base58.decode(signer.address))).toBe(true); | ||
| }); | ||
|
|
||
| it("Solana: rejects a message that is not base58", async () => { | ||
| const signer = createServerSigner({ ...params, chain: "solana" }); | ||
|
|
||
| await expect(signer.sign("0OIl")).rejects.toThrow("base58"); | ||
| }); | ||
|
|
||
| it("Stellar: signs the base64-decoded message with the address's ed25519 key, base64 out", async () => { | ||
| const signer = createServerSigner({ ...params, chain: "stellar" }); | ||
| // A quorum member's Stellar message is the 32-byte auth-entry preimage hash, base64-encoded. | ||
| const messageBytes = new Uint8Array(32).map((_, i) => i * 7); | ||
| const publicKey = ed25519.getPublicKey(signer.keyBytes); | ||
| expect(encodeStellarPublicKey(publicKey)).toBe(signer.address); | ||
|
|
||
| const signature = await signer.sign(Buffer.from(messageBytes).toString("base64")); | ||
|
|
||
| expect(ed25519.verify(Buffer.from(signature, "base64"), messageBytes, publicKey)).toBe(true); | ||
| }); | ||
|
|
||
| it("Stellar: rejects a message that is not base64", async () => { | ||
| const signer = createServerSigner({ ...params, chain: "stellar" }); | ||
|
|
||
| await expect(signer.sign("not base64!")).rejects.toThrow("base64"); | ||
| }); | ||
|
|
||
| it("signs again with the same signer after a first signature", async () => { | ||
| const signer = createServerSigner({ ...params, chain: "base-sepolia" }); | ||
| const message = "0x01"; | ||
|
|
||
| const first = await signer.sign(message); | ||
|
|
||
| await expect(signer.sign(message)).resolves.toBe(first); | ||
| }); | ||
|
|
||
| it("leaves keyBytes intact after signing", async () => { | ||
| const signer = createServerSigner({ ...params, chain: "solana" }); | ||
| const before = signer.keyBytes.slice(); | ||
|
|
||
| await signer.sign(base58.encode(new Uint8Array([9]))); | ||
|
|
||
| expect(signer.keyBytes).toEqual(before); | ||
| expect(before.some((b) => b !== 0)).toBe(true); | ||
| }); | ||
| }); | ||
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.
Uh oh!
There was an error while loading. Please reload this page.