Skip to content

Commit

Permalink
refactor: verifyCredential now accept credential info and joyid serve…
Browse files Browse the repository at this point in the history
…r url as parameter
  • Loading branch information
yuche committed Jun 25, 2024
1 parent 9336f97 commit 94e1e60
Show file tree
Hide file tree
Showing 3 changed files with 116 additions and 8 deletions.
5 changes: 5 additions & 0 deletions .changeset/plenty-yaks-think.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@joyid/ckb': patch
---

refactor: verifyCredential now accept credential info and joyid server url as parameter
40 changes: 35 additions & 5 deletions packages/ckb/src/verify.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -89,15 +89,45 @@ 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
const res = await verifyCredential(pubkey, address, keyType as any, alg)
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)
Expand All @@ -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)
Expand All @@ -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)
Expand All @@ -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)
Expand Down
79 changes: 76 additions & 3 deletions packages/ckb/src/verify.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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. <br>
* Testnet: https://api.testnet.joyid.dev/api/v1 <br>
* Mainnet: https://api.joy.id/api/v1
* @returns {Promise<boolean>} A promise that resolves to a boolean indicating
* whether the verification was successful.
*/
export async function verifyCredential(
credential: CredentialInfo,
joyidServerURL: string
): Promise<boolean>

/**
* 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<boolean>} 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<boolean> => {
const serverURL = getConfig().joyidServerURL
): Promise<boolean>

/**
* 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 <br>
* 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<boolean>} 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<boolean> {
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) => {
Expand Down

0 comments on commit 94e1e60

Please sign in to comment.