Skip to content

Commit 1e72293

Browse files
W3CCredential.fromJSON deep copy
1 parent 8f42ab4 commit 1e72293

File tree

4 files changed

+72
-4
lines changed

4 files changed

+72
-4
lines changed

package-lock.json

+2-2
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@0xpolygonid/js-sdk",
3-
"version": "1.17.7",
3+
"version": "1.17.8",
44
"description": "SDK to work with Polygon ID",
55
"main": "dist/node/cjs/index.js",
66
"module": "dist/node/esm/index.js",

src/verifiable/credential.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ export class W3CCredential {
9696

9797
static fromJSON(obj: any): W3CCredential {
9898
const w = new W3CCredential();
99-
Object.assign(w, obj);
99+
Object.assign(w, structuredClone(obj));
100100
w.proof = Array.isArray(w.proof)
101101
? w.proof.map(W3CCredential.proofFromJSON)
102102
: W3CCredential.proofFromJSON(w.proof);

tests/credentials/credential-validation.test.ts

+68
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ import {
1717
IdentityStorage,
1818
InMemoryMerkleTreeStorage,
1919
IStateStorage,
20+
JsonDocumentObject,
2021
Profile,
2122
RootInfo,
2223
StateInfo,
@@ -489,4 +490,71 @@ describe('Verify credential proof', () => {
489490
const isValid = await credential.verifyProof(ProofType.Iden3SparseMerkleTreeProof, resolverURL);
490491
expect(isValid).to.be.eq(true);
491492
});
493+
494+
it('W3CCredential.fromJSON deep copy', async () => {
495+
const credObject = {
496+
id: 'urn:uuid:0f2a2d08-65e5-11ef-bc4e-0a58a9feac02',
497+
'@context': [
498+
'https://www.w3.org/2018/credentials/v1',
499+
'https://schema.iden3.io/core/jsonld/iden3proofs.jsonld',
500+
'ipfs://QmfK8fsSz5UWm44p5eN9KjsTzi1YEsDCG1LHMFcW9T2Dbe'
501+
],
502+
type: ['VerifiableCredential', 'HowManyAttribute'],
503+
issuanceDate: '2024-08-29T08:59:54.82008584Z',
504+
credentialSubject: {
505+
id: 'did:polygonid:privado:test:3LD3n5edw91PGdNh329rqRydBdaQj2Zwrqf7u49gU2',
506+
type: 'HowManyAttribute',
507+
'att-1-string': 'string',
508+
'att-2-int': 22,
509+
'att-3-bool': false,
510+
'att-4-obj': {
511+
'att-bool': true,
512+
'att-int': 14,
513+
'att-string': 'string2'
514+
}
515+
},
516+
credentialStatus: {
517+
id: 'https://issuer-node-api-v2-test.privado.id/v1/agent',
518+
revocationNonce: 683471303,
519+
type: 'Iden3commRevocationStatusV1.0'
520+
},
521+
issuer: 'did:polygonid:polygon:amoy:2qUPgJsiQP8sdQuckFEj73bDRU84NYnmzwQJr5M1Fd',
522+
credentialSchema: {
523+
id: 'https://ipfs.io/ipfs/QmQvWV4BJqV16czvr2Cpc8qe5mGG51sii4YuDyMPjSxYp7',
524+
type: 'JsonSchema2023'
525+
},
526+
proof: [
527+
{
528+
type: 'BJJSignature2021',
529+
issuerData: {
530+
id: 'did:polygonid:polygon:amoy:2qUPgJsiQP8sdQuckFEj73bDRU84NYnmzwQJr5M1Fd',
531+
state: {
532+
claimsTreeRoot: '8a68b09bc81d49cea0e0a0f56990a0b3cb1bb55d963d309fb357356d7897140d',
533+
value: '1c44078aa45ec70bebbb98f525edf3177ccb516e12beffd84fbdbaf86ad2bc13'
534+
},
535+
authCoreClaim:
536+
'cca3371a6cb1b715004407e325bd993c000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000548961cf73cbd6b488027734ea8dc2dd913b15f2dccf2ce13c7bb1fad409cb1a0504589c8c855e1455cf136d2fd27a4c1cded8915633f8e36a06843ef753b21e0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000',
537+
mtp: {
538+
existence: true,
539+
siblings: []
540+
},
541+
credentialStatus: {
542+
id: 'https://issuer-node-api-v2-test.privado.id/v1/agent',
543+
revocationNonce: 0,
544+
type: 'Iden3commRevocationStatusV1.0',
545+
statusIssuer: null
546+
}
547+
},
548+
coreClaim:
549+
'31df932e35b3beef628544cf7d28dbcb2200000000000000000000000000000002a2c7b26f04aff4b573354055c1d44e3a35021a75ec5e941c10780719aa0b008489bda654ede194c8fbd7e82417f48813157f04fb917f3a4413be6209d338290000000000000000000000000000000000000000000000000000000000000000c7f1bc2800000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000',
550+
signature:
551+
'4732c4e4350b84087cd43f463bbf4103b1a3561659ae6fb144ba0e78657281233d393fbef1490ba7492c56f3f89fe7e08e47ddcffef20d02f4a40bddfde06702'
552+
}
553+
]
554+
};
555+
const cred = W3CCredential.fromJSON(credObject);
556+
delete (credObject.credentialSubject as JsonDocumentObject)['att-4-obj'];
557+
expect(cred.credentialSubject['att-4-obj']).not.to.be.eq(undefined);
558+
expect(cred.credentialSubject['att-4-obj']['att-bool']).to.be.eq(true);
559+
});
492560
});

0 commit comments

Comments
 (0)