Skip to content

Commit

Permalink
import key into test agent
Browse files Browse the repository at this point in the history
  • Loading branch information
finn-tbd committed Aug 3, 2023
1 parent 5dd1220 commit 4a6a908
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 7 deletions.
42 changes: 38 additions & 4 deletions packages/web5/tests/test-utils/test-user-agent.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ import type { DidIonCreateOptions } from '@tbd54566975/dids';

import { DidIonApi, DidKeyApi, DidResolver } from '@tbd54566975/dids';
import { Web5UserAgent, ProfileApi, ProfileStore } from '@tbd54566975/web5-user-agent';
import { Dwn, DataStoreLevel, EventLogLevel, MessageStoreLevel } from '@tbd54566975/dwn-sdk-js';
import { KeyManager, KeyManagerOptions, KeyManagerStore, ManagedKeyPair, ManagedKey, KmsKeyStore, KmsPrivateKeyStore, ManagedPrivateKey, LocalKms} from '@tbd54566975/crypto';
import { Dwn, DataStoreLevel, EventLogLevel, MessageStoreLevel, Encoder } from '@tbd54566975/dwn-sdk-js';
import { KeyManager, KeyManagerOptions, KeyManagerStore, ManagedKeyPair, ManagedKey, KmsKeyStore, KmsPrivateKeyStore, ManagedPrivateKey, LocalKms, ImportableKeyPair} from '@tbd54566975/crypto';
import { MemoryStore } from '@tbd54566975/common';

import { AppStorage } from '../../src/app-storage.js';
Expand All @@ -25,6 +25,7 @@ export type TestAgentOptions = {
didIon: DidIonApi;
didKey: DidKeyApi;
signKeyPair: ManagedKeyPair;
keyManager: KeyManager;
}

export type TestProfile = {
Expand All @@ -48,6 +49,8 @@ export class TestAgent {
didIon: DidIonApi;
didKey: DidKeyApi;
signKeyPair: ManagedKeyPair;
keyManager: KeyManager;
kid?: string;

constructor(options: TestAgentOptions) {
this.agent = options.agent;
Expand All @@ -62,6 +65,7 @@ export class TestAgent {
this.didIon = options.didIon;
this.didKey = options.didKey;
this.signKeyPair = options.signKeyPair;
this.keyManager = options.keyManager;
}

async clearStorage(): Promise<void> {
Expand Down Expand Up @@ -151,7 +155,8 @@ export class TestAgent {
didResolver,
didIon : DidIon,
didKey : DidKey,
signKeyPair : keyPair
signKeyPair : keyPair,
keyManager : keyManager,
});
}

Expand All @@ -160,6 +165,7 @@ export class TestAgent {
const DidKey = new DidKeyApi();

const appDidState = await DidKey.create();
console.log('creating did ion with options: ', options.profileDidOptions);
const profileDidState = await DidIon.create(options.profileDidOptions);

const profile = await this.profileApi.createProfile({
Expand All @@ -169,8 +175,36 @@ export class TestAgent {
});

// TODO: Import did auth key into key manager (but will have to convert format from jwk to key material)
const key = profileDidState.keys[0];

console.log('did ion key: ', key);
console.log('did verification methods: ', profileDidState.didDocument?.verificationMethod);

const importableKey: ImportableKeyPair = {
privateKey: {
algorithm : { name: 'ECDSA', namedCurve: 'secp256k1' },
extractable : true,
kms : 'local',
material : Encoder.base64UrlToBytes(key.privateKeyJwk.d),
type : 'private',
usages : ['sign'],
},
publicKey: {
algorithm : { name: 'ECDSA', namedCurve: 'secp256k1' },
extractable : true,
kms : 'local',
material : Encoder.base64UrlToBytes(key.privateKeyJwk.x),
type : 'public',
usages : ['verify'],
}
};
const keypair = await this.keyManager.importKey(importableKey);

this.kid = keypair.privateKey.id;

return { did: profile.did.id };
return {
did: profile.did.id,
};
}

async openStorage() {
Expand Down
2 changes: 1 addition & 1 deletion packages/web5/tests/web5-vc-ssi.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ describe('web5.vc.ssi', () => {

const result = await vcApi.create({
credentialSubject : { firstName: 'alice' },
kid : testAgent.signKeyPair.privateKey.id,
kid : testAgent.kid,
});
console.log('vc api create: ', result);
expect(result.status.code).to.equal(202);
Expand Down
6 changes: 4 additions & 2 deletions packages/web5/tests/web5-vc.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import jwt from 'jsonwebtoken';

let did: string;
let vcApi: VcApi;
let testAgent;
let testAgent: TestAgent;
let testProfileOptions: TestProfileOptions;

describe('web5.vc', () => {
Expand All @@ -36,9 +36,11 @@ describe('web5.vc', () => {
it('valid vc', async () => {
const credentialSubject = {firstName: 'alice'};

const vcCreateRequest: VcCreateRequest = { credentialSubject: credentialSubject, kid: testAgent.signKeyPair.privateKey.id};
const vcCreateRequest: VcCreateRequest = { credentialSubject: credentialSubject, kid: testAgent.kid};
const result = await vcApi.create(vcCreateRequest);

console.log(result);

const resultRecord = await result.record?.data.text();
const decodedVc = jwt.decode(resultRecord, { complete: true });

Expand Down

0 comments on commit 4a6a908

Please sign in to comment.