Skip to content

Commit

Permalink
Merge pull request #26 from peaqnetwork/feature/1208218002083108_deta…
Browse files Browse the repository at this point in the history
…ch-generate

Feature/1208218002083108 detach generate
  • Loading branch information
jpgundrum authored Sep 5, 2024
2 parents 17608b4 + 8ad8bed commit 01a9d6e
Show file tree
Hide file tree
Showing 6 changed files with 70 additions and 93 deletions.
92 changes: 35 additions & 57 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

45 changes: 19 additions & 26 deletions packages/sdk/src/modules/did/did.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import type { DidDocument, ReadDidResponse } from '../../types';
import { CustomDocumentFields, Did } from './index';
import { unsubscribeRuntimeVersion } from '../../utils';
import { CreateDidError, ReadDidError, UpdateDidError, RemoveDidError} from '../../utils/errors';
import { Main as SDK } from '../main';

dotenv.config(); // Load variables from .env file

Expand Down Expand Up @@ -38,6 +39,7 @@ describe('Did', () => {
let user2: KeyringPair;
let did: Did;
let did2: Did;
let sdk: SDK;

beforeAll(async () => {
const provider = new WsProvider(BASE_URL);
Expand All @@ -48,6 +50,7 @@ describe('Did', () => {
user2 = keyring2.addFromUri(SEED2);
did = new Did(api, { pair: user });
did2 = new Did(api, { pair: user2 });
sdk = await SDK.createOfflineInstance();
}, 40000);

afterAll(async () => {
Expand All @@ -60,7 +63,6 @@ describe('Did', () => {
*/
describe('generate()', () => {
it('generate did with an incorrect Substrate address', async () => {
const new_did = 'did-test-1';
const address1 = 'Incorrect Address Format';
const address2 = '5Df42mkztLtkksgQuLy4YV6hmhzdjYvDknoxHv1QBkaY12Pgg'; // address has an additional char at the end (49 chars)
const address3 = '5Df42mkztLtkksgQuLy4YV6hmhzdjYvDknoxHv1QBkaY12P'; // address has an additional char at the end (47 chars)
Expand All @@ -69,57 +71,48 @@ describe('Did', () => {
const address6 = '5Df42mkztLtkksgQuLy4YV6hmhzdjYvDknoxHv1QBkaY12PI'; // address that is proper length but has I included
const address7 = '5Df42mkztLtkksgQuLy4YV6hmhzdjYvDknoxHv1QBkaY12Pl'; // address that is proper length but has I included

await expect(did.generate({address: address1}))
await expect(sdk.did.generate({address: address1}))
.rejects.toThrow(new CreateDidError(address_error));
await expect(did.generate({address: address2}))
await expect(sdk.did.generate({address: address2}))
.rejects.toThrow(new CreateDidError(address_error));
await expect(did.generate({address: address3}))
await expect(sdk.did.generate({address: address3}))
.rejects.toThrow(new CreateDidError(address_error));
await expect(did.generate({address: address4}))
await expect(sdk.did.generate({address: address4}))
.rejects.toThrow(new CreateDidError(address_error));
await expect(did.generate({address: address5}))
await expect(sdk.did.generate({address: address5}))
.rejects.toThrow(new CreateDidError(address_error));
await expect(did.generate({address: address6}))
await expect(sdk.did.generate({address: address6}))
.rejects.toThrow(new CreateDidError(address_error));
await expect(did.generate({address: address7}))
await expect(sdk.did.generate({address: address7}))
.rejects.toThrow(new CreateDidError(address_error));
});

it('generate did with an incorrect Ethereum address', async () => {
const new_did = 'did-test-1';
const address1 = 'Incorrect Address Format';
const address2 = '0x9Eeab1aCcb1A701aEfAB00F3b8a275a39646641CC'; // address has an additional char at the end (43 chars)
const address3 = '0x9Eeab1aCcb1A701aEfAB00F3b8a275a39646641'; // address has an one less char at the end (41 chars)
const address4 = '0x9Eeab1aCcb1A701aEfAB00F3b8a275a39646641Z'; // address is proper length but has invalid hex value of 'Z'

await expect(did.generate({address: address1}))
await expect(sdk.did.generate({address: address1}))
.rejects.toThrow(new CreateDidError(address_error));
await expect(did.generate({address: address2}))
await expect(sdk.did.generate({address: address2}))
.rejects.toThrow(new CreateDidError(address_error));
await expect(did.generate({address: address3}))
await expect(sdk.did.generate({address: address3}))
.rejects.toThrow(new CreateDidError(address_error));
await expect(did.generate({address: address4}))
await expect(sdk.did.generate({address: address4}))
.rejects.toThrow(new CreateDidError(address_error));
});

// throws an error when a seed phrase is not 12 or 24 words long
it('generate did with an incorrect seed', async () => {
const new_did = 'did-test-1';
await expect(did.generate({seed: 'My incorrect seed phrase'}))
.rejects.toThrow(new CreateDidError('SeedError: Invalid seed phrase length: Seed phrase must be either 12 or 24 words long.'));
});

it('generate did', async () => {
const result = await did.generate({});
const result = await sdk.did.generate({address: user.address});
const did_hash = result.value;
// check did hash return value: starts with '0x' and only contains hexadecimal values
expect(did_hash.startsWith('0x')).toBe(true);
expect(/^0x[a-fA-F0-9]+$/.test(did_hash)).toBe(true);
});

it('generate did Substrate address', async () => {
const new_did = 'did-test-1';
const result = await did.generate({address: user.address});
const result = await sdk.did.generate({address: user.address});
const did_hash = result.value;
// check did hash return value: starts with '0x' and only contains hexadecimal values
expect(did_hash.startsWith('0x')).toBe(true);
Expand All @@ -134,7 +127,7 @@ describe('Did', () => {

it('generate did Ethereum address', async () => {
const addressETH = '0x9Eeab1aCcb1A701aEfAB00F3b8a275a39646641C';
const result = await did.generate({address: addressETH, customDocumentFields: {controller: addressETH}});
const result = await sdk.did.generate({address: addressETH, customDocumentFields: {controller: addressETH}});
const did_hash = result.value;
// check did hash return value: starts with '0x' and only contains hexadecimal values
expect(did_hash.startsWith('0x')).toBe(true);
Expand Down Expand Up @@ -166,7 +159,7 @@ describe('Did', () => {
}]
}

const result = await did.generate({customDocumentFields: customFields});
const result = await sdk.did.generate({address: user.address, customDocumentFields: customFields});
const did_hash = result.value;
// check did hash return value: starts with '0x' and only contains hexadecimal values
expect(did_hash.startsWith('0x')).toBe(true);
Expand Down Expand Up @@ -199,7 +192,7 @@ describe('Did', () => {
}]
}

const result = await did.generate({address: addressETH, customDocumentFields: customFields});
const result = await sdk.did.generate({address: addressETH, customDocumentFields: customFields});
const did_hash = result.value;
// check did hash return value: starts with '0x' and only contains hexadecimal values
expect(did_hash.startsWith('0x')).toBe(true);
Expand Down
12 changes: 4 additions & 8 deletions packages/sdk/src/modules/did/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,7 @@ type Service = {
}

interface GenerateDidOptions {
address?: Address;
seed?: string;
address: Address;
customDocumentFields?: CustomDocumentFields;
}

Expand Down Expand Up @@ -133,17 +132,14 @@ export class Did extends Base {
public async generate(options: GenerateDidOptions): Promise<GenerateDidResult> {
try {

const { address = '', seed = '', customDocumentFields } = options;

if (seed !== '') this._checkSeed(seed);
const { address = '', customDocumentFields } = options;
if (address !== '') this._checkAddress(address);

const keyPair = this._metadata?.pair || this._getKeyPair(seed);
const accountAddress = address || keyPair.address;
const accountAddress = address;

const didDocumentHash = this._generateDidDocument({
didAccountAddress: accountAddress,
didControllerAddress: keyPair.address,
didControllerAddress: accountAddress,
customDocumentFields,
});

Expand Down
11 changes: 11 additions & 0 deletions packages/sdk/src/modules/main/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,17 @@ export class Main extends Base {
return sdk;
}

/**
* Creates a new offline instance of the SDK to use the generate function.
*
* @param None
* @returns The created offline instance of the SDK.
*/
public static async createOfflineInstance(): Promise<Main> {
const sdk = new Main({});
return sdk;
}

/**
* Connects the SDK to the network.
*/
Expand Down
1 change: 0 additions & 1 deletion packages/sdk/src/modules/main/main.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,5 @@ describe('Main', () => {
expect(sdk['_api']?.isConnected).toBe(false);
});
});

});

2 changes: 1 addition & 1 deletion packages/sdk/src/types/common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ export interface SDKMetadata {
}

export interface Options {
baseUrl: string;
baseUrl?: string;
seed?: string;
}

Expand Down

0 comments on commit 01a9d6e

Please sign in to comment.