Skip to content

Commit

Permalink
update(identity-wallet): added error for creation with same seed
Browse files Browse the repository at this point in the history
  • Loading branch information
pragmatos committed Jun 30, 2023
1 parent 682f3ef commit c89b2ad
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 4 deletions.
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

0 comments on commit c89b2ad

Please sign in to comment.