Skip to content

Commit

Permalink
Merge pull request #59 from ADORSYS-GIS/fix/did-peer-generation
Browse files Browse the repository at this point in the history
Fix/did peer generation
  • Loading branch information
forkimenjeckayang authored Dec 18, 2024
2 parents 422004a + 8621c1a commit d424198
Show file tree
Hide file tree
Showing 25 changed files with 3,085 additions and 3,769 deletions.
4,415 changes: 2,056 additions & 2,359 deletions package-lock.json

Large diffs are not rendered by default.

This file was deleted.

This file was deleted.

This file was deleted.

36 changes: 27 additions & 9 deletions packages/multiple-did-identities/src/did-methods/DidPeerMethod.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,11 @@ import { canonicalize } from 'json-canonicalize';
import { base64UrlEncodeService } from '../utils/base64UrlEncode';
import { concatenateKeyStrings } from '../utils/concatenateKeyStrings';
import { convertServiceToAbbreviatedFormat } from '../utils/convertServiceToAbbreviatedFormat';
import { generateKeyPairs } from '../utils/generateKeyPairs';
import {
generateKeyPairs,
generateKeyPairsED25519,
generateKeyPairsX25519,
} from '../utils/generateKeyPairs';
import {
DIDKeyPairVariants,
DIDMethodName,
Expand Down Expand Up @@ -131,13 +135,14 @@ export class DidPeerMethod implements IDidMethod {

// DID PEER METHOD 2 (did:peer:2)-------RESOLVABLE
public async generateMethod2(): Promise<DIDKeyPairMethod2> {
const keyPairs = await generateKeyPairs(2);
const KeyV = keyPairs[0];
const KeyE = keyPairs[1];
const KeyV = (await generateKeyPairsED25519(1))[0];
const KeyE = (await generateKeyPairsX25519(1))[0];

const ED25519_PUB_CODE = new Uint8Array([0xed, 0x01]);
const X25519_PUB_CODE = new Uint8Array([0xec, 0x01]);

const publicKeyMultibaseV = `z${bs58.encode([...ED25519_PUB_CODE, ...KeyV.rawPublicKey])}`;
const publicKeyMultibaseE = `z${bs58.encode([...ED25519_PUB_CODE, ...KeyE.rawPublicKey])}`;
const publicKeyMultibaseE = `z${bs58.encode([...X25519_PUB_CODE, ...KeyE.rawPublicKey])}`;

const purposepublicKeyMultibaseV = `.${PurposeCode.Verification}${publicKeyMultibaseV}`;
const purposepublicKeyMultibaseE = `.${PurposeCode.Encryption}${publicKeyMultibaseE}`;
Expand Down Expand Up @@ -175,6 +180,10 @@ export class DidPeerMethod implements IDidMethod {
const finalEncodedServices = encodedServices.join('');
const did = `did:peer:2${concatPurposeKeys}${finalEncodedServices}`;

// Update the privateKeyJwk.id with the DID
KeyV.privateKeyJwk.id = `${did}#key-1`;
KeyE.privateKeyJwk.id = `${did}#key-2`;

// Define verification methods
const verificationMethod: VerificationMethod2[] = [
{
Expand All @@ -199,6 +208,8 @@ export class DidPeerMethod implements IDidMethod {
],
id: did,
verificationMethod: verificationMethod,
authentication: ['#key-1'],
keyAgreement: ['#key-2'],
service: service,
};

Expand All @@ -216,13 +227,14 @@ export class DidPeerMethod implements IDidMethod {
public async generateMethod2RoutingKey(
mediatorRoutingKey: string,
): Promise<DIDKeyPairMethod2> {
const keyPairs = await generateKeyPairs(2);
const KeyV = keyPairs[0];
const KeyE = keyPairs[1];
const KeyV = (await generateKeyPairsED25519(1))[0];
const KeyE = (await generateKeyPairsX25519(1))[0];

const ED25519_PUB_CODE = new Uint8Array([0xed, 0x01]);
const X25519_PUB_CODE = new Uint8Array([0xec, 0x01]);

const publicKeyMultibaseV = `z${bs58.encode([...ED25519_PUB_CODE, ...KeyV.rawPublicKey])}`;
const publicKeyMultibaseE = `z${bs58.encode([...ED25519_PUB_CODE, ...KeyE.rawPublicKey])}`;
const publicKeyMultibaseE = `z${bs58.encode([...X25519_PUB_CODE, ...KeyE.rawPublicKey])}`;

const purposepublicKeyMultibaseV = `.${PurposeCode.Verification}${publicKeyMultibaseV}`;
const purposepublicKeyMultibaseE = `.${PurposeCode.Encryption}${publicKeyMultibaseE}`;
Expand Down Expand Up @@ -260,6 +272,10 @@ export class DidPeerMethod implements IDidMethod {
const finalEncodedServices = encodedServices.join('');
const did = `did:peer:2${concatPurposeKeys}${finalEncodedServices}`;

// Update the privateKeyJwk.id with the DID
KeyV.privateKeyJwk.id = `${did}#key-1`;
KeyE.privateKeyJwk.id = `${did}#key-2`;

// Define verification methods
const verificationMethod: VerificationMethod2[] = [
{
Expand All @@ -284,6 +300,8 @@ export class DidPeerMethod implements IDidMethod {
],
id: did,
verificationMethod: verificationMethod,
authentication: [`#key-1`],
keyAgreement: [`#key-2`],
service: service,
};

Expand Down
18 changes: 16 additions & 2 deletions packages/multiple-did-identities/src/did-methods/IDidMethod.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,16 +41,18 @@ export interface GenesisDocument {

export interface DIDKeyPairMethod2 extends Did {
didDocument: DIDDocumentMethod2;
privateKeyV: JWK;
privateKeyV: PrivateKeyJWK;
publicKeyV: JWK;
privateKeyE: JWK;
privateKeyE: PrivateKeyJWK;
publicKeyE: JWK;
}

export interface DIDDocumentMethod2 {
'@context': string[];
id: string;
verificationMethod?: VerificationMethod2[];
authentication: string[];
keyAgreement: string[];
service?: Service[];
}

Expand Down Expand Up @@ -120,3 +122,15 @@ export interface JWKKeys {
x: string;
d?: string;
}

export interface PrivateKeyJWK {
id: string;
type: string;
privateKeyJwk: {
crv: string;
d: string;
kty: string;
x: string;
y?: string;
};
}
Original file line number Diff line number Diff line change
@@ -1,16 +1,17 @@
import { EventEmitter } from 'eventemitter3';
import { DIDIdentityService } from '../lib/DIDIdentityService';
import { DidMethodFactory } from '../did-methods/DidMethodFactory';
import {
ServiceResponse,
ServiceResponseStatus,
} from '@adorsys-gis/status-service';
import { DidEventChannel } from '../utils/DidEventChannel';
import { EventEmitter } from 'eventemitter3';
import {
DidMethodFactory,
DIDMethodName,
PeerGenerationMethod,
} from '../did-methods/DidMethodFactory';
import { DidIdentity, DIDKeyPair } from '../did-methods/IDidMethod';
import { DIDIdentityService } from '../lib/DIDIdentityService';
import { DidEventChannel } from '../utils/DidEventChannel';
import { mockDidKeyPair } from './testFixtures';

describe('DIDIdentityService', () => {
let didIdentityService: DIDIdentityService;
Expand Down Expand Up @@ -54,21 +55,9 @@ describe('DIDIdentityService', () => {
it('should create a DID identity with did:peer:0 and emit the event', async () => {
const method = DIDMethodName.Peer;
const method_type = PeerGenerationMethod.Method0;
const did = 'did:peer:0z1234567890';

const mockDIDPeer0: DIDKeyPair = {
did: 'did:peer:0z1234567890',
privateKey: {
kty: 'OKP',
crv: 'Ed25519',
x: 'mockPublicKey',
d: 'mockPrivateKey',
},
publicKey: {
kty: 'OKP',
crv: 'Ed25519',
x: 'mockPublicKey',
},
};
const mockDIDPeer0 = mockDidKeyPair(did);

jest
.spyOn(DidMethodFactory, 'generateDid')
Expand All @@ -94,20 +83,7 @@ describe('DIDIdentityService', () => {
const method = DIDMethodName.Peer;
const methodType = PeerGenerationMethod.Method0;

const mockDIDPeer0 = {
did,
privateKey: {
kty: 'OKP',
crv: 'Ed25519',
x: 'mockPublicKey',
d: 'mockPrivateKey',
},
publicKey: {
kty: 'OKP',
crv: 'Ed25519',
x: 'mockPublicKey',
},
};
const mockDIDPeer0 = mockDidKeyPair(did);

jest
.spyOn(DidMethodFactory, 'generateDid')
Expand Down Expand Up @@ -140,20 +116,7 @@ describe('DIDIdentityService', () => {
const method = DIDMethodName.Peer;
const method_type = PeerGenerationMethod.Method0;

const mockDIDPeer0 = {
did,
privateKey: {
kty: 'OKP',
crv: 'Ed25519',
x: 'mockPublicKey',
d: 'mockPrivateKey',
},
publicKey: {
kty: 'OKP',
crv: 'Ed25519',
x: 'mockPublicKey',
},
};
const mockDIDPeer0 = mockDidKeyPair(did);

jest
.spyOn(DidMethodFactory, 'generateDid')
Expand Down Expand Up @@ -189,20 +152,7 @@ describe('DIDIdentityService', () => {
const method = DIDMethodName.Peer;
const methodType = PeerGenerationMethod.Method0;

const mockDIDPeer0 = {
did,
privateKey: {
kty: 'OKP',
crv: 'Ed25519',
x: 'mockPublicKey',
d: 'mockPrivateKey',
},
publicKey: {
kty: 'OKP',
crv: 'Ed25519',
x: 'mockPublicKey',
},
};
const mockDIDPeer0 = mockDidKeyPair(did);

jest
.spyOn(DidMethodFactory, 'generateDid')
Expand Down
Loading

0 comments on commit d424198

Please sign in to comment.