Skip to content

Commit

Permalink
add possibility to disable publishing
Browse files Browse the repository at this point in the history
  • Loading branch information
vmidyllic committed Sep 9, 2024
1 parent d26297b commit d07206b
Show file tree
Hide file tree
Showing 2 changed files with 92 additions and 40 deletions.
68 changes: 31 additions & 37 deletions src/identity/identity-wallet.ts
Original file line number Diff line number Diff line change
Expand Up @@ -76,13 +76,19 @@ export type IdentityCreationOptions = {
/**
* Options for creating Auth BJJ credential
* seed - seed to generate BJJ key pair
* revocationOpts -
* revocationOpts
* nonce - explicit revocation nonce to use
* onChain - onchain status related option
* txCallback - defines how the TransactionReceipt is handled
* publishMode - specifies the work of transaction polling type: sync / async / callback
* genesisPublishingDisabled - genesis is publishing by default. Set `true` to prevent genesis publishing
*/
export type AuthBJJCredentialCreationOptions = {
revocationOpts: {
id: string;
type: CredentialStatusType;
nonce?: number;
genesisPublishingDisabled?: boolean;
onChain?: {
txCallback?: (tx: TransactionReceipt) => Promise<void>;
publishMode?: PublishMode;
Expand Down Expand Up @@ -661,10 +667,6 @@ export class IdentityWallet implements IIdentityWallet {
allowedIssuers: [did.string()]
});

if (credentials.length > 1) {
throw new Error('more than 1 auth credential for the same key is located in storage');
}

// if credential exists with the same credential status type we return this credential
if (
credentials.length === 1 &&
Expand All @@ -676,10 +678,10 @@ export class IdentityWallet implements IIdentityWallet {
};
}

// if credential exists, but its credential status type is different from what user passes - we remove old credential
// so we can upgrade credential status of auth credential for old identities
if (credentials.length === 1) {
await this._credentialWallet.remove(credentials[0].id);
// otherwise something is already wrong with storage as it has more than 1 credential in it or credential status type of existing credential is different from what user provides - We should remove everything and create new credential.
// in this way credential status of auth credential can be upgraded
for (let i = 0; i < credentials.length; i++) {
await this._credentialWallet.remove(credentials[i].id);
}

// otherwise we create a new credential
Expand Down Expand Up @@ -710,10 +712,14 @@ export class IdentityWallet implements IIdentityWallet {

credential.proof = [mtpProof];

await this.publishRevocationInfoByCredentialStatusType(did, opts.revocationOpts.type, {
rhsUrl: opts.revocationOpts.id,
onChain: opts.revocationOpts.onChain
});
// only if user specified that genesis state publishing is not needed we won't do this.
if (!opts.revocationOpts.genesisPublishingDisabled) {
await this.publishRevocationInfoByCredentialStatusType(did, opts.revocationOpts.type, {
rhsUrl: opts.revocationOpts.id,
onChain: opts.revocationOpts.onChain
});

}

await this._credentialWallet.save(credential);

Expand Down Expand Up @@ -1263,30 +1269,18 @@ export class IdentityWallet implements IIdentityWallet {
}

let nodes: ProofNode[] = [];
if (opts?.treeModel) {
nodes = await getNodesRepresentation(
opts.revokedNonces,
{
revocationTree: opts.treeModel.revocationTree,
claimsTree: opts.treeModel.claimsTree,
state: opts.treeModel.state,
rootsTree: opts.treeModel.rootsTree
},
opts.treeModel.state
);
} else {
const treeState = await this.getDIDTreeModel(issuerDID);
nodes = await getNodesRepresentation(
opts?.revokedNonces,
{
revocationTree: treeState.revocationTree,
claimsTree: treeState.claimsTree,
state: treeState.state,
rootsTree: treeState.rootsTree
},
treeState.state
);
}

const tree = opts?.treeModel ?? await this.getDIDTreeModel(issuerDID);
nodes = await getNodesRepresentation(
opts?.revokedNonces ?? [],
{
revocationTree: tree.revocationTree,
claimsTree: tree.claimsTree,
state: tree.state,
rootsTree: tree.rootsTree
},
tree.state
);

if (!nodes.length) {
return;
Expand Down
64 changes: 61 additions & 3 deletions tests/identity/id.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@ import {
NativeProver,
Iden3SparseMerkleTreeProof,
BJJSignatureProof2021,
TreeState
TreeState,
IdentityCreationOptions
} from '../../src';
import {
MOCK_STATE_STORAGE,
Expand All @@ -26,15 +27,16 @@ import {
registerKeyProvidersInMemoryKMS,
WALLET_KEY,
createEthereumBasedIdentity,
SEED_ISSUER
SEED_ISSUER,
RHS_CONTRACT_ADDRESS
} from '../helpers';
import { expect } from 'chai';
import { Wallet } from 'ethers';
import { getRandomBytes } from '@iden3/js-crypto';
import { Blockchain, DidMethod, NetworkId } from '@iden3/js-iden3-core';
import { ZERO_HASH } from '@iden3/js-merkletree';

describe('identity', () => {
describe.only('identity', () => {
let credWallet: ICredentialWallet;
let idWallet: IdentityWallet;
let dataStorage: IDataStorage;
Expand Down Expand Up @@ -397,4 +399,60 @@ describe('identity', () => {
expect(credential).to.be.deep.eq(restoredCredential);
expect(did.string()).to.be.eq(restoredDid.string());
});

it.only('replace auth bjj credential', async () => {

const idRequest: IdentityCreationOptions = {
method: DidMethod.Iden3,
blockchain: Blockchain.Polygon,
networkId: NetworkId.Amoy,
seed: SEED_ISSUER,
revocationOpts: {
type: CredentialStatusType.Iden3ReverseSparseMerkleTreeProof,
id: RHS_URL
}
}
const { did, credential } = await idWallet.createIdentity(idRequest);
expect(did.string()).to.equal(expectedDID);


let credentials = await credWallet.findByQuery(
{
credentialSubject: {
x: {
$eq: credential.credentialSubject['x'],
},
y: {
$eq: credential.credentialSubject['y'],
}
}
}
)
expect(credentials.length).to.be.equal(1);

idRequest.revocationOpts.type = CredentialStatusType.Iden3OnchainSparseMerkleTreeProof2023;
idRequest.revocationOpts.id = RHS_CONTRACT_ADDRESS;
idRequest.revocationOpts.genesisPublishingDisabled = true;


const { did: did2, credential: credential2 } = await idWallet.createIdentity(idRequest);
expect(did2.string()).to.equal(expectedDID);
expect(credential2.credentialStatus.type).to.be.equal(CredentialStatusType.Iden3OnchainSparseMerkleTreeProof2023);
expect(credential2.credentialStatus.id).to.contain('state')

credentials = await credWallet.findByQuery(
{
credentialSubject: {
x: {
$eq: credential2.credentialSubject['x'],
},
y: {
$eq: credential2.credentialSubject['y'],
}
}
}
)
expect(credentials.length).to.be.equal(1);

});
});

0 comments on commit d07206b

Please sign in to comment.