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

update(identity-wallet): added error for creation with same seed #86

Merged
merged 1 commit into from
Jun 30, 2023
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
3 changes: 2 additions & 1 deletion src/identity/identity-wallet.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ import { pushHashesToRHS, TreesModel } from '../credentials/rhs';
import { TreeState } from '../circuits';
import { byteEncoder } from '../utils';
import { Options, Path, getDocumentLoader } from '@iden3/js-jsonld-merklization';
import { sha256js } from 'cross-sha256';

/**
* DID creation options
Expand Down Expand Up @@ -268,7 +269,7 @@ export class IdentityWallet implements IIdentityWallet {
async createIdentity(
opts: IdentityCreationOptions
): Promise<{ did: DID; credential: W3CCredential }> {
const tmpIdentifier = uuid.v4();
const tmpIdentifier = opts.seed ? uuid.v5(new sha256js().update(opts.seed).digest('hex'), uuid.NIL) : uuid.v4();

opts.method = opts.method ?? DidMethod.Iden3;
opts.blockchain = opts.blockchain ?? Blockchain.Polygon;
Expand Down
5 changes: 2 additions & 3 deletions src/storage/indexed-db/merkletree.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ export class MerkleTreeIndexedDBStorage implements IMerkleTreeStorage {
};
const meta = await get(identifier, this._merkleTreeMetaStore);
if (meta) {
return meta;
throw new Error(`Present merkle tree meta information in the store for current identifier ${identifier}`);
}
const treesMeta = createMetaInfo();
await set(identifier, treesMeta, this._merkleTreeMetaStore);
Expand Down Expand Up @@ -128,7 +128,6 @@ export class MerkleTreeIndexedDBStorage implements IMerkleTreeStorage {

const treesMeta = meta.map((m) => ({ ...m, identifier: newIdentifier }));

await set(newIdentifier, treesMeta, this._merkleTreeMetaStore);
await del(oldIdentifier, this._merkleTreeMetaStore);
await set(oldIdentifier, treesMeta, this._merkleTreeMetaStore);
}
}
6 changes: 6 additions & 0 deletions src/storage/local-storage/merkletree.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,10 @@ export class MerkleTreeLocalStorage implements IMerkleTreeStorage {
const meta = localStorage.getItem(MerkleTreeLocalStorage.storageKeyMeta);
if (meta) {
const metaInfo: IdentityMerkleTreeMetaInformation[] = JSON.parse(meta);
const presentMetaForIdentifier = metaInfo.find((m) => m.treeId === `${identifier}+${m.type}`);
if(presentMetaForIdentifier) {
throw new Error(`Present merkle tree meta information in the store for current identifier ${identifier}`);
}
const identityMetaInfo = metaInfo.filter((m) => m.identifier === identifier);
if (identityMetaInfo.length > 0) {
return identityMetaInfo;
Expand All @@ -57,6 +61,8 @@ export class MerkleTreeLocalStorage implements IMerkleTreeStorage {
MerkleTreeLocalStorage.storageKeyMeta,
JSON.stringify([...metaInfo, ...treesMeta])
);

return [...metaInfo, ...treesMeta];
}
const treesMeta = createMetaInfo();
localStorage.setItem(MerkleTreeLocalStorage.storageKeyMeta, JSON.stringify(treesMeta));
Expand Down
3 changes: 3 additions & 0 deletions src/storage/memory/merkletree.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,9 @@ export class InMemoryMerkleTreeStorage implements IMerkleTreeStorage {
if (!identifier) {
identifier = `${uuid.v4()}`;
}
if(this._data[identifier]) {
throw new Error(`Present merkle tree meta information in the store for current identifier ${identifier}`);
}
this._data[identifier] = [];

const treesMeta: IdentityMerkleTreeMetaInformation[] = [];
Expand Down