Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion packages/rust-bindings/ts-src/dapp.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,6 @@ import wasmBase64 from '../lib/dapp/web/esm/index_bg.wasm';
// Expected to resolve to base64 encoded bytes of wasm module

const bytes = Buffer.from(wasmBase64, 'base64');
initSync(bytes);
initSync({ module: bytes });

export * from '../lib/dapp/web/esm/index.js';
2 changes: 1 addition & 1 deletion packages/rust-bindings/ts-src/wallet.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,6 @@ import wasmBase64 from '../lib/wallet/web/esm/index_bg.wasm';
// Expected to resolve to base64 encoded bytes of wasm module

const bytes = Buffer.from(wasmBase64, 'base64');
initSync(bytes);
initSync({ module: bytes });

export * from '../lib/wallet/web/esm/index.js';
86 changes: 86 additions & 0 deletions packages/sdk/test/ci/wasm/VerifiablePresentationRequestV1.test.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,21 @@
import _JB from 'json-bigint';
import fs from 'node:fs';
import path from 'node:path';

import { AttributeKeyString, TransactionHash } from '../../../src/pub/types.ts';
import { VerifiablePresentationRequestV1 } from '../../../src/pub/wasm.ts';
import { IdentityProviderDID } from '../../../src/pub/web3-id.ts';

const vraFixtureEncoded = fs
.readFileSync(path.resolve(__dirname, './fixtures/VerifiablePresentationRequestV1.Anchor.hex'))
.toString();
const vraFixture = JSON.parse(
fs.readFileSync(path.resolve(__dirname, './fixtures/VerifiablePresentationRequestV1.Anchor.json')).toString()
);
const presentationRequestFixture = JSON.parse(
fs.readFileSync(path.resolve(__dirname, './fixtures/VerifiablePresentationRequestV1.json')).toString()
);

const JSONBig = _JB({ alwaysParseAsBig: true, useNativeBigInt: true });

describe('VerifiablePresentationRequestV1', () => {
Expand All @@ -27,6 +39,39 @@ describe('VerifiablePresentationRequestV1', () => {
expect(presentationRequest).toEqual(roundtrip);
});

it('should match the JSON fixture representation', () => {
const context = VerifiablePresentationRequestV1.createSimpleContext(
Uint8Array.from([0, 1, 2, 3]),
'0102010201020102010201020102010201020102010201020102010201020102',
'Wine payment'
);
const statement = VerifiablePresentationRequestV1.statementBuilder()
.addIdentityStatement(
[0, 1, 2].map((i) => new IdentityProviderDID('Testnet', i)),
(b) => b.revealAttribute(AttributeKeyString.firstName)
)
.getStatements();
const transactionRef = TransactionHash.fromHexString(
'0102030401020304010203040102030401020304010203040102030401020304'
);
const presentationRequest = VerifiablePresentationRequestV1.create(context, statement, transactionRef);

const json = presentationRequest.toJSON();
const jsonString = JSONBig.stringify(json);
const expectedJsonString = JSONBig.stringify(presentationRequestFixture);

expect(jsonString).toBe(expectedJsonString);
});

it('should deserialize from JSON fixture representation', () => {
const request = VerifiablePresentationRequestV1.fromJSON(
presentationRequestFixture as VerifiablePresentationRequestV1.JSON
);
expect(request.toJSON()).toEqual(presentationRequestFixture);
});
});

describe('VerifiablePresentationRequestV1.Anchor', () => {
it('should compute the correct anchor', () => {
const context = VerifiablePresentationRequestV1.createSimpleContext(
Uint8Array.from([0, 1, 2, 3]),
Expand Down Expand Up @@ -55,4 +100,45 @@ describe('VerifiablePresentationRequestV1', () => {
};
expect(roundtrip).toEqual(expectedData);
});

it('should match the fixture anchor representation', () => {
const context = VerifiablePresentationRequestV1.createSimpleContext(
Uint8Array.from([0, 1, 2, 3]),
'0102010201020102010201020102010201020102010201020102010201020102',
'Wine payment'
);
const statement = VerifiablePresentationRequestV1.statementBuilder()
.addIdentityStatement(
[0, 1, 2].map((i) => new IdentityProviderDID('Testnet', i)),
(b) => b.revealAttribute(AttributeKeyString.firstName)
)
.getStatements();

const anchor = VerifiablePresentationRequestV1.createAnchor(context, statement, {
verifier: 'Test Verifier',
purpose: 'Age verification',
});

const anchorData = VerifiablePresentationRequestV1.decodeAnchor(anchor);
const json = {
type: anchorData.type,
version: anchorData.version,
hash: Buffer.from(anchorData.hash).toString('hex'),
public: anchorData.public,
};
const jsonString = JSONBig.stringify(json);
const expectedJsonString = JSONBig.stringify(vraFixture);

expect(jsonString).toBe(expectedJsonString);
});

it('should decode from fixture anchor representation', () => {
const anchor = Uint8Array.from(Buffer.from(vraFixtureEncoded, 'hex'));
const decoded = VerifiablePresentationRequestV1.decodeAnchor(anchor);

expect(decoded.type).toBe(vraFixture.type);
expect(decoded.version).toBe(Number(vraFixture.version));
expect(Buffer.from(decoded.hash).toString('hex')).toBe(vraFixture.hash);
expect(decoded.public).toEqual(vraFixture.public);
});
});
219 changes: 124 additions & 95 deletions packages/sdk/test/ci/wasm/VerifiablePresentationV1.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import _JB from 'json-bigint';
import fs from 'node:fs';
import path from 'node:path';

import { AttributeKeyString, IdentityObjectV1, IdentityProvider, IpInfo } from '../../../src/pub/types.ts';
import {
Expand All @@ -10,108 +12,135 @@ import { createAccountDID, createIdentityCommitmentInputWithHdWallet } from '../
import { BlockHash } from '../../../src/types/index.ts';
import { TESTNET_GLOBAL_CONTEXT, TEST_SEED_1 } from './constants.ts';

test('create testnet account-based presentation v1', () => {
const requestContext = VerifiablePresentationRequestV1.createContext({
given: [{ label: 'Nonce', context: Uint8Array.from([0, 1, 2]) }],
requested: ['BlockHash'],
});
const context = VerifiablePresentationV1.createContext(requestContext, [
{ label: 'BlockHash', context: BlockHash.fromHexString('01'.repeat(32)) },
]);

const values: Record<string, string> = {};
values.dob = '0';
values.firstName = 'a';

const statements: VerifiablePresentationV1.Statement[] = [
{
id: createAccountDID(
'Testnet',
'94d3e85bbc8ff0091e562ad8ef6c30d57f29b19f17c98ce155df2a30100df4cac5e161fb81aebe3a04300e63f086d0d8'
),
statement: [
{
attributeTag: AttributeKeyString.dob,
lower: '81',
type: 'AttributeInRange',
upper: '1231',
const presentationFixture = JSON.parse(
fs.readFileSync(path.resolve(__dirname, './fixtures/VerifiablePresentationV1.json')).toString()
);

const JSONBig = _JB({ alwaysParseAsBig: true, useNativeBigInt: true });

describe('VerifiablePresentationV1', () => {
test('create testnet account-based presentation v1', () => {
const requestContext = VerifiablePresentationRequestV1.createContext({
given: [{ label: 'Nonce', context: Uint8Array.from([0, 1, 2]) }],
requested: ['BlockHash'],
});
const context = VerifiablePresentationV1.createContext(requestContext, [
{ label: 'BlockHash', context: BlockHash.fromHexString('01'.repeat(32)) },
]);

const values: Record<string, string> = {};
values.dob = '0';
values.firstName = 'a';

const statements: VerifiablePresentationV1.Statement[] = [
{
id: createAccountDID(
'Testnet',
'94d3e85bbc8ff0091e562ad8ef6c30d57f29b19f17c98ce155df2a30100df4cac5e161fb81aebe3a04300e63f086d0d8'
),
statement: [
{
attributeTag: AttributeKeyString.dob,
lower: '81',
type: 'AttributeInRange',
upper: '1231',
},
{
attributeTag: AttributeKeyString.firstName,
type: 'RevealAttribute',
},
],
},
];
const inputs: VerifiablePresentationV1.CommitmentInput[] = [
{
type: 'account',
issuer: 1,
values,
randomness: {
dob: '575851a4e0558d589a57544a4a9f5ad1bd8467126c1b6767d32f633ea03380e6',
firstName: '575851a4e0558d589a57544a4a9f5ad1bd8467126c1b6767d32f633ea03380e6',
},
{
attributeTag: AttributeKeyString.firstName,
type: 'RevealAttribute',
},
];

const presentation = VerifiablePresentationV1.create(statements, inputs, context, TESTNET_GLOBAL_CONTEXT);

const json = JSON.stringify(presentation);
const roundtrip = VerifiablePresentationV1.fromJSON(JSON.parse(json));
expect(presentation).toEqual(roundtrip);
// TODO: for now we just check that it does not fail - later we need to check the actual values
});

test('create testnet id-based presentation v1', () => {
const requestContext = VerifiablePresentationRequestV1.createContext({
given: [{ label: 'Nonce', context: Uint8Array.from([0, 1, 2]) }],
requested: ['BlockHash'],
});
const context = VerifiablePresentationV1.createContext(requestContext, [
{ label: 'BlockHash', context: BlockHash.fromHexString('01'.repeat(32)) },
]);

const wallet = ConcordiumHdWallet.fromHex(TEST_SEED_1, 'Testnet');
const idObject: IdentityObjectV1 = JSON.parse(
fs.readFileSync('./test/ci/resources/identity-object.json').toString()
).value;
const ipInfo: IpInfo = JSON.parse(fs.readFileSync('./test/ci/resources/ip_info.json').toString()).value;

const inputContext: IdentityProvider = {
ipInfo,
arsInfos: {
[1]: {
arPublicKey: '0102',
arIdentity: 0,
arDescription: { description: 'test', name: 'test', url: 'https://ar.com' },
},
],
},
];
const inputs: VerifiablePresentationV1.CommitmentInput[] = [
{
type: 'account',
issuer: 1,
values,
randomness: {
dob: '575851a4e0558d589a57544a4a9f5ad1bd8467126c1b6767d32f633ea03380e6',
firstName: '575851a4e0558d589a57544a4a9f5ad1bd8467126c1b6767d32f633ea03380e6',
},
},
];
};
const input = createIdentityCommitmentInputWithHdWallet(idObject, inputContext, 0, wallet);

const presentation = VerifiablePresentationV1.create(statements, inputs, context, TESTNET_GLOBAL_CONTEXT);
const statements: VerifiablePresentationV1.Statement[] = [
{
id: 'ccd:testnet:id',
statement: [
{
attributeTag: AttributeKeyString.dob,
lower: '81',
type: 'AttributeInRange',
upper: '1231',
},
{
attributeTag: AttributeKeyString.firstName,
type: 'RevealAttribute',
},
],
},
];

const json = JSON.stringify(presentation);
const roundtrip = VerifiablePresentationV1.fromJSON(JSON.parse(json));
expect(presentation).toEqual(roundtrip);
// TODO: for now we just check that it does not fail - later we need to check the actual values
});
const presentation = VerifiablePresentationV1.create(statements, [input], context, TESTNET_GLOBAL_CONTEXT);

test('create testnet id-based presentation v1', () => {
const requestContext = VerifiablePresentationRequestV1.createContext({
given: [{ label: 'Nonce', context: Uint8Array.from([0, 1, 2]) }],
requested: ['BlockHash'],
const json = JSON.stringify(presentation);
const roundtrip = VerifiablePresentationV1.fromJSON(JSON.parse(json));
expect(presentation).toEqual(roundtrip);
// TODO: for now we just check that it does not fail - later we need to check the actual values
});
const context = VerifiablePresentationV1.createContext(requestContext, [
{ label: 'BlockHash', context: BlockHash.fromHexString('01'.repeat(32)) },
]);

const wallet = ConcordiumHdWallet.fromHex(TEST_SEED_1, 'Testnet');
const idObject: IdentityObjectV1 = JSON.parse(
fs.readFileSync('./test/ci/resources/identity-object.json').toString()
).value;
const ipInfo: IpInfo = JSON.parse(fs.readFileSync('./test/ci/resources/ip_info.json').toString()).value;

const inputContext: IdentityProvider = {
ipInfo,
arsInfos: {
[1]: {
arPublicKey: '0102',
arIdentity: 0,
arDescription: { description: 'test', name: 'test', url: 'https://ar.com' },
},
},
};
const input = createIdentityCommitmentInputWithHdWallet(idObject, inputContext, 0, wallet);

const statements: VerifiablePresentationV1.Statement[] = [
{
id: 'ccd:testnet:id',
statement: [
{
attributeTag: AttributeKeyString.dob,
lower: '81',
type: 'AttributeInRange',
upper: '1231',
},
{
attributeTag: AttributeKeyString.firstName,
type: 'RevealAttribute',
},
],
},
];

const presentation = VerifiablePresentationV1.create(statements, [input], context, TESTNET_GLOBAL_CONTEXT);
it('should match the JSON fixture representation', () => {
const presentation = VerifiablePresentationV1.fromJSON(
presentationFixture as unknown as VerifiablePresentationV1.JSON
);

const json = presentation.toJSON();
const jsonString = JSONBig.stringify(json);
const expectedJsonString = JSONBig.stringify(presentationFixture);

const json = JSON.stringify(presentation);
const roundtrip = VerifiablePresentationV1.fromJSON(JSON.parse(json));
expect(presentation).toEqual(roundtrip);
// TODO: for now we just check that it does not fail - later we need to check the actual values
expect(jsonString).toBe(expectedJsonString);
});

it('should deserialize from JSON fixture representation', () => {
const presentation = VerifiablePresentationV1.fromJSON(
presentationFixture as unknown as VerifiablePresentationV1.JSON
);
expect(presentation.toJSON()).toEqual(presentationFixture);
});
});
Loading