Skip to content

Commit 307d936

Browse files
committed
test: fix tests
1 parent ef7a74b commit 307d936

7 files changed

Lines changed: 209 additions & 230 deletions

File tree

src/messaging/Message.test.ts

Lines changed: 13 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,10 @@
1515
*/
1616

1717
import { Attestation, CType } from '@kiltprotocol/credentials'
18-
import { SupportedContentType, createLightDidDocument, multibaseKeyToDidKey, parse } from '@kiltprotocol/did'
18+
import { createLightDidDocument, multibaseKeyToDidKey, parse } from '@kiltprotocol/did'
1919
import { Claim, Credential } from '@kiltprotocol/legacy-credentials'
2020
import { DidResolver, init } from '@kiltprotocol/sdk-js'
2121
import type {
22-
DereferenceResult,
2322
Did,
2423
DidDocument,
2524
DidUrl,
@@ -38,6 +37,7 @@ import {
3837
createLocalDemoFullDidFromKeypair,
3938
createLocalDemoFullDidFromLightDid,
4039
makeEncryptionKeyTool,
40+
makeMockDereference,
4141
makeSigningKeyTool,
4242
} from '../tests'
4343
import type {
@@ -63,6 +63,8 @@ import { decrypt, encrypt, verifyMessageEnvelope } from './MessageEnvelope'
6363
import { fromBody, verifyRequiredCTypeProperties } from './utils'
6464

6565
describe('Messaging', () => {
66+
let mockDereference: ReturnType<typeof makeMockDereference>
67+
6668
let aliceLightDid: DidDocument
6769
let aliceLightDidWithDetails: DidDocument
6870
let aliceFullDid: DidDocument
@@ -76,30 +78,6 @@ describe('Messaging', () => {
7678
let bobAuthentication: SignerInterface<Signers.DidPalletSupportedAlgorithms, DidUrl>
7779
const bobEncKey = makeEncryptionKeyTool('Bob//enc')
7880

79-
async function mockDereference(keyUri: DidUrl | Did): Promise<DereferenceResult<SupportedContentType>> {
80-
const { did, fragment } = parse(keyUri)
81-
const document = [
82-
aliceLightDidWithDetails,
83-
aliceLightDid,
84-
aliceFullDid,
85-
bobLightDidWithDetails,
86-
bobLightDid,
87-
bobFullDid,
88-
].find(({ id }) => id === did)
89-
if (!document) throw new Error('Cannot resolve mocked DID')
90-
let result
91-
if (!fragment) {
92-
result = document
93-
} else {
94-
result = document.verificationMethod?.find(({ id }) => keyUri.endsWith(id))
95-
}
96-
return {
97-
contentStream: result,
98-
contentMetadata: {},
99-
dereferencingMetadata: {} as any,
100-
}
101-
}
102-
10381
beforeAll(async () => {
10482
await init()
10583
const aliceAuthKey = await makeSigningKeyTool('ed25519')
@@ -138,6 +116,15 @@ describe('Messaging', () => {
138116
algorithms: Signers.DID_PALLET_SUPPORTED_ALGORITHMS,
139117
})
140118
)[0]
119+
120+
mockDereference = makeMockDereference([
121+
aliceLightDidWithDetails,
122+
aliceLightDid,
123+
aliceFullDid,
124+
bobLightDidWithDetails,
125+
bobLightDid,
126+
bobFullDid,
127+
])
141128
})
142129

143130
it('verify message encryption and signing', async () => {

src/messaging/interface/attestation/Attestation.test.ts

Lines changed: 46 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -6,51 +6,47 @@
66
*/
77

88
/* eslint-disable @typescript-eslint/ban-ts-comment */
9-
import {
10-
CType,
11-
Claim,
12-
DidDocument,
13-
ICType,
14-
IClaim,
15-
Credential,
16-
Quote,
17-
IConfirmPaymentContent,
18-
IAttestation,
19-
connect,
20-
KiltKeyringPair,
21-
DecryptCallback,
22-
EncryptCallback,
23-
ConfigService,
24-
ChainHelpers,
25-
Blockchain,
26-
} from '@kiltprotocol/sdk-js'
9+
import { Blockchain } from '@kiltprotocol/chain-helpers'
10+
import { CType } from '@kiltprotocol/credentials'
11+
import { Claim, Credential } from '@kiltprotocol/legacy-credentials'
12+
import { ConfigService, connect } from '@kiltprotocol/sdk-js'
13+
import type { DidDocument, IAttestation, ICType, IClaim, KiltKeyringPair } from '@kiltprotocol/types'
14+
import Keyring from '@polkadot/keyring'
2715
import { BN } from '@polkadot/util'
2816
import { mnemonicGenerate } from '@polkadot/util-crypto'
29-
import Keyring from '@polkadot/keyring'
30-
3117
import {
32-
KeyToolSignCallback,
18+
confirmPayment,
19+
receiveAttestation,
20+
requestAttestation,
21+
requestPayment,
22+
submitAttestation,
23+
submitTerms,
24+
validateConfirmedPayment,
25+
} from '.'
26+
import { createAttesterSignedQuote, verifyAttesterSignedQuote, verifyQuoteAgreement } from '../../../quote'
27+
import {
28+
KeyToolSigners,
3329
createAttestation,
3430
createCtype,
3531
fundAccount,
3632
generateDid,
3733
keypairs,
3834
makeDecryptCallback,
35+
makeDidSigners,
3936
makeEncryptCallback,
40-
makeSignCallback,
4137
startContainer,
4238
} from '../../../tests'
43-
import { receiveSessionRequest, requestSession, verifySession } from '../session'
44-
import { IRequestAttestation, IRequestPayment, ISession, ISessionRequest, ISubmitTerms, ITerms } from '../../../types'
4539
import {
46-
confirmPayment,
47-
receiveAttestation,
48-
requestAttestation,
49-
requestPayment,
50-
submitAttestation,
51-
submitTerms,
52-
validateConfirmedPayment,
53-
} from '.'
40+
DecryptCallback,
41+
EncryptCallback,
42+
IConfirmPaymentContent,
43+
IRequestAttestation,
44+
IRequestPayment,
45+
ISession,
46+
ISessionRequest,
47+
ISubmitTerms,
48+
ITerms,
49+
} from '../../../types'
5450
import {
5551
isIConfirmPayment,
5652
isIRequestPayment,
@@ -59,22 +55,22 @@ import {
5955
isSubmitTerms,
6056
} from '../../../utils'
6157
import { decrypt } from '../../MessageEnvelope'
62-
import { verifyAttesterSignedQuote, verifyQuoteAgreement } from '../../../quote'
58+
import { receiveSessionRequest, requestSession, verifySession } from '../session'
6359

6460
describe('Attestation', () => {
6561
//Alice
6662
let aliceAccount: KiltKeyringPair
6763
let aliceFullDid: DidDocument
68-
let aliceSign: KeyToolSignCallback
69-
let aliceSignAssertion: KeyToolSignCallback
64+
let aliceSign: KeyToolSigners
65+
let aliceSignAssertion: KeyToolSigners
7066
let aliceDecryptCallback: DecryptCallback
7167
let aliceEncryptCallback: EncryptCallback
7268
let aliceMnemonic: string
7369

7470
//Bob
7571
let bobAccount: KiltKeyringPair
7672
let bobFullDid: DidDocument
77-
let bobSign: KeyToolSignCallback
73+
let bobSign: KeyToolSigners
7874
let bobDecryptCallback: DecryptCallback
7975
let bobEncryptCallback: EncryptCallback
8076

@@ -107,8 +103,8 @@ describe('Attestation', () => {
107103
const keyPairsAlice = await keypairs(aliceMnemonic)
108104
aliceEncryptCallback = makeEncryptCallback(keyPairsAlice.keyAgreement)(aliceFullDid)
109105
aliceDecryptCallback = makeDecryptCallback(keyPairsAlice.keyAgreement)
110-
aliceSign = makeSignCallback(keyPairsAlice.authentication)
111-
aliceSignAssertion = makeSignCallback(keyPairsAlice.assertionMethod)
106+
aliceSign = makeDidSigners(keyPairsAlice.authentication)
107+
aliceSignAssertion = makeDidSigners(keyPairsAlice.assertionMethod)
112108

113109
//setup bob
114110
const bobMnemonic = mnemonicGenerate()
@@ -119,7 +115,7 @@ describe('Attestation', () => {
119115
const keyPairsBob = await keypairs(bobMnemonic)
120116
bobEncryptCallback = makeEncryptCallback(keyPairsBob.keyAgreement)(bobFullDid)
121117
bobDecryptCallback = makeDecryptCallback(keyPairsBob.keyAgreement)
122-
bobSign = makeSignCallback(keyPairsBob.authentication)
118+
bobSign = makeDidSigners(keyPairsBob.authentication)
123119

124120
//sessions
125121
sessionRequest = requestSession(aliceFullDid, 'MyApp')
@@ -128,7 +124,7 @@ describe('Attestation', () => {
128124
sessionRequest,
129125
bobEncryptCallback,
130126
bobDecryptCallback,
131-
bobSign(bobFullDid)
127+
await bobSign(bobFullDid)
132128
)
133129
bobSession = session
134130

@@ -137,7 +133,7 @@ describe('Attestation', () => {
137133
sessionResponse,
138134
aliceDecryptCallback,
139135
aliceEncryptCallback,
140-
aliceSign(aliceFullDid)
136+
await aliceSign(aliceFullDid)
141137
)
142138

143139
testCType = CType.fromProperties('testCtype', {
@@ -148,10 +144,10 @@ describe('Attestation', () => {
148144
name: 'Bob',
149145
}
150146

151-
claim = Claim.fromCTypeAndClaimContents(testCType, claimContents, bobFullDid.uri)
147+
claim = Claim.fromCTypeAndClaimContents(testCType, claimContents, bobFullDid.id)
152148

153149
const quoteData = {
154-
attesterDid: aliceFullDid.uri,
150+
attesterDid: aliceFullDid.id,
155151
cTypeHash: claim.cTypeHash,
156152
cost: {
157153
tax: { vat: 3.3 },
@@ -163,7 +159,7 @@ describe('Attestation', () => {
163159
timeframe: new Date(2023, 8, 23).toISOString(),
164160
}
165161
// Quote signed by attester
166-
const quoteAttesterSigned = await Quote.createAttesterSignedQuote(quoteData, aliceSign(aliceFullDid))
162+
const quoteAttesterSigned = await createAttesterSignedQuote(quoteData, aliceSession['authenticationSigner'])
167163

168164
submitTermsContent = {
169165
claim,
@@ -302,7 +298,7 @@ describe('Attestation', () => {
302298

303299
const txTransfer = api.tx.balances.transfer(aliceAccount.address, COST)
304300

305-
const finalizedTx = await ChainHelpers.Blockchain.signAndSubmitTx(txTransfer, bobAccount, {
301+
const finalizedTx = await Blockchain.signAndSubmitTx(txTransfer, bobAccount, {
306302
resolveOn: Blockchain.IS_FINALIZED,
307303
})
308304

@@ -334,7 +330,7 @@ describe('Attestation', () => {
334330
delegationId: null,
335331
claimHash: credential.rootHash,
336332
cTypeHash: claim.cTypeHash,
337-
owner: bobFullDid.uri,
333+
owner: bobFullDid.id,
338334
revoked: false,
339335
}
340336

@@ -363,7 +359,7 @@ describe('Attestation', () => {
363359
delegationId: null,
364360
claimHash: credential.rootHash,
365361
cTypeHash: claim.cTypeHash,
366-
owner: bobFullDid.uri,
362+
owner: bobFullDid.id,
367363
revoked: false,
368364
}
369365

@@ -372,11 +368,11 @@ describe('Attestation', () => {
372368
const requestAttestationMessages = await requestAttestation(requestTerms.encryptedMessage, credential, bobSession)
373369

374370
// Anchor attestation to the blockchain
375-
await createCtype(aliceFullDid.uri, aliceAccount, aliceMnemonic, testCType)
371+
await createCtype(aliceFullDid.id, aliceAccount, aliceMnemonic, testCType)
376372
await createAttestation(
377373
aliceAccount,
378-
aliceFullDid.uri,
379-
aliceSignAssertion(aliceFullDid),
374+
aliceFullDid.id,
375+
await aliceSignAssertion(aliceFullDid),
380376
credential.rootHash,
381377
claim.cTypeHash
382378
)

0 commit comments

Comments
 (0)