Merkilized flag refactoring

This commit is contained in:
Kolezhniuk
2022-11-08 21:54:07 +01:00
parent 3cb40b4e97
commit 9b9b567368
7 changed files with 294 additions and 203 deletions

View File

@@ -1,5 +1,5 @@
{
"cSpell.words": ["Iden", "Merklize"],
"cSpell.words": ["Iden", "Merklize", "Merklized"],
"workbench.colorCustomizations": {
"activityBar.activeBackground": "#ab307e",
"activityBar.background": "#ab307e",

View File

@@ -29,7 +29,7 @@ Index:
101: B.v Object Value
[1] Expiration: bool
[1] Updatable: bool
[3] Merklized:
[3] Merklized: data is merklized root is stored in the:
000: none
001: C.i Root Index (root located in i_2)
010: C.v Root Value (root located in v_2)
@@ -50,108 +50,6 @@ Value:
v_3: [ 253 bits] 0
*/
// Option provides the ability to set different Claim's fields on construction
export type Option = (c: Claim) => void;
// WithFlagUpdatable sets claim's flag `updatable`
export function withFlagUpdatable(val: boolean): Option {
return (c: Claim) => c.setFlagUpdatable(val);
}
// WithVersion sets claim's version
export function withVersion(ver: number): Option {
return (c: Claim) => c.setVersion(ver);
}
// WithIndexId sets Id to claim's index
export function withIndexId(id: Id): Option {
return (c: Claim) => c.setIndexId(id);
}
// WithValueId sets Id to claim's value
export function withValueId(id: Id): Option {
return (c: Claim) => c.setValueId(id);
}
// WithFlagMerklize sets claim's flag `merklize`
export function withFlagMerklize(p: MerklizePosition): Option {
return (c: Claim) => c.setFlagMerklize(p);
}
// WithId sets Id to claim's index or value depending on `pos`.
export function withId(id: Id, pos: IdPosition): Option {
return (c: Claim) => {
switch (pos) {
case IdPosition.Index:
c.setIndexId(id);
break;
case IdPosition.Value:
c.setValueId(id);
break;
default:
throw new Error(Constants.ERRORS.INCORRECT_ID_POSITION);
}
};
}
// WithRevocationNonce sets claim's revocation nonce.
export function withRevocationNonce(nonce: number): Option {
return (c: Claim) => c.setRevocationNonce(nonce);
}
// WithExpirationDate sets claim's expiration date to `dt`.
export function withExpirationDate(dt: Date): Option {
return (c: Claim) => c.setExpirationDate(dt);
}
// WithIndexData sets data to index slots A & B.
// Returns ErrSlotOverflow if slotA or slotB value are too big.
export function withIndexData(slotA: ElemBytes, slotB: ElemBytes): Option {
return (c: Claim) => c.setIndexData(slotA, slotB);
}
// WithIndexDataBytes sets data to index slots A & B.
// Returns ErrSlotOverflow if slotA or slotB value are too big.
export function withIndexDataBytes(slotA: Uint8Array | null, slotB: Uint8Array | null): Option {
return (c: Claim) => c.setIndexDataBytes(slotA, slotB);
}
// WithIndexDataInts sets data to index slots A & B.
// Returns ErrSlotOverflow if slotA or slotB value are too big.
export function withIndexDataInts(slotA: bigint | null, slotB: bigint | null): Option {
return (c: Claim) => c.setIndexDataInts(slotA, slotB);
}
// WithValueData sets data to value slots A & B.
// Returns ErrSlotOverflow if slotA or slotB value are too big.
export function withValueData(slotA: ElemBytes, slotB: ElemBytes): Option {
return (c: Claim) => c.setValueData(slotA, slotB);
}
// WithValueDataBytes sets data to value slots A & B.
// Returns ErrSlotOverflow if slotA or slotB value are too big.
export function withValueDataBytes(slotA: Uint8Array, slotB: Uint8Array): Option {
return (c: Claim) => c.setValueDataBytes(slotA, slotB);
}
// WithValueDataInts sets data to value slots A & B.
// Returns ErrSlotOverflow if slotA or slotB value are too big.
export function withValueDataInts(slotA: bigint | null, slotB: bigint | null): Option {
return (c: Claim) => c.setValueDataInts(slotA, slotB);
}
// NewClaim creates new Claim with specified SchemaHash and any number of
// options. Using options you can specify any field in claim.
export function newClaim(sh: SchemaHash, ...args: Option[]): Claim {
const c = new Claim();
c.setSchemaHash(sh);
for (let i = 0; i < args.length; i++) {
const fn = args[i];
fn(c);
}
return c;
}
export enum SlotName {
IndexA = 'IndexA',
IndexB = 'IndexB',
@@ -186,19 +84,19 @@ export enum IdPosition {
Value = 2
}
// merklizeFlag for the time being describes the location of root (in index or value
// merklizedFlag for the time being describes the location of root (in index or value
// slots or nowhere at all).
//
// Values merklizeFlagIndex indicates that root is located in index[2] slots.
// Values merklizeFlagValue indicates that root is located in value[2] slots.
export enum MerklizeFlag {
// Values merklizedFlagIndex indicates that root is located in index[2] slots.
// Values merklizedFlagValue indicates that root is located in value[2] slots.
export enum MerklizedFlag {
None = 0b00000000,
Index = 0b00100000,
Value = 0b01000000,
Invalid = 0b10000000
}
export enum MerklizePosition {
export enum MerklizedPosition {
None = 0,
Index = 1,
Value = 2
@@ -221,6 +119,18 @@ export class Claim {
}
}
// NewClaim creates new Claim with specified SchemaHash and any number of
// options. Using options you can specify any field in claim.
static newClaim(sh: SchemaHash, ...args: ClaimOption[]): Claim {
const c = new Claim();
c.setSchemaHash(sh);
for (let i = 0; i < args.length; i++) {
const fn = args[i];
fn(c);
}
return c;
}
// GetSchemaHash return copy of claim's schema hash.
getSchemaHash(): SchemaHash {
return new SchemaHash(this._index[0].bytes.slice(0, Constants.SCHEMA.HASH_LENGTH));
@@ -331,45 +241,45 @@ export class Claim {
}
}
setFlagMerklize(s: MerklizePosition): void {
setFlagMerklized(s: MerklizedPosition): void {
let f: number;
switch (s) {
case MerklizePosition.Index:
f = MerklizeFlag.Index;
case MerklizedPosition.Index:
f = MerklizedFlag.Index;
break;
case MerklizePosition.Value:
f = MerklizeFlag.Value;
case MerklizedPosition.Value:
f = MerklizedFlag.Value;
break;
default:
f = MerklizeFlag.None;
f = MerklizedFlag.None;
}
// clean last 3 bits
this.index[0].bytes[Flags.ByteIdx] &= 0b00011111;
this.index[0].bytes[Flags.ByteIdx] |= f;
}
private getMerklize(): MerklizeFlag {
private getMerklized(): MerklizedFlag {
let mt = this.index[0].bytes[Flags.ByteIdx];
// clean all except last 3 bits
mt &= 0b11100000;
return mt as MerklizeFlag;
return mt as MerklizedFlag;
}
// GetMerklizePosition returns the position at which the Merklize flag is stored.
getMerklizePosition(): MerklizePosition {
switch (this.getMerklize()) {
case MerklizeFlag.None:
return MerklizePosition.None;
case MerklizeFlag.Index:
return MerklizePosition.Index;
case MerklizeFlag.Value:
return MerklizePosition.Value;
// GetMerklizedPosition returns the position at which the Merklized flag is stored.
getMerklizedPosition(): MerklizedPosition {
switch (this.getMerklized()) {
case MerklizedFlag.None:
return MerklizedPosition.None;
case MerklizedFlag.Index:
return MerklizedPosition.Index;
case MerklizedFlag.Value:
return MerklizedPosition.Value;
default:
throw new Error(Constants.ERRORS.INCORRECT_MERKLIZE_POSITION);
throw new Error(Constants.ERRORS.INCORRECT_MERKLIZED_POSITION);
}
}
private setSlotInt(slot: ElemBytes, value: bigint | null, slotName: SlotName) {
public setSlotInt(slot: ElemBytes, value: bigint | null, slotName: SlotName) {
if (!value) {
value = BigInt(0);
}
@@ -586,3 +496,131 @@ export class Claim {
}
}
}
// Option provides the ability to set different Claim's fields on construction
export type ClaimOption = (c: Claim) => void;
export class ClaimOptions {
// WithFlagUpdatable sets claim's flag `updatable`
static withFlagUpdatable(val: boolean): ClaimOption {
return (c: Claim) => c.setFlagUpdatable(val);
}
// WithVersion sets claim's version
static withVersion(ver: number): ClaimOption {
return (c: Claim) => c.setVersion(ver);
}
// WithIndexId sets Id to claim's index
static withIndexId(id: Id): ClaimOption {
return (c: Claim) => c.setIndexId(id);
}
// WithValueId sets Id to claim's value
static withValueId(id: Id): ClaimOption {
return (c: Claim) => c.setValueId(id);
}
// WithFlagMerklized sets claim's flag `merklized`
static withFlagMerklized(p: MerklizedPosition): ClaimOption {
return (c: Claim) => c.setFlagMerklized(p);
}
// WithId sets Id to claim's index or value depending on `pos`.
static withId(id: Id, pos: IdPosition): ClaimOption {
return (c: Claim) => {
switch (pos) {
case IdPosition.Index:
c.setIndexId(id);
break;
case IdPosition.Value:
c.setValueId(id);
break;
default:
throw new Error(Constants.ERRORS.INCORRECT_ID_POSITION);
}
};
}
// WithRevocationNonce sets claim's revocation nonce.
static withRevocationNonce(nonce: number): ClaimOption {
return (c: Claim) => c.setRevocationNonce(nonce);
}
// WithExpirationDate sets claim's expiration date to `dt`.
static withExpirationDate(dt: Date): ClaimOption {
return (c: Claim) => c.setExpirationDate(dt);
}
// WithIndexData sets data to index slots A & B.
// Returns ErrSlotOverflow if slotA or slotB value are too big.
static withIndexData(slotA: ElemBytes, slotB: ElemBytes): ClaimOption {
return (c: Claim) => c.setIndexData(slotA, slotB);
}
// WithIndexDataBytes sets data to index slots A & B.
// Returns ErrSlotOverflow if slotA or slotB value are too big.
static withIndexDataBytes(slotA: Uint8Array | null, slotB: Uint8Array | null): ClaimOption {
return (c: Claim) => c.setIndexDataBytes(slotA, slotB);
}
// WithIndexDataInts sets data to index slots A & B.
// Returns ErrSlotOverflow if slotA or slotB value are too big.
static withIndexDataInts(slotA: bigint | null, slotB: bigint | null): ClaimOption {
return (c: Claim) => c.setIndexDataInts(slotA, slotB);
}
// WithValueData sets data to value slots A & B.
// Returns ErrSlotOverflow if slotA or slotB value are too big.
static withValueData(slotA: ElemBytes, slotB: ElemBytes): ClaimOption {
return (c: Claim) => c.setValueData(slotA, slotB);
}
// WithValueDataBytes sets data to value slots A & B.
// Returns ErrSlotOverflow if slotA or slotB value are too big.
static withValueDataBytes(slotA: Uint8Array, slotB: Uint8Array): ClaimOption {
return (c: Claim) => c.setValueDataBytes(slotA, slotB);
}
// WithValueDataInts sets data to value slots A & B.
// Returns ErrSlotOverflow if slotA or slotB value are too big.
static withValueDataInts(slotA: bigint | null, slotB: bigint | null): ClaimOption {
return (c: Claim) => c.setValueDataInts(slotA, slotB);
}
// WithIndexMerklizedRoot sets root to index i_2
// Returns ErrSlotOverflow if root value are too big.
static withIndexMerklizedRoot(r: bigint): ClaimOption {
return (c: Claim) => {
c.setFlagMerklized(MerklizedPosition.Index);
c.setSlotInt(c.index[2], r, SlotName.IndexA);
};
}
// WithValueMerklizedRoot sets root to value v_2
// Returns ErrSlotOverflow if root value are too big.
static withValueMerklizedRoot(r: bigint): ClaimOption {
return (c: Claim) => {
c.setFlagMerklized(MerklizedPosition.Value);
c.setSlotInt(c.value[2], r, SlotName.ValueA);
};
}
// WithMerklizedRoot sets root to value v_2 or index i_2
// Returns ErrSlotOverflow if root value are too big.
static withMerklizedRoot(r: bigint, pos: MerklizedPosition): ClaimOption {
return (c: Claim) => {
switch (pos) {
case MerklizedPosition.Index:
c.setFlagMerklized(MerklizedPosition.Index);
c.setSlotInt(c.index[2], r, SlotName.IndexA);
break;
case MerklizedPosition.Value:
c.setFlagMerklized(MerklizedPosition.Value);
c.setSlotInt(c.value[2], r, SlotName.ValueA);
break;
default:
throw new Error(Constants.ERRORS.INCORRECT_MERKLIZED_POSITION);
}
};
}
}

View File

@@ -13,7 +13,7 @@ export const Constants = Object.freeze({
INVALID_SUBJECT_POSITION: 'invalid subject position',
// ErrIncorrectMerklizePosition means that passed position is not one of predefined:
// MerklizePositionIndex or MerklizePositionValue
INCORRECT_MERKLIZE_POSITION: 'incorrect Merklize position'
INCORRECT_MERKLIZED_POSITION: 'incorrect Merklize position'
},
SCHEMA: {
HASH_LENGTH: 16

View File

@@ -115,13 +115,16 @@ export function findDIDMethodByValue(byteNumber: number): DidMethod {
throw new Error(`bytes ${byteNumber} are not defined in core lib as valid did method`);
}
export type DIDOptions = (did: DID) => void;
// WithNetwork sets Blockchain and NetworkID (eth:main)
export function withNetwork(blockchain: Blockchain, network: NetworkId): DIDOptions {
return (did: DID) => {
did.networkId = network;
did.blockchain = blockchain;
};
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)
@@ -221,7 +224,7 @@ export class DID {
return did;
}
static newDID(didStr: string, ...args: DIDOptions[]): 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));

View File

@@ -1,24 +1,12 @@
import {
Flags,
IdPosition,
MerklizeFlag,
MerklizePosition,
SubjectFlag,
withFlagMerklize
} from './../src/claim';
import { BytesHelper, ElemBytes } from './../src/elemBytes';
import {
Claim,
newClaim,
withExpirationDate,
withFlagUpdatable,
withIndexData,
withIndexDataBytes,
withIndexDataInts,
withRevocationNonce,
withValueData,
withValueDataInts,
withVersion
ClaimOptions,
Flags,
IdPosition,
MerklizedFlag,
MerklizedPosition,
SubjectFlag
} from '../src/claim';
import { SchemaHash } from '../src/schemaHash';
import { poseidonHash } from '../src/utils';
@@ -27,7 +15,7 @@ import { Constants } from '../src/constants';
import { Id } from '../src/id';
describe('claim test', () => {
it('new claim', () => {
const claim = newClaim(new SchemaHash(), withFlagUpdatable(true));
const claim = Claim.newClaim(new SchemaHash(), ClaimOptions.withFlagUpdatable(true));
expect(claim.value.length).toEqual(4);
for (let i = 1; i < 4; i++) {
expect(claim.index[i].bytes.every((b) => b === 0)).toBeTruthy();
@@ -45,7 +33,7 @@ describe('claim test', () => {
});
it('raw slots', async () => {
const claim = newClaim(new SchemaHash(), withFlagUpdatable(true));
const claim = Claim.newClaim(new SchemaHash(), ClaimOptions.withFlagUpdatable(true));
const { index, value } = claim.rawSlots();
const indexHash = await poseidonHash([
index[0].toBigInt(),
@@ -74,7 +62,7 @@ describe('claim test', () => {
Uint8Array.from(Array.from({ length: 16 }, () => Math.floor(Math.random() * 16)))
);
expect(sc.bytes.length).toEqual(16);
const claim = newClaim(sc);
const claim = Claim.newClaim(sc);
expect(sc.bytes).toEqual(claim.index[0].bytes.slice(0, sc.bytes.length));
const shFromClaim = claim.getSchemaHash();
const shFromClaimHexBytes = shFromClaim.marshalText();
@@ -83,7 +71,7 @@ describe('claim test', () => {
it('getFlagUpdatable', async () => {
const sc = new SchemaHash();
let claim = newClaim(sc);
let claim = Claim.newClaim(sc);
expect(claim.getFlagUpdatable()).toBeFalsy();
claim.setFlagUpdatable(true);
@@ -92,17 +80,17 @@ describe('claim test', () => {
claim.setFlagUpdatable(false);
expect(claim.getFlagUpdatable()).toBeFalsy();
claim = newClaim(sc, withFlagUpdatable(true));
claim = Claim.newClaim(sc, ClaimOptions.withFlagUpdatable(true));
expect(claim.getFlagUpdatable()).toBeTruthy();
claim = newClaim(sc, withFlagUpdatable(false));
claim = Claim.newClaim(sc, ClaimOptions.withFlagUpdatable(false));
expect(claim.getFlagUpdatable()).toBeFalsy();
});
it('getVersion', async () => {
const sc = new SchemaHash();
const maxUint32 = Math.pow(2, 32) - 1;
const claim = newClaim(sc, withVersion(maxUint32));
const claim = Claim.newClaim(sc, ClaimOptions.withVersion(maxUint32));
expect(maxUint32).toEqual(claim.getVersion());
const randomInt = Math.floor(Math.random() * maxUint32);
claim.setVersion(randomInt);
@@ -112,7 +100,7 @@ describe('claim test', () => {
it('getRevocationNonce', () => {
const sc = new SchemaHash();
const nonce = Math.floor(Math.random() * Number.MAX_SAFE_INTEGER);
const claim = newClaim(sc, withRevocationNonce(nonce));
const claim = Claim.newClaim(sc, ClaimOptions.withRevocationNonce(nonce));
expect(nonce).toEqual(claim.getRevocationNonce());
const nonce2 = Math.floor(Math.random() * Number.MAX_SAFE_INTEGER);
claim.setRevocationNonce(nonce2);
@@ -123,7 +111,7 @@ describe('claim test', () => {
const sh = new SchemaHash();
const expDate = new Date();
expDate.setSeconds(0, 0);
const c1 = newClaim(sh, withExpirationDate(expDate));
const c1 = Claim.newClaim(sh, ClaimOptions.withExpirationDate(expDate));
let expDate2 = c1.getExpirationDate();
expect(expDate2).not.toBeNull();
@@ -159,7 +147,11 @@ describe('claim test', () => {
const iySlot = new ElemBytes().setBigInt(iY);
const vxSlot = new ElemBytes().setBigInt(vX);
const vySlot = new ElemBytes().setBigInt(vY);
newClaim(new SchemaHash(), withIndexData(ixSlot, iySlot), withValueData(vxSlot, vySlot));
Claim.newClaim(
new SchemaHash(),
ClaimOptions.withIndexData(ixSlot, iySlot),
ClaimOptions.withValueData(vxSlot, vySlot)
);
expect(ixSlot.toBigInt().toString()).toEqual(iX.toString());
expect(iySlot.toBigInt().toString()).toEqual(iY.toString());
expect(vxSlot.toBigInt().toString()).toEqual(vX.toString());
@@ -190,10 +182,10 @@ describe('claim test', () => {
expSlot.setBigInt(BigInt(0));
const value = BigInt(64);
const claim = newClaim(new SchemaHash(), withIndexDataInts(value, null));
const claim = Claim.newClaim(new SchemaHash(), ClaimOptions.withIndexDataInts(value, null));
expect(expSlot.bytes).toEqual(claim.index[3].bytes);
const claim2 = newClaim(new SchemaHash(), withIndexDataInts(null, value));
const claim2 = Claim.newClaim(new SchemaHash(), ClaimOptions.withIndexDataInts(null, value));
expect(expSlot.bytes).toEqual(claim2.index[2].bytes);
});
@@ -201,26 +193,29 @@ describe('claim test', () => {
const expSlot = new ElemBytes();
expSlot.setBigInt(BigInt(0));
const value = BigInt(64);
const claim = newClaim(new SchemaHash(), withValueDataInts(value, null));
const claim = Claim.newClaim(new SchemaHash(), ClaimOptions.withValueDataInts(value, null));
expect(expSlot.bytes).toEqual(claim.value[3].bytes);
const claim2 = newClaim(new SchemaHash(), withValueDataInts(null, value));
const claim2 = Claim.newClaim(new SchemaHash(), ClaimOptions.withValueDataInts(null, value));
expect(expSlot.bytes).toEqual(claim2.value[2].bytes);
});
it('with index data bytes', () => {
it('ClaimOptions.with index data bytes', () => {
const iX = BigInt(
'124482786795178117845085577341029868555797443843373384650556214865383112817'
);
const expSlot = new ElemBytes();
expSlot.setBigInt(BigInt(0));
const claim = newClaim(new SchemaHash(), withIndexDataBytes(BytesHelper.intToBytes(iX), null));
const claim = Claim.newClaim(
new SchemaHash(),
ClaimOptions.withIndexDataBytes(BytesHelper.intToBytes(iX), null)
);
expect(expSlot.bytes).toEqual(claim.index[3].bytes);
});
describe('serialization', () => {
it('with strings', () => {
it('ClaimOptions.with strings', () => {
const input = `[
"15163995036539824738096525342132337704181738148399168403057770094395141110111",
"3206594817839378626027676511482956481343861686313501795018892230311002175077",
@@ -268,7 +263,7 @@ describe('claim test', () => {
expect(JSON.parse(input)).toEqual(JSON.parse(result));
});
it('with binary', () => {
it('ClaimOptions.with binary', () => {
const binDataStr = [
'5fb90badb37c5821b6d95526a41a9504680b4e7c8b763a1b1d49d4955c848621',
'65f606f6a63b7f3dfd2567c18979e4d60f26686d9bf2fb26c901ff354cde1607',
@@ -320,18 +315,18 @@ describe('claim test', () => {
});
});
describe('work with id', () => {
describe('work ClaimOptions.with id', () => {
it('id position', () => {
const tests = [
{
name: 'self claim',
claim: () => newClaim(new SchemaHash()),
claim: () => Claim.newClaim(new SchemaHash()),
expectedPosition: IdPosition.None
},
{
name: 'subject stored in index',
claim: () => {
const claim = newClaim(new SchemaHash());
const claim = Claim.newClaim(new SchemaHash());
const genesis32bytes = BytesHelper.hashBytes('genesistest');
const genesis = genesis32bytes.slice(0, 27);
claim.setIndexId(new Id(Constants.ID.TYPE_DEFAULT, genesis));
@@ -342,7 +337,7 @@ describe('claim test', () => {
{
name: 'subject stored in value',
claim: () => {
const claim = newClaim(new SchemaHash());
const claim = Claim.newClaim(new SchemaHash());
const genesis32bytes = BytesHelper.hashBytes('genesistest');
const genesis = genesis32bytes.slice(0, 27);
claim.setValueId(new Id(Constants.ID.TYPE_DEFAULT, genesis));
@@ -364,7 +359,7 @@ describe('claim test', () => {
{
name: 'invalid position',
claim: () => {
const c = newClaim(new SchemaHash());
const c = Claim.newClaim(new SchemaHash());
c.setSubject(SubjectFlag.Invalid);
return c;
},
@@ -383,59 +378,110 @@ describe('claim test', () => {
describe('merklization', () => {
const tests = [
{
name: 'not merklized',
claim: () => newClaim(new SchemaHash()),
expectedPosition: MerklizePosition.None
name: 'not merklizedd',
claim: () => Claim.newClaim(new SchemaHash()),
expectedPosition: MerklizedPosition.None
},
{
name: 'mt root stored in index',
claim: () => {
const claim = newClaim(new SchemaHash());
claim.setFlagMerklize(MerklizePosition.Index);
const claim = Claim.newClaim(new SchemaHash());
claim.setFlagMerklized(MerklizedPosition.Index);
return claim;
},
expectedPosition: MerklizePosition.Index
expectedPosition: MerklizedPosition.Index
},
{
name: 'mt root stored in value',
claim: () => {
const claim = newClaim(new SchemaHash());
claim.setFlagMerklize(MerklizePosition.Value);
const claim = Claim.newClaim(new SchemaHash());
claim.setFlagMerklized(MerklizedPosition.Value);
return claim;
},
expectedPosition: MerklizePosition.Value
expectedPosition: MerklizedPosition.Value
},
{
name: 'mt root random bits',
claim: () => {
const c = newClaim(new SchemaHash());
c.setFlagMerklize(MerklizePosition.Value);
const c = Claim.newClaim(new SchemaHash());
c.setFlagMerklized(MerklizedPosition.Value);
return c;
},
expectedPosition: MerklizePosition.Value
expectedPosition: MerklizedPosition.Value
}
];
tests.forEach((test) => {
it(test.name, () => {
const c = test.claim();
const position = c.getMerklizePosition();
const position = c.getMerklizedPosition();
expect(position).toEqual(test.expectedPosition);
});
});
it('Error Case', () => {
const c = newClaim(new SchemaHash());
const c = Claim.newClaim(new SchemaHash());
c.index[0].bytes[Flags.ByteIdx] &= 0b11111000;
c.index[0].bytes[Flags.ByteIdx] |= MerklizeFlag.Invalid;
expect(() => c.getMerklizePosition()).toThrow(
new Error(Constants.ERRORS.INCORRECT_MERKLIZE_POSITION)
c.index[0].bytes[Flags.ByteIdx] |= MerklizedFlag.Invalid;
expect(() => c.getMerklizedPosition()).toThrow(
new Error(Constants.ERRORS.INCORRECT_MERKLIZED_POSITION)
);
});
it('WithFlagMerklized', () => {
const claim = newClaim(new SchemaHash(), withFlagMerklize(MerklizePosition.Index));
expect(MerklizeFlag.Index).toEqual(claim.index[0].bytes[Flags.ByteIdx] & 0b11100000);
it('ClaimOptions.WithFlagMerklized', () => {
const claim = Claim.newClaim(
new SchemaHash(),
ClaimOptions.withFlagMerklized(MerklizedPosition.Index)
);
expect(MerklizedFlag.Index).toEqual(claim.index[0].bytes[Flags.ByteIdx] & 0b11100000);
});
it('withIndexMerklizedRoot', () => {
const expVal = BigInt(9999);
const expSlot = new ElemBytes();
expSlot.setBigInt(expVal);
const claim = Claim.newClaim(new SchemaHash(), ClaimOptions.withIndexMerklizedRoot(expVal));
expect(expSlot).toEqual(claim.index[2]);
const position = claim.getMerklizedPosition();
expect(MerklizedPosition.Index).toEqual(position);
});
it('WithValueMerklizedRoot', () => {
const expVal = BigInt(9999);
const expSlot = new ElemBytes();
expSlot.setBigInt(expVal);
const claim = Claim.newClaim(new SchemaHash(), ClaimOptions.withValueMerklizedRoot(expVal));
expect(expSlot).toEqual(claim.value[2]);
const position = claim.getMerklizedPosition();
expect(MerklizedPosition.Value).toEqual(position);
});
it('ClaimOptions.withMerklizedRoot', () => {
const expVal = BigInt(9999);
const expSlot = new ElemBytes();
expSlot.setBigInt(expVal);
const claim = Claim.newClaim(
new SchemaHash(),
ClaimOptions.withMerklizedRoot(expVal, MerklizedPosition.Index)
);
expect(expSlot).toEqual(claim.index[2]);
const position = claim.getMerklizedPosition();
expect(MerklizedPosition.Index).toEqual(position);
const claim2 = Claim.newClaim(
new SchemaHash(),
ClaimOptions.withMerklizedRoot(expVal, MerklizedPosition.Value)
);
expect(expSlot).toEqual(claim2.value[2]);
const position2 = claim2.getMerklizedPosition();
expect(MerklizedPosition.Value).toEqual(position2);
});
});
});

View File

@@ -1,4 +1,4 @@
import { DID, NetworkId, DIDMethodByte, DidMethod, withNetwork, Blockchain } from './../src/did';
import { DID, NetworkId, DIDMethodByte, DidMethod, DIDOptions, Blockchain } from './../src/did';
describe('DID tests', () => {
const tests: {
@@ -19,13 +19,13 @@ describe('DID tests', () => {
description: 'Test eth did',
identifier: 'zyaYCrj27j7gJfrBboMW49HFRSkQznyy12ABSVzTy',
did: 'did:iden3:eth:main:zyaYCrj27j7gJfrBboMW49HFRSkQznyy12ABSVzTy',
options: withNetwork(Blockchain.Ethereum, NetworkId.Main)
options: DIDOptions.withNetwork(Blockchain.Ethereum, NetworkId.Main)
},
{
description: 'Test polygon did',
identifier: 'wyFiV4w71QgWPn6bYLsZoysFay66gKtVa9kfu6yMZ',
did: 'did:iden3:polygon:mumbai:wyFiV4w71QgWPn6bYLsZoysFay66gKtVa9kfu6yMZ',
options: withNetwork(Blockchain.Polygon, NetworkId.Mumbai)
options: DIDOptions.withNetwork(Blockchain.Polygon, NetworkId.Mumbai)
}
];

View File

@@ -1,13 +1,17 @@
import { Hex } from './../src/hex';
import { ElemBytes } from './../src/elemBytes';
import { newClaim, withExpirationDate, withVersion } from '../src/claim';
import { ClaimOptions, Claim } from '../src/claim';
import { SchemaHash } from '../src/schemaHash';
import { poseidonHash } from '../src/utils';
describe('example new claim', () => {
it('new claim', async () => {
const schemaHash = new SchemaHash();
const expDate = new Date(Date.UTC(2021, 0, 10, 20, 30, 0, 0));
const claim = newClaim(schemaHash, withExpirationDate(expDate), withVersion(42));
const claim = Claim.newClaim(
schemaHash,
ClaimOptions.withExpirationDate(expDate),
ClaimOptions.withVersion(42)
);
const expDateRes = claim.getExpirationDate();
expect(expDateRes).toEqual(expDate);
expect(claim.getVersion()).toEqual(42);