Skip to content

Commit 66c2a72

Browse files
restore auth credential from wallet if existed (#224)
* get auth credential from wallet if existed
1 parent 50bd29c commit 66c2a72

File tree

5 files changed

+52
-14
lines changed

5 files changed

+52
-14
lines changed

src/credentials/credential-wallet.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -192,7 +192,7 @@ export class CredentialWallet implements ICredentialWallet {
192192
});
193193

194194
if (!authBJJCredsOfIssuer.length) {
195-
throw new Error('no auth credentials found');
195+
throw new Error(VerifiableConstants.ERRORS.NO_AUTH_CRED_FOUND);
196196
}
197197

198198
for (let index = 0; index < authBJJCredsOfIssuer.length; index++) {

src/identity/identity-wallet.ts

Lines changed: 31 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -626,6 +626,36 @@ export class IdentityWallet implements IIdentityWallet {
626626
rootOfRoots: ZERO_HASH
627627
};
628628

629+
const identity = await this._storage.identity.getIdentity(did.string());
630+
if (!identity) {
631+
await this._storage.identity.saveIdentity({
632+
did: did.string(),
633+
state: currentState,
634+
isStatePublished: false,
635+
isStateGenesis: true
636+
});
637+
}
638+
639+
// check whether we have auth credential, if not - create a new one
640+
const credentials = await this._credentialWallet.findByQuery({
641+
credentialSubject: {
642+
x: {
643+
$eq: pubKey.p[0].toString()
644+
},
645+
y: {
646+
$eq: pubKey.p[1].toString()
647+
}
648+
},
649+
allowedIssuers: [did.string()]
650+
});
651+
652+
if (credentials.length) {
653+
return {
654+
did,
655+
credential: credentials[0]
656+
};
657+
}
658+
629659
const credential = await this.createAuthBJJCredential(
630660
did,
631661
pubKey,
@@ -658,13 +688,6 @@ export class IdentityWallet implements IIdentityWallet {
658688
onChain: opts.revocationOpts.onChain
659689
});
660690

661-
await this._storage.identity.saveIdentity({
662-
did: did.string(),
663-
state: currentState,
664-
isStatePublished: false,
665-
isStateGenesis: true
666-
});
667-
668691
await this._credentialWallet.save(credential);
669692

670693
return {
@@ -1042,7 +1065,7 @@ export class IdentityWallet implements IIdentityWallet {
10421065
}
10431066
}
10441067

1045-
throw new Error('no auth credentials found');
1068+
throw new Error(VerifiableConstants.ERRORS.NO_AUTH_CRED_FOUND);
10461069
}
10471070

10481071
/** {@inheritDoc IIdentityWallet.revokeCredential} */

src/verifiable/constants.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,8 @@ export const VerifiableConstants = Object.freeze({
44
FiELD_IS_EMPTY: 'fieldPath is empty',
55
CONTEXT_TYPE_IS_EMPTY: 'ctxType is empty',
66
// ErrStateNotFound issuer state is genesis state.
7-
IDENTITY_DOES_NOT_EXIST: 'Identity does not exist'
7+
IDENTITY_DOES_NOT_EXIST: 'Identity does not exist',
8+
NO_AUTH_CRED_FOUND: 'no auth credentials found'
89
},
910
CREDENTIAL_TYPE: {
1011
// VerifiableCredential is a W3C verifiable credential type

tests/handlers/auth.test.ts

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,8 @@ import {
4040
Sec256k1Provider,
4141
StateInfo,
4242
hexToBytes,
43-
NativeProver
43+
NativeProver,
44+
VerifiableConstants
4445
} from '../../src';
4546
import { Token } from '@iden3/js-jwz';
4647
import { Blockchain, DID, DidMethod, NetworkId } from '@iden3/js-iden3-core';
@@ -2223,12 +2224,12 @@ describe('auth', () => {
22232224

22242225
// check that we don't have auth credentials now
22252226
await expect(idWallet.getActualAuthCredential(issuerDID)).to.rejectedWith(
2226-
'no auth credentials found'
2227+
VerifiableConstants.ERRORS.NO_AUTH_CRED_FOUND
22272228
);
22282229

22292230
// check that we can't issue new credential
22302231
await expect(idWallet.issueCredential(issuerDID, claimReq)).to.rejectedWith(
2231-
'no auth credentials found'
2232+
VerifiableConstants.ERRORS.NO_AUTH_CRED_FOUND
22322233
);
22332234

22342235
// this should this work because we haven't revoked user keys
@@ -2258,7 +2259,7 @@ describe('auth', () => {
22582259

22592260
// this should not work because we revoked user keys
22602261
await expect(handleAuthorizationRequest(userDID, authReqBody)).to.rejectedWith(
2261-
'no auth credentials found'
2262+
VerifiableConstants.ERRORS.NO_AUTH_CRED_FOUND
22622263
);
22632264
});
22642265
});

tests/identity/id.test.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ import {
2828
} from '../helpers';
2929
import { expect } from 'chai';
3030
import { Wallet } from 'ethers';
31+
import { getRandomBytes } from '@iden3/js-crypto';
3132

3233
describe('identity', () => {
3334
let credWallet: ICredentialWallet;
@@ -318,4 +319,16 @@ describe('identity', () => {
318319
const afterRevokeProofNRcredential2 = await idWallet.generateNonRevocationMtp(did, credential2);
319320
expect(afterRevokeProofNRcredential2.proof.existence).to.equal(false);
320321
});
322+
323+
it("restore identity (doesn't create a new auth BJJ credential)", async () => {
324+
const seed = getRandomBytes(32);
325+
const { did, credential } = await createIdentity(idWallet, { seed });
326+
327+
// "restore" identity from the same seed
328+
const { did: restoredDid, credential: restoredCredential } = await createIdentity(idWallet, {
329+
seed
330+
});
331+
expect(credential).to.be.deep.eq(restoredCredential);
332+
expect(did.string()).to.be.eq(restoredDid.string());
333+
});
321334
});

0 commit comments

Comments
 (0)