Skip to content

Commit

Permalink
Fix unit tests for linked signature suites
Browse files Browse the repository at this point in the history
  • Loading branch information
ntn-x2 committed Oct 23, 2023
1 parent caf9e92 commit 4e6c3df
Show file tree
Hide file tree
Showing 11 changed files with 72 additions and 56 deletions.
4 changes: 2 additions & 2 deletions packages/did/src/Did.signature.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -334,7 +334,7 @@ describe('full DID', () => {
controller: `did:kilt:${keypair.address}`,
id: '#0x12345',
publicKeyMultibase: keypairToMultibaseKey(keypair),
type: 'MultiKey',
type: 'Multikey',
},
],
}
Expand All @@ -343,7 +343,7 @@ describe('full DID', () => {
verificationMethod: {
id: '#0x12345',
controller: signingDid,
type: 'MultiKey',
type: 'Multikey',
publicKeyMultibase: keypairToMultibaseKey(keypair),
},
})
Expand Down
2 changes: 1 addition & 1 deletion packages/did/src/Did.utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -253,7 +253,7 @@ export function didKeyToVerificationMethod(
return {
controller,
id,
type: 'MultiKey',
type: 'Multikey',
publicKeyMultibase: u8aToString(
multibaseEncode('base58btc', Uint8Array.from(multiCodecPublicKey))
) as `z${string}`,
Expand Down
12 changes: 6 additions & 6 deletions packages/did/src/DidDetails/LightDidDetails.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ describe('When creating an instance from the details', () => {
publicKey: authKey.publicKey,
type: 'sr25519',
}),
type: 'MultiKey',
type: 'Multikey',
},
{
controller: `did:kilt:light:00${authKey.address}:z17GNCdxLqMYTMC5pnnDrPZGxLEFcXvDamtGNXeNkfSaFf8cktX6erFJiQy8S3ugL981NNys7Rz8DJiaNPZi98v1oeFVL7PjUGNTz1g3jgZo4VgQri2SYHBifZFX9foHZH4DreZXFN66k5dPrvAtBpFXaiG2WZkkxsnxNWxYpqWPPcxvbTE6pJbXxWKjRUd7rog1h9vjA93QA9jMDxm6BSGJHACFgSPUU3UTLk2kjNwT2bjZVvihVFu1zibxwHjowb7N6UQfieJ7ny9HnaQy64qJvGqh4NNtpwkhwm5DTYUoAeAhjt3a6TWyxmBgbFdZF7`,
Expand All @@ -74,7 +74,7 @@ describe('When creating an instance from the details', () => {
publicKey: encKey.publicKey,
type: 'x25519',
}),
type: 'MultiKey',
type: 'Multikey',
},
],
service: [
Expand Down Expand Up @@ -117,7 +117,7 @@ describe('When creating an instance from the details', () => {
publicKey: authKey.publicKey,
type: 'ed25519',
}),
type: 'MultiKey',
type: 'Multikey',
},
{
controller: `did:kilt:light:01${authKey.address}:z15dZSRuzEPTFnBErPxqJie4CmmQH1gYKSQYxmwW5Qhgz5Sr7EYJA3J65KoC5YbgF3NGoBsTY2v6zwj1uDnZzgXzLy8R72Fhjmp8ujY81y2AJc8uQ6s2pVbAMZ6bnvaZ3GVe8bMjY5MiKFySS27qRi`,
Expand All @@ -126,7 +126,7 @@ describe('When creating an instance from the details', () => {
publicKey: encKey.publicKey,
type: 'x25519',
}),
type: 'MultiKey',
type: 'Multikey',
},
],
})
Expand Down Expand Up @@ -198,7 +198,7 @@ describe('When creating an instance from a URI', () => {
publicKey: authKey.publicKey,
type: 'sr25519',
}),
type: 'MultiKey',
type: 'Multikey',
},
{
controller: `did:kilt:light:00${authKey.address}:z17GNCdxLqMYTMC5pnnDrPZGxLEFcXvDamtGNXeNkfSaFf8cktX6erFJiQy8S3ugL981NNys7Rz8DJiaNPZi98v1oeFVL7PjUGNTz1g3jgZo4VgQri2SYHBifZFX9foHZH4DreZXFN66k5dPrvAtBpFXaiG2WZkkxsnxNWxYpqWPPcxvbTE6pJbXxWKjRUd7rog1h9vjA93QA9jMDxm6BSGJHACFgSPUU3UTLk2kjNwT2bjZVvihVFu1zibxwHjowb7N6UQfieJ7ny9HnaQy64qJvGqh4NNtpwkhwm5DTYUoAeAhjt3a6TWyxmBgbFdZF7`,
Expand All @@ -207,7 +207,7 @@ describe('When creating an instance from a URI', () => {
publicKey: encKey.publicKey,
type: 'x25519',
}),
type: 'MultiKey',
type: 'Multikey',
},
],
service: [
Expand Down
46 changes: 23 additions & 23 deletions packages/did/src/DidResolver/DidResolver.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ function generateAuthenticationVerificationMethod(
return {
id: '#auth',
controller,
type: 'MultiKey',
type: 'Multikey',
publicKeyMultibase: Did.keypairToMultibaseKey({
publicKey: new Uint8Array(32).fill(0),
type: 'ed25519',
Expand All @@ -100,7 +100,7 @@ function generateEncryptionVerificationMethod(
return {
id: '#enc',
controller,
type: 'MultiKey',
type: 'Multikey',
publicKeyMultibase: Did.keypairToMultibaseKey({
publicKey: new Uint8Array(32).fill(1),
type: 'x25519',
Expand All @@ -114,7 +114,7 @@ function generateAssertionVerificationMethod(
return {
id: '#att',
controller,
type: 'MultiKey',
type: 'Multikey',
publicKeyMultibase: Did.keypairToMultibaseKey({
publicKey: new Uint8Array(32).fill(2),
type: 'sr25519',
Expand All @@ -128,7 +128,7 @@ function generateCapabilityDelegationVerificationMethod(
return {
id: '#del',
controller,
type: 'MultiKey',
type: 'Multikey',
publicKeyMultibase: Did.keypairToMultibaseKey({
publicKey: new Uint8Array(33).fill(3),
type: 'ecdsa',
Expand Down Expand Up @@ -294,7 +294,7 @@ describe('When resolving a full DID', () => {
{
controller: fullDidWithAuthenticationKey,
id: '#auth',
type: 'MultiKey',
type: 'Multikey',
publicKeyMultibase: Did.keypairToMultibaseKey({
type: 'ed25519',
publicKey: new Uint8Array(32).fill(0),
Expand Down Expand Up @@ -343,7 +343,7 @@ describe('When resolving a full DID', () => {
{
controller: fullDidWithAllKeys,
id: '#auth',
type: 'MultiKey',
type: 'Multikey',
publicKeyMultibase: Did.keypairToMultibaseKey({
type: 'ed25519',
publicKey: new Uint8Array(32).fill(0),
Expand All @@ -352,7 +352,7 @@ describe('When resolving a full DID', () => {
{
controller: fullDidWithAllKeys,
id: '#enc',
type: 'MultiKey',
type: 'Multikey',
publicKeyMultibase: Did.keypairToMultibaseKey({
type: 'x25519',
publicKey: new Uint8Array(32).fill(1),
Expand All @@ -361,7 +361,7 @@ describe('When resolving a full DID', () => {
{
controller: fullDidWithAllKeys,
id: '#att',
type: 'MultiKey',
type: 'Multikey',
publicKeyMultibase: Did.keypairToMultibaseKey({
type: 'sr25519',
publicKey: new Uint8Array(32).fill(2),
Expand All @@ -370,7 +370,7 @@ describe('When resolving a full DID', () => {
{
controller: fullDidWithAllKeys,
id: '#del',
type: 'MultiKey',
type: 'Multikey',
publicKeyMultibase: Did.keypairToMultibaseKey({
type: 'ecdsa',
publicKey: new Uint8Array(33).fill(3),
Expand Down Expand Up @@ -414,7 +414,7 @@ describe('When resolving a full DID', () => {
{
controller: fullDidWithServiceEndpoints,
id: '#auth',
type: 'MultiKey',
type: 'Multikey',
publicKeyMultibase: Did.keypairToMultibaseKey({
type: 'ed25519',
publicKey: new Uint8Array(32).fill(0),
Expand Down Expand Up @@ -466,7 +466,7 @@ describe('When resolving a full DID', () => {
{
controller: didWithAuthenticationKey,
id: '#auth',
type: 'MultiKey',
type: 'Multikey',
publicKeyMultibase: Did.keypairToMultibaseKey({
type: 'ed25519',
publicKey: new Uint8Array(32).fill(0),
Expand Down Expand Up @@ -541,7 +541,7 @@ describe('When resolving a light DID', () => {
{
controller: lightDidWithAuthenticationKey.id,
id: '#authentication',
type: 'MultiKey',
type: 'Multikey',
publicKeyMultibase: Did.keypairToMultibaseKey({
...authKey,
type: 'sr25519',
Expand Down Expand Up @@ -573,7 +573,7 @@ describe('When resolving a light DID', () => {
{
controller: lightDid.id,
id: '#authentication',
type: 'MultiKey',
type: 'Multikey',
publicKeyMultibase: Did.keypairToMultibaseKey({
...authKey,
type: 'sr25519',
Expand All @@ -582,7 +582,7 @@ describe('When resolving a light DID', () => {
{
controller: lightDid.id,
id: '#encryption',
type: 'MultiKey',
type: 'Multikey',
publicKeyMultibase: Did.keypairToMultibaseKey(encryptionKey),
},
],
Expand Down Expand Up @@ -707,7 +707,7 @@ describe('DID Resolution compliance', () => {
{
id: '#auth',
controller: did,
type: 'MultiKey',
type: 'Multikey',
publicKeyMultibase: Did.keypairToMultibaseKey({
publicKey: new Uint8Array(32).fill(0),
type: 'ed25519',
Expand Down Expand Up @@ -756,7 +756,7 @@ describe('DID Resolution compliance', () => {
{
id: '#auth',
controller: did,
type: 'MultiKey',
type: 'Multikey',
publicKeyMultibase: Did.keypairToMultibaseKey({
publicKey: new Uint8Array(32).fill(0),
type: 'ed25519',
Expand Down Expand Up @@ -785,7 +785,7 @@ describe('DID Resolution compliance', () => {
{
id: '#auth',
controller: did,
type: 'MultiKey',
type: 'Multikey',
publicKeyMultibase: Did.keypairToMultibaseKey({
publicKey: new Uint8Array(32).fill(0),
type: 'ed25519',
Expand Down Expand Up @@ -815,7 +815,7 @@ describe('DID Resolution compliance', () => {
{
id: '#auth',
controller: did,
type: 'MultiKey',
type: 'Multikey',
publicKeyMultibase: Did.keypairToMultibaseKey({
publicKey: new Uint8Array(32).fill(0),
type: 'ed25519',
Expand Down Expand Up @@ -879,7 +879,7 @@ describe('DID Resolution compliance', () => {
{
id: '#auth',
controller: did,
type: 'MultiKey',
type: 'Multikey',
publicKeyMultibase: Did.keypairToMultibaseKey({
publicKey: new Uint8Array(32).fill(0),
type: 'ed25519',
Expand All @@ -904,7 +904,7 @@ describe('DID Resolution compliance', () => {
{
id: '#auth',
controller: did,
type: 'MultiKey',
type: 'Multikey',
publicKeyMultibase: Did.keypairToMultibaseKey({
publicKey: new Uint8Array(32).fill(0),
type: 'ed25519',
Expand All @@ -931,7 +931,7 @@ describe('DID Resolution compliance', () => {
{
id: '#auth',
controller: did,
type: 'MultiKey',
type: 'Multikey',
publicKeyMultibase: Did.keypairToMultibaseKey({
publicKey: new Uint8Array(32).fill(0),
type: 'ed25519',
Expand All @@ -954,7 +954,7 @@ describe('DID Resolution compliance', () => {
id: '#auth',
controller:
'did:kilt:4r1WkS3t8rbCb11H8t3tJvGVCynwDXSUBiuGB6sLRHzCLCjs',
type: 'MultiKey',
type: 'Multikey',
publicKeyMultibase: Did.keypairToMultibaseKey({
publicKey: new Uint8Array(32).fill(0),
type: 'ed25519',
Expand Down Expand Up @@ -1037,7 +1037,7 @@ describe('DID Resolution compliance', () => {
{
id: '#auth',
controller: did,
type: 'MultiKey',
type: 'Multikey',
publicKeyMultibase: Did.keypairToMultibaseKey({
publicKey: new Uint8Array(32).fill(0),
type: 'ed25519',
Expand Down
2 changes: 1 addition & 1 deletion packages/types/src/Did.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ export type VerificationMethod = {
/**
* The type of the verification method. This is fixed for KILT DIDs.
*/
type: 'MultiKey'
type: 'Multikey'
/**
* The controller of the verification method.
*/
Expand Down
4 changes: 1 addition & 3 deletions packages/vc-export/src/documentLoader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -81,9 +81,7 @@ export const kiltContextsLoader: DocumentLoader = async (url) => {

export const kiltDidLoader: DocumentLoader = async (url) => {
const { did } = parse(url as DidUri)
const { dereferencingMetadata, contentStream } = await dereferenceDid(did, {
accept: 'application/did+ld+json',
})
const { dereferencingMetadata, contentStream } = await dereferenceDid(did)
if (isFailedDereferenceMetadata(dereferencingMetadata)) {
throw new Error(dereferencingMetadata.error)
}
Expand Down
5 changes: 3 additions & 2 deletions packages/vc-export/src/suites/KiltAttestationProofV1.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -313,7 +313,8 @@ describe('vc-js', () => {
it('creates and verifies a signed presentation (sr25519)', async () => {
const signer = {
sign: async ({ data }: { data: Uint8Array }) => keypair.sign(data),
id: didDocument.authentication?.[0],
// TODO: This goes against the signer interface of the rest of the SDK, where `id` is supposed to be only the verification method ID. Change this.
id: `${didDocument.id}${didDocument.authentication![0]}`,
}
const signingSuite = new Sr25519Signature2020({ signer })

Expand Down Expand Up @@ -451,7 +452,7 @@ describe('issuance', () => {
signer: async () => ({
verificationMethod: {
controller: attestedVc.issuer,
type: 'MultiKey',
type: 'Multikey',
id: '#test',
publicKeyMultibase: 'zasd',
},
Expand Down
10 changes: 8 additions & 2 deletions packages/vc-export/src/suites/Sr25519Signature2020.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
// @ts-expect-error not a typescript module
import * as vcjs from '@digitalbazaar/vc'

import { base58Encode } from '@polkadot/util-crypto'
import { Types, init, W3C_CREDENTIAL_CONTEXT_URL } from '@kiltprotocol/core'
import * as Did from '@kiltprotocol/did'
import { Crypto } from '@kiltprotocol/utils'
Expand Down Expand Up @@ -90,7 +91,8 @@ beforeAll(async () => {
it('issues and verifies a signed credential', async () => {
const signer = {
sign: async ({ data }: { data: Uint8Array }) => keypair.sign(data),
id: didDocument.assertionMethod![0],
// TODO: This goes against the signer interface of the rest of the SDK, where `id` is supposed to be only the verification method ID. Change this.
id: `${didDocument.id}${didDocument.assertionMethod![0]}`,
}
const attestationSigner = new Sr25519Signature2020({ signer })

Expand Down Expand Up @@ -121,13 +123,17 @@ it('issues and verifies a signed credential', async () => {
const assertionMethod = didDocument.verificationMethod?.find(({ id }) =>
id.includes('assertion')
)
const { publicKey } = Did.multibaseKeyToDidKey(
assertionMethod!.publicKeyMultibase
)
const publicKeyBase58 = base58Encode(publicKey)

result = await vcjs.verifyCredential({
credential: verifiableCredential,
suite: new Sr25519Signature2020({
key: new Sr25519VerificationKey2020({
...assertionMethod,
publicKeyBase58: assertionMethod!.publicKeyMultibase.substring(1),
publicKeyBase58,
}),
}),
documentLoader,
Expand Down
8 changes: 8 additions & 0 deletions packages/vc-export/src/suites/Sr25519Signature2020.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import { base58Decode, base58Encode } from '@polkadot/util-crypto'
import jsigs from 'jsonld-signatures' // cjs module

import { KiltCredentialV1, Types } from '@kiltprotocol/core'
import { KILT_DID_CONTEXT_URL, multibaseKeyToDidKey } from '@kiltprotocol/did'
import { context } from '../context/context.js'
import { Sr25519VerificationKey2020 } from './Sr25519VerificationKey.js'
import { includesContext } from './utils.js'
Expand Down Expand Up @@ -184,6 +185,8 @@ export class Sr25519Signature2020 extends LinkedDataSignature {
let contextUrl
if (verificationMethod.type === 'Sr25519VerificationKey2020') {
contextUrl = SUITE_CONTEXT_URL
} else if (verificationMethod.type === 'Multikey') {
contextUrl = KILT_DID_CONTEXT_URL
} else {
throw new Error(`Unsupported key type "${verificationMethod.type}".`)
}
Expand Down Expand Up @@ -230,11 +233,16 @@ export class Sr25519Signature2020 extends LinkedDataSignature {

const verificationMethod =
typeof document === 'string' ? JSON.parse(document) : document
const { publicKey } = multibaseKeyToDidKey(
verificationMethod.publicKeyMultibase
)
const publicKeyBase58 = base58Encode(publicKey)

await this.assertVerificationMethod({ verificationMethod })
const verificationKey = (
await Sr25519VerificationKey2020.from({
...verificationMethod,
publicKeyBase58,
})
).export({ publicKey: true, includeContext: true })
return verificationKey
Expand Down
Loading

0 comments on commit 4e6c3df

Please sign in to comment.