Sync up with go-inde3-core

This commit is contained in:
Kolezhniuk
2022-12-16 14:20:28 +01:00
parent cf9b4227c4
commit a8b4705204
3 changed files with 88 additions and 71 deletions

4
package-lock.json generated
View File

@@ -1,12 +1,12 @@
{
"name": "@iden3/js-iden3-core",
"version": "0.0.2",
"version": "0.0.1",
"lockfileVersion": 2,
"requires": true,
"packages": {
"": {
"name": "@iden3/js-iden3-core",
"version": "0.0.2",
"version": "0.0.1",
"license": "MIT",
"dependencies": {
"@iden3/js-crypto": "git@github.com:iden3/js-crypto.git",

View File

@@ -17,10 +17,13 @@ export enum NetworkId {
}
export enum DidMethod {
Iden3 = 'iden3'
Iden3 = 'iden3',
PolygonId = 'polygonid'
}
export const DIDMethodByte: { [key: string]: number } = {
[DidMethod.Iden3]: 0b00000001
[DidMethod.Iden3]: 0b00000001,
[DidMethod.PolygonId]: 0b00000010
};
// DIDNetworkFlag is a structure to represent DID blockchain and network id
@@ -50,6 +53,11 @@ export const DIDMethodNetwork: {
'polygon:mumbai': 0b00010000 | 0b00000010,
'ethereum:main': 0b00100000 | 0b00000001,
'ethereum:goerli': 0b00100000 | 0b00000010
},
[DidMethod.PolygonId]: {
'_:_': 0b00000000,
'polygon:main': 0b00010000 | 0b00000001,
'polygon:mumbai': 0b00010000 | 0b00000010
}
};
@@ -71,14 +79,18 @@ export function buildDIDType(
const sb: number | undefined = methodFn[new DIDNetworkFlag(blockchain, network).toString()];
if (typeof sb !== 'number') {
throw new Error(`blockchain ${blockchain} and network ${network} is not defined in core lib`);
throw new Error(
`blockchain ${blockchain.toString() ?? '-'} and network ${
network.toString() ?? '-'
} is not defined in core lib`
);
}
return Uint8Array.from([fb, sb]);
}
// FindNetworkIDForDIDMethodByValue finds network by byte value
export function findNetworkIDForDIDMethodByValue(method: string, byteNumber: number): NetworkId {
export function findNetworkIDForDIDMethodByValue(method: DidMethod, byteNumber: number): NetworkId {
const methodMap = DIDMethodNetwork[method];
if (!methodMap) {
throw new Error(`did method ${method} is not defined in core lib`);
@@ -118,18 +130,6 @@ export function findDIDMethodByValue(byteNumber: number): DidMethod {
throw new Error(`bytes ${byteNumber} are not defined in core lib as valid did method`);
}
export type DIDOption = (did: DID) => void;
export class DIDOptions {
// WithNetwork sets Blockchain and NetworkID (eth:main)
static withNetwork(blockchain: Blockchain, network: NetworkId): DIDOption {
return (did: DID) => {
did.networkId = network;
did.blockchain = blockchain;
};
}
}
// DID Decentralized Identifiers (DIDs)
// https://w3c.github.io/did-core/#did-syntax
export class DID {
@@ -138,18 +138,25 @@ export class DID {
public blockchain: Blockchain = Blockchain.NoChain;
public networkId: NetworkId = NetworkId.NoNetwork;
// DIDGenesisFromIdenState calculates the genesis ID from an Identity State and returns it as DID
static fromGenesisFromIdenState(typ: Uint8Array, state: bigint): DID {
const id = Id.idGenesisFromIdenState(typ, state);
return DID.parseFromId(id);
}
// toString did as a string
toString(): string {
if (this.blockchain == '') {
return [Constants.DID.DID_SCHEMA, DidMethod.Iden3, this.id.string()].join(':');
return [Constants.DID.DID_SCHEMA, this.method, this.id.string()].join(':');
}
return [
Constants.DID.DID_SCHEMA,
DidMethod.Iden3,
this.method,
this.blockchain,
this.networkId,
this.id.string()
]
.filter((i) => !!i)
.join(':');
}
@@ -167,6 +174,10 @@ export class DID {
// ParseDID method parse string and extract DID if string is valid Iden3 identifier
static parse(s: string): DID {
const args = s.split(':');
if (!args) {
throw new Error('did string is not valid');
}
const did = new DID();
did.method = args[1] as DidMethod;
@@ -226,11 +237,4 @@ export class DID {
}
return did;
}
static newDID(didStr: string, ...args: DIDOption[]): DID {
const did = new DID();
did.id = Id.fromString(didStr);
args.filter((opt) => !!opt).forEach((arg) => arg(did));
return did;
}
}

View File

@@ -1,49 +1,19 @@
import {
DID,
NetworkId,
DIDMethodByte,
DidMethod,
DIDOptions,
Blockchain,
buildDIDType
} from './../src/did';
import { DID, NetworkId, DIDMethodByte, DidMethod, Blockchain, buildDIDType } from './../src/did';
const helperBuildDIDFromType = (
method: DidMethod,
blockchain: Blockchain,
network: NetworkId
): DID => {
const typ = buildDIDType(method, blockchain, network);
const genesisState = BigInt(1);
const did = DID.fromGenesisFromIdenState(typ, genesisState);
return did;
};
describe('DID tests', () => {
const tests: {
did: string;
description: string;
options: (did: DID) => void;
identifier: string;
}[] = [
{
description: 'Test readonly did',
identifier: 'tN4jDinQUdMuJJo6GbVeKPNTPCJ7txyXTWU4T2tJa',
did: 'did:iden3:tN4jDinQUdMuJJo6GbVeKPNTPCJ7txyXTWU4T2tJa',
options: () => {
return null;
}
},
{
description: 'Test eth did',
identifier: 'zyaYCrj27j7gJfrBboMW49HFRSkQznyy12ABSVzTy',
did: 'did:iden3:eth:main:zyaYCrj27j7gJfrBboMW49HFRSkQznyy12ABSVzTy',
options: DIDOptions.withNetwork(Blockchain.Ethereum, NetworkId.Main)
},
{
description: 'Test polygon did',
identifier: 'wyFiV4w71QgWPn6bYLsZoysFay66gKtVa9kfu6yMZ',
did: 'did:iden3:polygon:mumbai:wyFiV4w71QgWPn6bYLsZoysFay66gKtVa9kfu6yMZ',
options: DIDOptions.withNetwork(Blockchain.Polygon, NetworkId.Mumbai)
}
];
tests.forEach((test) => {
it(test.description, () => {
const got = DID.newDID(test.identifier, test.options);
expect(got.toString()).toEqual(test.did);
});
});
it('parse DID', () => {
let didStr = 'did:iden3:polygon:mumbai:wyFiV4w71QgWPn6bYLsZoysFay66gKtVa9kfu6yMZ';
let did = DID.parse(didStr);
@@ -61,4 +31,47 @@ describe('DID tests', () => {
expect('').toEqual(did.blockchain);
expect([DIDMethodByte[DidMethod.Iden3], 0b0]).toMatchObject(did.id.type());
});
it('TestDIDGenesisFromState', () => {
const typ0 = buildDIDType(DidMethod.Iden3, Blockchain.NoChain, NetworkId.NoNetwork);
const genesisState = BigInt(1);
const did = DID.fromGenesisFromIdenState(typ0, genesisState);
expect(DidMethod.Iden3).toEqual(did.method);
expect(Blockchain.NoChain).toEqual(did.blockchain);
expect(NetworkId.NoNetwork).toEqual(did.networkId);
expect('did:iden3:tJ93RwaVfE1PEMxd5rpZZuPtLCwbEaDCrNBhAy8HM').toEqual(did.toString());
});
it('TestDID_PolygonID_Types', () => {
// Polygon no chain, no network
const did = helperBuildDIDFromType(
DidMethod.PolygonId,
Blockchain.NoChain,
NetworkId.NoNetwork
);
expect(DidMethod.PolygonId).toEqual(did.method);
expect(Blockchain.NoChain).toEqual(did.blockchain);
expect(NetworkId.NoNetwork).toEqual(did.networkId);
expect('did:polygonid:2mbH5rt9zKT1mTivFAie88onmfQtBU9RQhjNPLwFZh').toEqual(did.toString());
// Polygon | Polygon chain, Main
const did2 = helperBuildDIDFromType(DidMethod.PolygonId, Blockchain.Polygon, NetworkId.Main);
expect(DidMethod.PolygonId).toEqual(did2.method);
expect(Blockchain.Polygon).toEqual(did2.blockchain);
expect(NetworkId.Main).toEqual(did2.networkId);
expect('did:polygonid:polygon:main:2pzr1wiBm3Qhtq137NNPPDFvdk5xwRsjDFnMxpnYHm').toEqual(
did2.toString()
);
// Polygon | Polygon chain, Mumbai
const did3 = helperBuildDIDFromType(DidMethod.PolygonId, Blockchain.Polygon, NetworkId.Mumbai);
expect(DidMethod.PolygonId).toEqual(did3.method);
expect(Blockchain.Polygon).toEqual(did3.blockchain);
expect(NetworkId.Mumbai).toEqual(did3.networkId);
expect('did:polygonid:polygon:mumbai:2qCU58EJgrELNZCDkSU23dQHZsBgAFWLNpNezo1g6b').toEqual(
did3.toString()
);
});
});