From c92a0f988f1cbaa8749166be13ed4a7feaaa01a8 Mon Sep 17 00:00:00 2001 From: Raphael Flechtner Date: Mon, 23 Oct 2023 16:27:19 +0200 Subject: [PATCH] chore: remove all uses of SignCallback --- .../credentialsV1/KiltAttestationProofV1.ts | 6 --- packages/did/src/Did.signature.spec.ts | 53 +++++++------------ 2 files changed, 19 insertions(+), 40 deletions(-) diff --git a/packages/core/src/credentialsV1/KiltAttestationProofV1.ts b/packages/core/src/credentialsV1/KiltAttestationProofV1.ts index 903488b65..9e07673eb 100644 --- a/packages/core/src/credentialsV1/KiltAttestationProofV1.ts +++ b/packages/core/src/credentialsV1/KiltAttestationProofV1.ts @@ -48,7 +48,6 @@ import type { ICType, IDelegationNode, KiltAddress, - SignExtrinsicCallback, SignerInterface, } from '@kiltprotocol/types' import * as CType from '../ctype/index.js' @@ -661,11 +660,6 @@ export type AttestationHandler = ( timestamp?: number }> -export interface DidSigner { - did: DidUri - signer: SignExtrinsicCallback -} - export type TxHandler = { account: KiltAddress signAndSubmit?: AttestationHandler diff --git a/packages/did/src/Did.signature.spec.ts b/packages/did/src/Did.signature.spec.ts index c3d3b743d..510e1fe15 100644 --- a/packages/did/src/Did.signature.spec.ts +++ b/packages/did/src/Did.signature.spec.ts @@ -9,14 +9,13 @@ import type { KiltKeyringPair, KeyringPair, DidDocument, - SignCallback, DidUrl, DidSignature, DereferenceResult, SignerInterface, } from '@kiltprotocol/types' -import { Crypto, SDKErrors } from '@kiltprotocol/utils' +import { Crypto, SDKErrors, Signers } from '@kiltprotocol/utils' import { randomAsHex, randomAsU8a } from '@polkadot/util-crypto' import type { NewLightDidVerificationKey } from './DidDetails' @@ -308,8 +307,8 @@ describe('light DID', () => { describe('full DID', () => { let keypair: KiltKeyringPair let did: DidDocument - let sign: SignCallback - beforeAll(() => { + let signer: SignerInterface & { id: DidUrl } + beforeAll(async () => { keypair = Crypto.makeKeypairFromSeed() did = { id: `did:kilt:${keypair.address}`, @@ -323,15 +322,11 @@ describe('full DID', () => { }, ], } - sign = async ({ data, did: signingDid }) => ({ - signature: keypair.sign(data), - verificationMethod: { - id: '#0x12345', - controller: signingDid, - type: 'Multikey', - publicKeyMultibase: keypairToMultibaseKey(keypair), - }, - }) + signer = (await Signers.signerFromKeypair({ + keypair, + keyUri: `${did.id}#0x12345`, + algorithm: 'Ed25519', + })) as SignerInterface & { id: DidUrl } }) beforeEach(() => { @@ -358,16 +353,14 @@ describe('full DID', () => { it('verifies did signature over string', async () => { const SIGNED_STRING = 'signed string' - const { signature, verificationMethod } = await sign({ + const signature = await signer.sign({ data: Crypto.coToUInt8(SIGNED_STRING), - did: did.id, - verificationRelationship: 'authentication', }) await expect( verifyDidSignature({ message: SIGNED_STRING, signature, - signerUrl: `${did.id}${verificationMethod.id}`, + signerUrl: signer.id, expectedVerificationRelationship: 'authentication', }) ).resolves.not.toThrow() @@ -375,16 +368,14 @@ describe('full DID', () => { it('verifies did signature over bytes', async () => { const SIGNED_BYTES = Uint8Array.from([1, 2, 3, 4, 5]) - const { signature, verificationMethod } = await sign({ + const signature = await signer.sign({ data: SIGNED_BYTES, - did: did.id, - verificationRelationship: 'authentication', }) await expect( verifyDidSignature({ message: SIGNED_BYTES, signature, - signerUrl: `${did.id}${verificationMethod.id}`, + signerUrl: signer.id, expectedVerificationRelationship: 'authentication', }) ).resolves.not.toThrow() @@ -397,16 +388,14 @@ describe('full DID', () => { contentStream: { id: did.id }, }) const SIGNED_STRING = 'signed string' - const { signature, verificationMethod } = await sign({ + const signature = await signer.sign({ data: Crypto.coToUInt8(SIGNED_STRING), - did: did.id, - verificationRelationship: 'authentication', }) await expect( verifyDidSignature({ message: SIGNED_STRING, signature, - signerUrl: `${did.id}${verificationMethod.id}`, + signerUrl: signer.id, expectedVerificationRelationship: 'authentication', }) ).rejects.toThrow() @@ -418,16 +407,14 @@ describe('full DID', () => { dereferencingMetadata: { error: 'notFound' }, }) const SIGNED_STRING = 'signed string' - const { signature, verificationMethod } = await sign({ + const signature = await signer.sign({ data: Crypto.coToUInt8(SIGNED_STRING), - did: did.id, - verificationRelationship: 'authentication', }) await expect( verifyDidSignature({ message: SIGNED_STRING, signature, - signerUrl: `${did.id}${verificationMethod.id}`, + signerUrl: signer.id, expectedVerificationRelationship: 'authentication', }) ).rejects.toThrow() @@ -435,10 +422,8 @@ describe('full DID', () => { it('accepts signature of full did for light did if enabled', async () => { const SIGNED_STRING = 'signed string' - const { signature, verificationMethod } = await sign({ + const signature = await signer.sign({ data: Crypto.coToUInt8(SIGNED_STRING), - did: did.id, - verificationRelationship: 'authentication', }) const authKey = did.verificationMethod?.find( @@ -460,7 +445,7 @@ describe('full DID', () => { verifyDidSignature({ message: SIGNED_STRING, signature, - signerUrl: `${did.id}${verificationMethod.id}`, + signerUrl: signer.id, expectedSigner, expectedVerificationRelationship: 'authentication', }) @@ -470,7 +455,7 @@ describe('full DID', () => { verifyDidSignature({ message: SIGNED_STRING, signature, - signerUrl: `${did.id}${verificationMethod.id}`, + signerUrl: signer.id, expectedSigner, allowUpgraded: true, expectedVerificationRelationship: 'authentication',