diff --git a/.changeset/plenty-yaks-think.md b/.changeset/plenty-yaks-think.md new file mode 100644 index 0000000..63e7e33 --- /dev/null +++ b/.changeset/plenty-yaks-think.md @@ -0,0 +1,5 @@ +--- +'@joyid/ckb': patch +--- + +refactor: verifyCredential now accept credential info and joyid server url as parameter diff --git a/packages/ckb/src/verify.spec.ts b/packages/ckb/src/verify.spec.ts index d599a4d..6a466e0 100644 --- a/packages/ckb/src/verify.spec.ts +++ b/packages/ckb/src/verify.spec.ts @@ -89,7 +89,37 @@ describe('verify', () => { }) }) - describe.skip('verifyCredential', () => { + describe('verifyCredential', () => { + const joyidServerURL = 'https://api.testnet.joyid.dev/api/v1' + + it('main key r1 with credential object', async () => { + const credential = { + address: + 'ckt1qrfrwcdnvssswdwpn3s9v8fp87emat306ctjwsm3nmlkjg8qyza2cqgqq8n3thvttqfasgrql80e3s5l66lcneyxrsuglhh2', + pubkey: + '93222c01a3514baab677d722b39b3dede3660e9203a729b969f82b98c428d441a32a8094bff3d90827ca326fb88066cf70bae9c67bef8494549315e7d7b152e6', + keyType: 'main_key', + alg: -7, + } as const + + const res = await verifyCredential(credential, joyidServerURL) + expect(res).toBe(true) + }) + + it('sub key r1 with credential object', async () => { + const credential = { + address: + 'ckt1qrfrwcdnvssswdwpn3s9v8fp87emat306ctjwsm3nmlkjg8qyza2cqgqq8n3thvttqfasgrql80e3s5l66lcneyxrsuglhh2', + pubkey: + 'd81a8aca0dfabcb4a67fd3f3518daae06b2e93899fc4988b1dbf2cca496cfe4478444bfb9df5f3d65ec072c82817216442260ce108fa1fbe45999cc24aa93a81', + keyType: 'sub_key', + alg: -7, + } as const + + const res = await verifyCredential(credential, joyidServerURL) + expect(res).toBe(true) + }) + it.skip('r1_main_session_key_auth', async () => { const { address, pubkey, keyType, alg } = r1_main_session_key_sign_credential @@ -97,7 +127,7 @@ describe('verify', () => { expect(res).toBe(true) }) - it('r1_mainkey_auth', async () => { + it.skip('r1_mainkey_auth', async () => { const { address, pubkey, keyType, alg } = r1_mainkey_sign_credential const res = await verifyCredential(pubkey, address, keyType as any, alg) expect(res).toBe(true) @@ -110,7 +140,7 @@ describe('verify', () => { expect(res).toBe(true) }) - it('r1_subkey_auth', async () => { + it.skip('r1_subkey_auth', async () => { const { address, pubkey, keyType, alg } = r1_subkey_sign_credential const res = await verifyCredential(pubkey, address, keyType as any, alg) expect(res).toBe(true) @@ -123,7 +153,7 @@ describe('verify', () => { expect(res).toBe(true) }) - it('rsa_mainkey_auth', async () => { + it.skip('rsa_mainkey_auth', async () => { const { address, pubkey, keyType, alg } = rsa_mainkey_sign_credential const res = await verifyCredential(pubkey, address, keyType as any, alg) expect(res).toBe(true) @@ -136,7 +166,7 @@ describe('verify', () => { expect(res).toBe(true) }) - it('rsa_subkey_auth', async () => { + it.skip('rsa_subkey_auth', async () => { const { address, pubkey, keyType, alg } = rsa_subkey_sign_credential const res = await verifyCredential(pubkey, address, keyType as any, alg) expect(res).toBe(true) diff --git a/packages/ckb/src/verify.ts b/packages/ckb/src/verify.ts index 78c42ac..707d51f 100644 --- a/packages/ckb/src/verify.ts +++ b/packages/ckb/src/verify.ts @@ -141,16 +141,89 @@ export const verifySignature = async ( return verifySessionKeySignature(message, signature, pubkey) } -export const verifyCredential = async ( +interface CredentialInfo { + pubkey: string + address: string + keyType: CredentialKeyType + alg: SigningAlg +} + +/** + * Verifies the credential information. + * + * @param {CredentialInfo} credential Information of the credential to verify. + * @param {string} joyidServerURL The URL of the JOYID server.
+ * Testnet: https://api.testnet.joyid.dev/api/v1
+ * Mainnet: https://api.joy.id/api/v1 + * @returns {Promise} A promise that resolves to a boolean indicating + * whether the verification was successful. + */ +export async function verifyCredential( + credential: CredentialInfo, + joyidServerURL: string +): Promise + +/** + * Verifies the credential information. + * + * @param {string} pubkey The public key of the credential to verify. + * @param {string} address The address of the credential to verify. + * @param {CredentialKeyType} keyType Type of the key. + * @param {SigningAlg} alg The signing algorithm to be used. + * @returns {Promise} A promise that resolves to a boolean indicating + * whether the verification was successful. + */ +export async function verifyCredential( pubkey: string, address: string, keyType: CredentialKeyType, alg: SigningAlg -): Promise => { - const serverURL = getConfig().joyidServerURL +): Promise + +/** + * Verifies the credential information. + * + * @param {string|CredentialInfo} credentialOrPubkey Either the public key or + * the `CredentialInfo`. + * @param {string} addressOrJoyidServerURL Either the address or + * the `joyidServerURL`. + * Testnet: https://api.testnet.joyid.dev/api/v1
+ * Mainnet: https://api.joy.id/api/v1 + * @param {CredentialKeyType} _keyType Type of the key. + * @param {SigningAlg} _alg The signing algorithm to be used. + * @returns {Promise} A promise that resolves to a boolean indicating + * whether the verification was successful. + */ +export async function verifyCredential( + credentialOrPubkey: string | CredentialInfo, + addressOrJoyidServerURL?: string, + _keyType?: CredentialKeyType, + _alg?: SigningAlg +): Promise { + let pubkey: string, + address: string, + keyType: CredentialKeyType, + alg: SigningAlg + + let serverURL = getConfig().joyidServerURL + + if (typeof credentialOrPubkey === 'string') { + pubkey = credentialOrPubkey + address = addressOrJoyidServerURL! + keyType = _keyType! + alg = _alg! + } else { + ;({ pubkey, address, keyType, alg } = credentialOrPubkey) + serverURL = addressOrJoyidServerURL + } + try { const result = await fetch(`${serverURL}/credentials/${address}`, { method: 'GET', + headers: { + 'User-Agent': + 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/126.0.0.0 Safari/537.36', + }, }).then(async (res) => res.json()) return result.credentials.some((c: any) => {