Skip to content

Commit

Permalink
update tests
Browse files Browse the repository at this point in the history
  • Loading branch information
joe-p committed Jun 19, 2024
1 parent 2b636a6 commit 13389bd
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 26 deletions.
34 changes: 18 additions & 16 deletions reference_contract/__test__/algo-did.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import {
resolveDID, uploadDIDDocument, deleteDIDDocument, updateDIDDocument,
} from '../src/index';

jest.setTimeout(20000)
jest.setTimeout(20000);

describe('Algorand DID', () => {
/**
Expand Down Expand Up @@ -68,39 +68,41 @@ describe('Algorand DID', () => {
describe('uploadDIDDocument and Resolve', () => {
it('(LARGE) DIDocument upload and resolve', async () => {
const { appId } = await appClient.getAppReference();
const addr = algosdk.encodeAddress(bigDataUserKey);
const pubkeyHex = Buffer.from(bigDataUserKey).toString('hex');

// Large upload
await uploadDIDDocument(bigData, Number(appId), bigDataUserKey, sender, algodClient);

// Reconstruct DID from several boxes
const resolvedData: Buffer = await resolveDID(`did:algo:${addr}-${appId}`, algodClient);
expect(resolvedData.toString('hex')).toEqual(bigData.toString('hex'))
})
const resolvedData: Buffer = await resolveDID(`did:algo:custom:app:${appId}:${pubkeyHex}`, algodClient);
expect(resolvedData.toString('hex')).toEqual(bigData.toString('hex'));
});

it('(SMALL) DIDocument upload and resolve', async () => {
const { appId } = await appClient.getAppReference();
const addr = algosdk.encodeAddress(smallDataUserKey);
const pubkeyHex = Buffer.from(smallDataUserKey).toString('hex');

// Small upload
await uploadDIDDocument(Buffer.from(JSON.stringify(smallJSONObject)),
await uploadDIDDocument(
Buffer.from(JSON.stringify(smallJSONObject)),
Number(appId),
smallDataUserKey,
sender,
algodClient);
algodClient,
);

// Reconstruct DID from several boxes
const resolvedData: Buffer = await resolveDID(`did:algo:${addr}-${appId}`, algodClient);
expect(resolvedData.toString('hex')).toEqual(Buffer.from(JSON.stringify(smallJSONObject)).toString('hex'))
})
})
const resolvedData: Buffer = await resolveDID(`did:algo:custom:app:${appId}:${pubkeyHex}`, algodClient);
expect(resolvedData.toString('hex')).toEqual(Buffer.from(JSON.stringify(smallJSONObject)).toString('hex'));
});
});

describe('deleteDIDDocument', () => {
const deleteDIDDocumentTest = async (userKey: Uint8Array) => {
await deleteDIDDocument(appId, userKey, sender, algodClient);
const pubkeyHex = Buffer.from(userKey).toString('hex');

const addr = algosdk.encodeAddress(userKey);
await expect(resolveDID(`did:algo:${addr}-${appId}`, algodClient)).rejects.toThrow();
await expect(resolveDID(`did:algo:custom:app:${appId}:${pubkeyHex}`, algodClient)).rejects.toThrow();
};

it('deletes big (multi-box) data', async () => {
Expand Down Expand Up @@ -142,8 +144,8 @@ describe('Algorand DID', () => {
algodClient,
);

const addr = algosdk.encodeAddress(updateDataUserKey);
const resolvedData = await resolveDID(`did:algo:${addr}-${appId}`, algodClient);
const pubkeyHex = Buffer.from(updateDataUserKey).toString('hex');
const resolvedData = await resolveDID(`did:algo:custom:app:${appId}:${pubkeyHex}`, algodClient);

expect(resolvedData.toString()).toEqual(JSON.stringify(smallJSONObject));
});
Expand Down
27 changes: 17 additions & 10 deletions reference_contract/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,25 +22,32 @@ export type Metadata = {start: bigint, end: bigint, status: bigint, endSize: big
export async function resolveDID(did: string, algodClient: algosdk.Algodv2): Promise<Buffer> {
const splitDid = did.split(':');

if (splitDid[0] !== 'did') throw new Error(`Invalid protocol. Expected 'did', got ${splitDid[0]}`);
if (splitDid[1] !== 'algo') throw new Error(`Invalid DID method. Expected 'algod', got ${splitDid[1]}`);
const idxOffset = splitDid.length === 6 ? 0 : 1;

const splitID = splitDid[2].split('-');
if (splitDid[0] !== 'did') {
throw new Error(`invalid protocol, expected 'did', got ${splitDid[0]}`);
}
if (splitDid[1] !== 'algo') {
throw new Error(`invalid DID method, expected 'algo', got ${splitDid[1]}`);
}

let pubKey: Uint8Array;
try {
pubKey = algosdk.decodeAddress(splitID[0]).publicKey;
} catch (e) {
throw new Error(`Invalid public key. Expected Algorand address, got ${splitID[0]}`);
const nameSpace = splitDid[3 - idxOffset];

if (nameSpace !== 'app') {
throw new Error(`invalid namespace, expected 'app', got ${nameSpace}`);
}

const pubKey = Buffer.from(splitDid[5 - idxOffset], 'hex');

let appID: bigint;

try {
appID = BigInt(splitID[1]);
appID = BigInt(splitDid[4 - idxOffset]);
algosdk.encodeUint64(appID);
} catch (e) {
throw new Error(`Invalid app ID. Expected uint64, got ${splitID[1]}`);
throw new Error(
`invalid app ID, expected uint64, got ${splitDid[4 - idxOffset]}`,
);
}

const appClient = new ApplicationClient({
Expand Down

0 comments on commit 13389bd

Please sign in to comment.