Skip to content
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

chore: signDDO #1877

Open
wants to merge 10 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
94 changes: 89 additions & 5 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 3 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -56,10 +56,12 @@
"dependencies": {
"@oasisprotocol/sapphire-paratime": "^1.3.2",
"@oceanprotocol/contracts": "^2.2.0",
"axios": "^1.7.7",
"cross-fetch": "^4.0.0",
"crypto-js": "^4.1.1",
"decimal.js": "^10.4.1",
"ethers": "^5.7.2"
"ethers": "^5.7.2",
"jose": "^5.9.6"
},
"devDependencies": {
"@truffle/hdwallet-provider": "^2.0.14",
Expand Down
18 changes: 18 additions & 0 deletions src/@types/IssuerSignature.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
export interface JWT {
kty: string
d: string
crv: string
kid: string
x: string
}

export interface IssuerKey {
type: string
jwk: JWT
}

export interface SignedCredential {
jws: string
header: Record<string, any>
issuer: string
}
87 changes: 87 additions & 0 deletions src/utils/SignDDO.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
import { base64url, importJWK, JWTPayload, SignJWT } from 'jose'
import axios from 'axios'
import { ethers } from 'ethers'
import { IssuerKey, SignedCredential } from '../@types/IssuerSignature'

/**
* Signs a verifiable credential using Walt.id's issuer API.
* @param {any} verifiableCredential - The verifiable credential to sign.
* @param {string} waltIdIssuerApi - URL of the Walt.id issuer API.
* @param {string} issuerDid - DID of the issuer.
* @param {IssuerKey} issuerKey - Issuer's key for signing.
* @returns {Promise<SignedCredential>} - The signed credential's JWS, header, and issuer information.
* @throws {Error} If the signing process fails.
*/
export async function signCredentialWithWaltId(
verifiableCredential: any,
waltIdIssuerApi: string,
issuerDid: string,
issuerKey: IssuerKey
): Promise<SignedCredential> {
try {
const response = await axios.post(waltIdIssuerApi, {
credentialData: verifiableCredential,
issuerDid,
issuerKey,
subjectDid: verifiableCredential.credentialSubject.id
})
const jws = response.data
const header = { alg: issuerKey.jwk.kty }
return { jws, header, issuer: issuerDid }
} catch (error) {
console.error('Error signing credential with WaltId:', error)
throw error
}
}

/**
* Signs a verifiable credential locally using a private key.
* @param {any} verifiableCredential - The verifiable credential to sign.
* @param {string} privateKey - The private key.
* @returns {Promise<SignedCredential>} - The signed credential's JWS, header, and issuer information.
* @throws {Error} If the signing process fails.
*/
export async function signCredential(
verifiableCredential: any,
privateKey: string
): Promise<SignedCredential> {
try {
const wallet = new ethers.Wallet(privateKey)
const privateKeyBuffer = Buffer.from(privateKey.substring(2), 'hex')
const publicKeyHex = wallet._signingKey().publicKey
const publicKeyBuffer = Buffer.from(publicKeyHex.substring(2), 'hex')

// Extract x and y coordinates from the public key buffer
const xBuffer = publicKeyBuffer.slice(1, 33)
const yBuffer = publicKeyBuffer.slice(33, 65)

// Base64url-encode the values
const d = base64url.encode(privateKeyBuffer as any as Uint8Array)
const x = base64url.encode(xBuffer as any as Uint8Array)
const y = base64url.encode(yBuffer as any as Uint8Array)

const privateJwk = {
kty: 'EC',
crv: 'secp256k1',
d,
x,
y,
alg: 'ES256K',
use: 'sig'
}

const key = await importJWK(privateJwk, 'ES256K')

const jws = await new SignJWT(verifiableCredential as unknown as JWTPayload)
.setProtectedHeader({ alg: 'ES256K' })
.setIssuedAt()
.setIssuer(publicKeyHex)
.sign(key)
const header = { alg: 'ES256K' }

return { jws, header, issuer: publicKeyHex }
} catch (error) {
console.error('Error signing credential:', error)
throw error
}
}
1 change: 1 addition & 0 deletions src/utils/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,4 @@ export * from './TokenUtils'
export * from './ProviderErrors'
export * from './OrderUtils'
export * from './Assets'
export * from './SignDDO'
Loading