Skip to content

Commit

Permalink
feat: update polygonid libs (#611)
Browse files Browse the repository at this point in the history
  • Loading branch information
martines3000 authored Apr 5, 2024
1 parent fc1f796 commit c606a35
Show file tree
Hide file tree
Showing 35 changed files with 1,347 additions and 612 deletions.
3 changes: 1 addition & 2 deletions biome.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,7 @@
"patches/**",
"assets/**",
".changeset/**",
".nx/**",
"post-process.js"
".nx/**"
]
},
"organizeImports": {
Expand Down
12 changes: 10 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -77,8 +77,16 @@
"cross-fetch@4.0.0": "patches/cross-fetch@4.0.0.patch",
"@ceramicnetwork/common@2.30.0": "patches/@ceramicnetwork__common@2.30.0.patch",
"@metamask/snaps-sdk@1.2.0": "patches/@metamask__snaps-sdk@1.2.0.patch",
"@changesets/assemble-release-plan@6.0.0": "patches/@changesets__assemble-release-plan@6.0.0.patch"
"@changesets/assemble-release-plan@6.0.0": "patches/@changesets__assemble-release-plan@6.0.0.patch",
"@iden3/js-iden3-core@1.2.1": "patches/@iden3__js-iden3-core@1.2.1.patch",
"@iden3/js-crypto@1.0.3": "patches/@iden3__js-crypto@1.0.3.patch",
"@iden3/js-jwz@1.3.0": "patches/@iden3__js-jwz@1.3.0.patch",
"@iden3/js-merkletree@1.1.2": "patches/@iden3__js-merkletree@1.1.2.patch",
"ffjavascript@0.2.63": "patches/ffjavascript@0.2.63.patch",
"did-jwt@6.11.6": "patches/did-jwt@6.11.6.patch",
"web-worker@1.3.0": "patches/web-worker@1.3.0.patch",
"@iden3/js-jsonld-merklization@1.1.2": "patches/@iden3__js-jsonld-merklization@1.1.2.patch"
},
"allowNonAppliedPatches": true
"allowNonAppliedPatches": false
}
}
5 changes: 1 addition & 4 deletions packages/snap/build.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,7 @@ esbuild.build({
target: 'es2020',
treeShaking: true,
tsconfig: 'tsconfig.build.json',
alias: {
'@0xpolygonid/js-sdk':
'../../node_modules/.pnpm/@0xpolygonid+js-sdk@1.0.3/node_modules/@0xpolygonid/js-sdk/dist/esm/index.js',
},
alias: {},
plugins: [plugin(stdLibBrowser)],
inject: [require.resolve('node-stdlib-browser/helpers/esbuild/shim')],
define: {
Expand Down
8 changes: 4 additions & 4 deletions packages/snap/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@
"test:debug": "cross-env IS_TESTING=true cross-env NODE_NO_WARNINGS=1 vitest --inspect-brk --pool threads --poolOptions.threads.singleThread --poolOptions.threads.isolate false"
},
"dependencies": {
"@0xpolygonid/js-sdk": "1.0.3",
"@0xpolygonid/js-sdk": "1.9.4",
"@blockchain-lab-um/did-provider-key": "1.0.8",
"@blockchain-lab-um/masca-types": "1.3.2",
"@blockchain-lab-um/oidc-client-plugin": "0.3.1",
Expand All @@ -63,9 +63,9 @@
"@blockchain-lab-um/veramo-datamanager": "1.0.2",
"@ceramicnetwork/http-client": "2.27.0",
"@glazed/did-datastore": "0.3.2",
"@iden3/js-iden3-core": "1.0.1",
"@iden3/js-jwz": "1.0.1",
"@iden3/js-merkletree": "1.0.0",
"@iden3/js-iden3-core": "1.2.1",
"@iden3/js-jwz": "1.3.0",
"@iden3/js-merkletree": "1.1.2",
"@metamask/key-tree": "^9.0.0",
"@metamask/providers": "14.0.2",
"@metamask/snaps-sdk": "1.2.0",
Expand Down
26 changes: 8 additions & 18 deletions packages/snap/post-process.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
const fs = require('fs');
const pathUtils = require('path');
const fs = require('node:fs');
const pathUtils = require('node:path');

const { postProcessBundle } = require('@metamask/snaps-utils');

Expand All @@ -12,7 +12,7 @@ let bundleString = fs.readFileSync(bundlePath, 'utf8');
console.log('[Start]: MetaMask Snaps transform');

bundleString = postProcessBundle(bundleString, {
stripComments: true,
stripComments: true,
}).code;

console.log('[End]: MetaMask Snaps transform');
Expand All @@ -23,32 +23,22 @@ console.log('[Start]: Custom transform');
bundleString = 'var self = window;\n'.concat(bundleString);

bundleString = bundleString.replace(
"/** @type {import('cborg').TagDecoder[]} */",
'',
"/** @type {import('cborg').TagDecoder[]} */",
''
);

// [Polygon ID] Fix Worker
bundleString = 'var Worker = {};\n'.concat(bundleString);

// [Polygon ID] Fix promise
bundleString = bundleString.replaceAll(
`new Function("return this;")().Promise`,
'Promise',
);

// [Polygon ID] fix single thread
bundleString = bundleString.replaceAll(`if (singleThread)`, `if (true)`);
bundleString = bundleString.replaceAll('if (singleThread)', 'if (true)');

// [Polygon ID] fix single thread
bundleString = bundleString.replaceAll(
`singleThread: singleThread ? true : false`,
`singleThread: true`,
'singleThread: singleThread ? true : false',
'singleThread: true'
);

// [Polygon ID] Remove fs
bundleString = bundleString.replaceAll('fs2.readFileSync;', 'null;');
bundleString = bundleString.replaceAll('fs3.readFileSync;', 'null;');

console.log('[End]: Custom transform');

fs.writeFileSync(bundlePath, bundleString);
Expand Down
2 changes: 1 addition & 1 deletion packages/snap/snap.manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
"./files/circuits/credentialAtomicQuerySigV2/circuit_final.zkey",
"./files/circuits/credentialAtomicQuerySigV2/verification_key.json"
],
"shasum": "AVMmlkykM0Ej2kFD87CrT7VDaDt6lS2RDNuZBsirsTg="
"shasum": "BOsLk3unGah8h8EbmeUV37papTKym1Tlb+sYI/sK1wg="
},
"initialPermissions": {
"endowment:ethereum-provider": {},
Expand Down
12 changes: 10 additions & 2 deletions packages/snap/src/polygon-id/CircuitStorage.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@ import {
CircuitId,
CircuitStorage,
InMemoryDataSource,
base64ToBytes,
byteEncoder,
} from '@0xpolygonid/js-sdk';
import { base64ToBytes } from '@veramo/utils';

export interface B64File {
b64: string;
Expand All @@ -30,13 +30,15 @@ class CircuitStorageService {
method: 'snap_getFile',
params: {
path: './files/circuits/credentialAtomicQuerySigV2/circuit.wasm',
encoding: 'utf8',
},
});

const sigZKey = await snap.request({
method: 'snap_getFile',
params: {
path: './files/circuits/credentialAtomicQuerySigV2/circuit_final.zkey',
encoding: 'utf8',
},
});

Expand All @@ -52,13 +54,15 @@ class CircuitStorageService {
method: 'snap_getFile',
params: {
path: './files/circuits/authV2/circuit.wasm',
encoding: 'utf8',
},
});

const authZKey = await snap.request({
method: 'snap_getFile',
params: {
path: './files/circuits/authV2/circuit_final.zkey',
encoding: 'utf8',
},
});

Expand All @@ -74,13 +78,15 @@ class CircuitStorageService {
method: 'snap_getFile',
params: {
path: './files/circuits/credentialAtomicQueryMTPV2/circuit.wasm',
encoding: 'utf8',
},
});

const mtpZKey = await snap.request({
method: 'snap_getFile',
params: {
path: './files/circuits/credentialAtomicQueryMTPV2/circuit_final.zkey',
encoding: 'utf8',
},
});

Expand All @@ -98,7 +104,9 @@ class CircuitStorageService {
circuitId: 'credentialAtomicQuerySigV2',
wasm: base64ToBytes(sigWasm),
provingKey: base64ToBytes(sigZKey),
verificationKey: byteEncoder.encode(sigVerificationKey),
verificationKey: byteEncoder.encode(
JSON.stringify(sigVerificationKey)
),
}
);

Expand Down
92 changes: 34 additions & 58 deletions packages/snap/src/polygon-id/Polygon.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ import {
RHSResolver,
VerifiableConstants,
VerificationHandlerFunc,
type W3CCredential,
W3CCredential,
ZKPPacker,
byteEncoder,
hexToBytes,
Expand All @@ -35,7 +35,11 @@ import {
type HandleAuthorizationRequestParams,
type HandleCredentialOfferRequestParams,
} from '@blockchain-lab-um/masca-types';
import { Blockchain, DID, DidMethod, NetworkId } from '@iden3/js-iden3-core';
import {
type Blockchain,
DidMethod,
type NetworkId,
} from '@blockchain-lab-um/masca-types';
import { proving } from '@iden3/js-jwz';
import type { DIDResolutionOptions, DIDResolutionResult } from 'did-resolver';

Expand All @@ -52,6 +56,7 @@ import {
getDefaultEthConnectionConfig,
} from './constants';
import { SnapDataSource, SnapMerkleTreeStorage } from './storage';
import { DID } from '@iden3/js-iden3-core';

interface PolygonServicBaseInstance {
packageMgr: PackageManager;
Expand All @@ -78,25 +83,17 @@ class PolygonService {
static instance: Record<
DidMethod.Iden3 | DidMethod.PolygonId,
Record<
Blockchain.Ethereum | Blockchain.Polygon,
Blockchain.Polygon,
Record<NetworkId.Main | NetworkId.Mumbai, PolygonServicBaseInstance>
>
> = {
polygonid: {
eth: {
main: {} as PolygonServicBaseInstance,
mumbai: {} as PolygonServicBaseInstance,
},
polygon: {
main: {} as PolygonServicBaseInstance,
mumbai: {} as PolygonServicBaseInstance,
},
},
iden3: {
eth: {
main: {} as PolygonServicBaseInstance,
mumbai: {} as PolygonServicBaseInstance,
},
polygon: {
main: {} as PolygonServicBaseInstance,
mumbai: {} as PolygonServicBaseInstance,
Expand All @@ -121,20 +118,13 @@ class PolygonService {
for (const method of METHODS) {
for (const blockchain of BLOCKCHAINS) {
for (const networkId of NETWORKS) {
if (
!(
blockchain === Blockchain.Ethereum &&
networkId === NetworkId.Mumbai
)
) {
PolygonService.instance[method][blockchain][networkId] =
await PolygonService.createBaseInstance({
method,
blockchain,
networkId,
circuitData: authV2CircuitData,
});
}
PolygonService.instance[method][blockchain][networkId] =
await PolygonService.createBaseInstance({
method,
blockchain,
networkId,
circuitData: authV2CircuitData,
});
}
}
}
Expand Down Expand Up @@ -199,7 +189,7 @@ class PolygonService {

static async createBaseInstance(params: {
method: DidMethod.Iden3 | DidMethod.PolygonId;
blockchain: Blockchain.Ethereum | Blockchain.Polygon;
blockchain: Blockchain.Polygon;
networkId: NetworkId.Main | NetworkId.Mumbai;
circuitData: CircuitData;
}) {
Expand Down Expand Up @@ -256,7 +246,7 @@ class PolygonService {
throw new Error('The credential does not belong to the current identity');
}

await credWallet.save(credential);
await credWallet.save(W3CCredential.fromJSON(credential));
}

static async queryCredentials(): Promise<W3CCredential[]> {
Expand All @@ -267,29 +257,22 @@ class PolygonService {
for (const method of METHODS) {
for (const blockchain of BLOCKCHAINS) {
for (const networkId of NETWORKS) {
if (
!(
blockchain === Blockchain.Ethereum &&
networkId === NetworkId.Mumbai
const { credWallet } =
PolygonService.instance[method][blockchain][networkId];
const creds = await credWallet.list();
credentials.push(
...creds.filter(
(cred) =>
!cred.type.includes(
VerifiableConstants.AUTH.AUTH_BJJ_CREDENTIAL_TYPE
)
)
) {
const { credWallet } =
PolygonService.instance[method][blockchain][networkId];
const creds = await credWallet.list();
credentials.push(
...creds.filter(
(cred) =>
!cred.type.includes(
VerifiableConstants.AUTH.AUTH_BJJ_CREDENTIAL_TYPE
)
)
);
}
);
}
}
}

return credentials;
return JSON.parse(JSON.stringify(credentials));
}

static async deleteCredential(id: string) {
Expand All @@ -298,16 +281,9 @@ class PolygonService {
for (const method of METHODS) {
for (const blockchain of BLOCKCHAINS) {
for (const networkId of NETWORKS) {
if (
!(
blockchain === Blockchain.Ethereum &&
networkId === NetworkId.Mumbai
)
) {
const { credWallet } =
PolygonService.instance[method][blockchain][networkId];
await credWallet.remove(id);
}
const { credWallet } =
PolygonService.instance[method][blockchain][networkId];
await credWallet.remove(id);
}
}
}
Expand Down Expand Up @@ -348,7 +324,7 @@ class PolygonService {
}
);

return credentials;
return JSON.parse(JSON.stringify(credentials));
} catch (e) {
throw new Error('Error handling credential offer');
}
Expand Down Expand Up @@ -385,13 +361,14 @@ class PolygonService {
signal: AbortSignal.timeout(15000),
});
} catch (e) {
console.error(e);
throw new Error('Error sending authorization response');
}
}

static async createWallet(params: {
method: DidMethod.Iden3 | DidMethod.PolygonId;
blockchain: Blockchain.Ethereum | Blockchain.Polygon;
blockchain: Blockchain.Polygon;
networkId: NetworkId.Main | NetworkId.Mumbai;
}) {
const { method, blockchain, networkId } = params;
Expand Down Expand Up @@ -519,7 +496,6 @@ class PolygonService {
const jwsPacker = new JWSPacker(kms, { resolve: resolveDIDDocument });

mgr.registerPackers([packer, plainPacker, jwsPacker]);

return mgr;
}
}
Expand Down
Loading

0 comments on commit c606a35

Please sign in to comment.