Public
Edited
Mar 13, 2023
Insert cell
Insert cell
core = import("//esm.sh/@iden3/js-iden3-core@1.0.0-beta.0")
Insert cell
crypt0 = import("//esm.sh/@iden3/js-crypto@1.0.0-beta.0")
Insert cell
mt = import("//esm.sh/@iden3/js-merkletree@1.0.0-beta.0")
Insert cell
uuid = import("//esm.sh/uuid")
Insert cell
bjjPrivKey = new crypt0.PrivateKey(getRandomBytes(32))
Insert cell
bjjPubKey = bjjPrivKey.public()
Insert cell
authClaim = core.Claim.newClaim(
core.SchemaHash.authSchemaHash,
core.ClaimOptions.withIndexDataInts(bjjPubKey.p[0], bjjPubKey.p[1]),
core.ClaimOptions.withRevocationNonce(0n)
)
Insert cell
function describeClaim(claim) {
return {
schemaHash: claim.getSchemaHash().bigInt(),
id: claim.getIdPosition() ? claim.getId() : null,
subject: claim.getSubject() || null,
revocationNonce: claim.getRevocationNonce(),
expirationDate: claim.getExpirationDate(),
version: claim.getVersion(),
merklizedRoot: claim.getMerklized() ? claim.getMerklizedRoot() : null,
flags: {
expiration: claim.getFlagExpiration(),
merklized: !!claim.getMerklized(),
updatable: claim.getFlagUpdatable()
}
}
}
Insert cell
describeClaim(authClaim)
Insert cell
mtTypes = ["claims", "revocations", "roots"]
Insert cell
function createIdentityMerkleTrees(identifier = uuid.v4(), mtDepth = 40) {
const trees = {};
mtTypes.forEach(t => {
const treeId = `${identifier}+${t}`;
const db = new mt.InMemoryDB(mt.str2Bytes(treeId));
const tree = new mt.Merkletree(db, true, mtDepth);
tree.id = treeId;
trees[t] = tree;
});
return trees;
}
Insert cell
function idState(trees) {
return mt.hashElems(mtTypes.map(t => trees[t].root.bigInt()));
}
Insert cell
issuerMTs = {
let trees = createIdentityMerkleTrees("issuer");
await trees.claims.add(authClaim.hIndex(), authClaim.hValue());
return trees;
}
Insert cell
_.mapValues(issuerMTs, mt => mt.root.hex());
Insert cell
issuerState = idState(issuerMTs)
Insert cell
issuerState.bigInt()
Insert cell
authProof = {
const { proof, value } = await issuerMTs.claims.generateProof(authClaim.hIndex(), issuerMTs.claims.root);
return proof;
}
Insert cell
didType = core.buildDIDType(core.DidMethod.Iden3, core.Blockchain.Polygon, core.NetworkId.Mumbai)
Insert cell
issuerId = core.Id.idGenesisFromIdenState(didType, issuerState.bigInt())
Insert cell
issuerId.string()
Insert cell
issuerId.bigInt()
Insert cell
did = core.DID.parseFromId(issuerId)
Insert cell
did.toString()
Insert cell
Insert cell
// Fetch raw JSON-LD schema for https://github.com/iden3/claim-schema-vocab/blob/main/schemas/json-ld/kyc-v4.jsonld
kycSchema =
fetch("https://raw.githubusercontent.com/iden3/claim-schema-vocab/main/schemas/json-ld/kyc-v3.json-ld")
.then(resp => resp.json())
Insert cell
schemaType = "KYCAgeCredential"
Insert cell
kycSchema['@context'][0][schemaType]
Insert cell
// schema hash and claim path key are calculated at https://go.dev/play/p/e9Y4WyGqCzh for v4 (the playground linked in tutorial is for v3)
schemaHash = core.SchemaHash.newSchemaHashFromInt(74977327600848231385663280181476307657n)
Insert cell
schemaHash.marshalText()
Insert cell
claimPathKey = 20376033832371109177683048456014525905119173674985843915445634726167450989630n
Insert cell
revNonce = Math.round(Math.random() * 10000 + 1)
Insert cell
expirationDate = new Date(12345654321000)
Insert cell
Insert cell
getRandomBytes = (n) => crypto.getRandomValues(new Uint8Array(n))
Insert cell

Purpose-built for displays of data

Observable is your go-to platform for exploring data and creating expressive data visualizations. Use reactive JavaScript notebooks for prototyping and a collaborative canvas for visual data exploration and dashboard creation.
Learn more