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

refactor!: make verifyDidSignature accept Uint8Array #666

Merged
merged 6 commits into from
Oct 19, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
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
9 changes: 5 additions & 4 deletions packages/core/src/credential/Credential.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ import {
isDidSignature,
verifyDidSignature,
resolveKey,
signatureToJson,
signatureFromJson,
} from '@kiltprotocol/did'
import type {
DidResolveKey,
Expand All @@ -34,7 +36,6 @@ import type {
SignCallback,
} from '@kiltprotocol/types'
import { Crypto, DataUtils, SDKErrors } from '@kiltprotocol/utils'
import { u8aToHex } from '@polkadot/util'
import * as Claim from '../claim/index.js'
import { hashClaimContents } from '../claim/index.js'
import { verifyClaimAgainstSchema } from '../ctype/index.js'
Expand Down Expand Up @@ -233,7 +234,7 @@ export async function verifySignature(
)
const signingData = makeSigningData(input, claimerSignature.challenge)
await verifyDidSignature({
signature: claimerSignature,
...signatureFromJson(claimerSignature),
message: signingData,
expectedVerificationMethod: 'authentication',
didResolveKey,
Expand Down Expand Up @@ -415,14 +416,14 @@ export async function createPresentation({
excludedClaimProperties
)

const { signature, keyUri } = await signCallback({
const signature = await signCallback({
data: makeSigningData(presentation, challenge),
did: credential.claim.owner,
keyRelationship: 'authentication',
})

return {
...presentation,
claimerSignature: { signature: u8aToHex(signature), keyUri, challenge },
claimerSignature: { ...signatureToJson(signature), challenge },
}
}
20 changes: 9 additions & 11 deletions packages/core/src/quote/Quote.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,6 @@
* @group unit/quote
*/

import { u8aToHex } from '@polkadot/util'

import type {
DidDocument,
IClaim,
Expand All @@ -27,7 +25,6 @@ import { Crypto } from '@kiltprotocol/utils'
import * as Did from '@kiltprotocol/did'
import {
createLocalDemoFullDidFromKeypair,
makeDidSignature,
makeSigningKeyTool,
} from '@kiltprotocol/testing'
import * as CType from '../ctype'
Expand Down Expand Up @@ -142,10 +139,13 @@ describe('Quote', () => {

it('tests created quote data against given data', async () => {
expect(validQuoteData.attesterDid).toEqual(attesterIdentity.uri)
const signature = await makeDidSignature(
Crypto.hashStr(Crypto.encodeObjectAsStr(validAttesterSignedQuote)),
claimerIdentity.uri,
claimer.getSignCallback(claimerIdentity)
const sign = claimer.getSignCallback(claimerIdentity)
const signature = Did.signatureToJson(
await sign({
data: Crypto.hash(Crypto.encodeObjectAsStr(validAttesterSignedQuote)),
did: claimerIdentity.uri,
keyRelationship: 'authentication',
})
)
expect(signature).toEqual(quoteBothAgreed.claimerSignature)

Expand All @@ -166,10 +166,8 @@ describe('Quote', () => {
})
),
validAttesterSignedQuote.attesterSignature.signature,
u8aToHex(
Did.getKey(attesterIdentity, attesterKeyId!)?.publicKey ||
new Uint8Array()
)
Did.getKey(attesterIdentity, attesterKeyId!)?.publicKey ||
new Uint8Array()
)
).not.toThrow()
await Quote.verifyAttesterSignedQuote(validAttesterSignedQuote, {
Expand Down
23 changes: 11 additions & 12 deletions packages/core/src/quote/Quote.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,12 @@ import type {
DidUri,
} from '@kiltprotocol/types'
import { Crypto, JsonSchema, SDKErrors } from '@kiltprotocol/utils'
import { resolveKey, verifyDidSignature } from '@kiltprotocol/did'
import {
resolveKey,
verifyDidSignature,
signatureToJson,
signatureFromJson,
} from '@kiltprotocol/did'
import { QuoteSchema } from './QuoteSchema.js'

/**
Expand Down Expand Up @@ -79,10 +84,7 @@ export async function createAttesterSignedQuote(
})
return {
...quoteInput,
attesterSignature: {
keyUri: signature.keyUri,
signature: Crypto.u8aToHex(signature.signature),
},
attesterSignature: signatureToJson(signature),
}
}

Expand All @@ -103,7 +105,7 @@ export async function verifyAttesterSignedQuote(
): Promise<void> {
const { attesterSignature, ...basicQuote } = quote
await verifyDidSignature({
signature: attesterSignature,
...signatureFromJson(attesterSignature),
message: Crypto.hashStr(Crypto.encodeObjectAsStr(basicQuote)),
expectedVerificationMethod: 'authentication',
didResolveKey,
Expand Down Expand Up @@ -140,13 +142,13 @@ export async function createQuoteAgreement(
const { attesterSignature, ...basicQuote } = attesterSignedQuote

await verifyDidSignature({
signature: attesterSignature,
...signatureFromJson(attesterSignature),
message: Crypto.hashStr(Crypto.encodeObjectAsStr(basicQuote)),
expectedVerificationMethod: 'authentication',
didResolveKey,
})

const { signature, keyUri } = await sign({
const signature = await sign({
data: Crypto.hash(Crypto.encodeObjectAsStr(attesterSignedQuote)),
did: claimerDid,
keyRelationship: 'authentication',
Expand All @@ -155,10 +157,7 @@ export async function createQuoteAgreement(
return {
...attesterSignedQuote,
rootHash: credentialRootHash,
claimerSignature: {
signature: Crypto.u8aToHex(signature),
keyUri,
},
claimerSignature: signatureToJson(signature),
}
}

Expand Down
Loading