-
Notifications
You must be signed in to change notification settings - Fork 31
feat: identity to addresses #315
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
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,6 @@ | ||
| --- | ||
| "@ckb-ccc/core": minor | ||
| --- | ||
|
|
||
| feat(core): `Signer.fromSignature` | ||
|
|
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,7 @@ | ||
| --- | ||
| "@ckb-ccc/core": major | ||
| "@ckb-ccc/joy-id": minor | ||
| --- | ||
|
|
||
| feat(joy-id): address info in identity | ||
|
|
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,27 +1,55 @@ | ||
| import { verifySignature } from "@joyid/ckb"; | ||
| import { | ||
| CredentialKeyType, | ||
| SigningAlg, | ||
| verifyCredential, | ||
| verifySignature, | ||
| } from "@joyid/ckb"; | ||
| import { BytesLike } from "../../bytes/index.js"; | ||
| import { hexFrom } from "../../hex/index.js"; | ||
|
|
||
| /** | ||
| * @public | ||
| */ | ||
| export function verifyMessageJoyId( | ||
| export async function verifyMessageJoyId( | ||
| message: string | BytesLike, | ||
| signature: string, | ||
| identity: string, | ||
| ): Promise<boolean> { | ||
| const challenge = | ||
| typeof message === "string" ? message : hexFrom(message).slice(2); | ||
| const { publicKey, keyType } = JSON.parse(identity) as { | ||
| const { address, publicKey, keyType } = JSON.parse(identity) as { | ||
| address: string; | ||
| publicKey: string; | ||
| keyType: string; | ||
| keyType: CredentialKeyType; | ||
| }; | ||
| const signatureObj = JSON.parse(signature) as { | ||
| alg: SigningAlg; | ||
| signature: string; | ||
| message: string; | ||
| }; | ||
|
|
||
| if ( | ||
| !(await verifySignature({ | ||
| challenge, | ||
| pubkey: publicKey, | ||
| keyType, | ||
| ...signatureObj, | ||
| })) | ||
| ) { | ||
| return false; | ||
| } | ||
|
|
||
| // eslint-disable-next-line @typescript-eslint/no-unsafe-argument | ||
| return verifySignature({ | ||
| challenge, | ||
| pubkey: publicKey, | ||
| keyType, | ||
| ...JSON.parse(signature), | ||
| }); | ||
| // I sincerely hope one day we can get rid of the centralized registry | ||
| const registry = address.startsWith("ckb") | ||
| ? "https://api.joy.id/api/v1/" | ||
| : "https://api.testnet.joyid.dev/api/v1/"; | ||
|
Comment on lines
+43
to
+45
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. Hardcoding URLs directly within the function makes the code harder to maintain and test. If these URLs change, you'll have to find and replace them in the code. It would be better to extract these URLs into named constants at the module level. This improves readability and centralizes configuration. |
||
| return verifyCredential( | ||
| { | ||
| pubkey: publicKey, | ||
| address, | ||
| keyType, | ||
| alg: signatureObj.alg, | ||
| }, | ||
| registry, | ||
| ); | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,65 @@ | ||
| import { Address } from "../address/index.js"; | ||
| import { BytesLike } from "../bytes/index.js"; | ||
| import { Client } from "../client/index.js"; | ||
| import { SignerBtcPublicKeyReadonly } from "./btc/index.js"; | ||
| import { SignerCkbPublicKey, SignerCkbScriptReadonly } from "./ckb/index.js"; | ||
| import { SignerDogeAddressReadonly } from "./doge/index.js"; | ||
| import { SignerEvmAddressReadonly } from "./evm/index.js"; | ||
| import { SignerNostrPublicKeyReadonly } from "./nostr/index.js"; | ||
| import { Signature, Signer, SignerSignType } from "./signer/index.js"; | ||
|
|
||
| /** | ||
| * Creates a signer from a signature. | ||
| * | ||
| * @param client - The client instance. | ||
| * @param signature - The signature to create the signer from. | ||
| * @param message - The message that was signed. | ||
| * @param addresses - The addresses to check against the signer. | ||
| * @returns The signer if the signature is valid and the addresses match, otherwise undefined. | ||
| * @throws Error if the signature sign type is unknown. | ||
| */ | ||
| export async function signerFromSignature( | ||
| client: Client, | ||
| signature: Signature, | ||
| message?: string | BytesLike | null, | ||
| ...addresses: (string | string[])[] | ||
| ): Promise<Signer | undefined> { | ||
| if ( | ||
| message != undefined && | ||
| !(await Signer.verifyMessage(message, signature)) | ||
| ) { | ||
| return; | ||
| } | ||
|
|
||
| const signer = await (async () => { | ||
| switch (signature.signType) { | ||
| case SignerSignType.EvmPersonal: | ||
| return new SignerEvmAddressReadonly(client, signature.identity); | ||
| case SignerSignType.BtcEcdsa: | ||
| return new SignerBtcPublicKeyReadonly(client, "", signature.identity); | ||
| case SignerSignType.JoyId: { | ||
| const { address } = JSON.parse(signature.identity) as { | ||
| address: string; | ||
| }; | ||
|
Comment on lines
+41
to
+43
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. Using Consider using a case SignerSignType.JoyId: {
let address: string;
try {
const identity = JSON.parse(signature.identity);
address = identity.address;
} catch (e) {
throw new Error(`Failed to parse JoyId identity: ${e}`);
}
if (!address) {
throw new Error("Missing address in JoyId identity");
}
return new SignerCkbScriptReadonly(
client,
(await Address.fromString(address, client)).script,
);
} |
||
| return new SignerCkbScriptReadonly( | ||
| client, | ||
| (await Address.fromString(address, client)).script, | ||
| ); | ||
| } | ||
| case SignerSignType.NostrEvent: | ||
| return new SignerNostrPublicKeyReadonly(client, signature.identity); | ||
| case SignerSignType.CkbSecp256k1: | ||
| return new SignerCkbPublicKey(client, signature.identity); | ||
| case SignerSignType.DogeEcdsa: | ||
| return new SignerDogeAddressReadonly(client, signature.identity); | ||
| case SignerSignType.Unknown: | ||
| throw new Error("Unknown signer sign type"); | ||
| } | ||
|
Comment on lines
+35
to
+57
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. The For example: switch (signature.signType) {
// ... other cases
default:
throw new Error(`Unsupported sign type: ${signature.signType as string}`);
} |
||
| })(); | ||
| const signerAddresses = await signer.getAddresses(); | ||
| if (!addresses.flat().every((addr) => signerAddresses.includes(addr))) { | ||
| return; | ||
| } | ||
|
|
||
| return signer; | ||
| } | ||
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.
Using
JSON.parsewith type assertions (as) is not type-safe. If theidentityorsignaturestrings are malformed or don't match the expected structure, this will throw an unhandled exception at runtime, which could crash the application. It's recommended to wrap these parsing operations in atry...catchblock and validate the parsed objects to handle potential errors gracefully.For example: