diff --git a/reference_contract/__test__/algo-did.test.ts b/reference_contract/__test__/algo-did.test.ts index 2bdfa75..60854b5 100644 --- a/reference_contract/__test__/algo-did.test.ts +++ b/reference_contract/__test__/algo-did.test.ts @@ -12,7 +12,7 @@ import { resolveDID, uploadDIDDocument, deleteDIDDocument, updateDIDDocument, } from '../src/index'; -jest.setTimeout(20000) +jest.setTimeout(20000); describe('Algorand DID', () => { /** @@ -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 () => { @@ -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)); }); diff --git a/reference_contract/src/index.ts b/reference_contract/src/index.ts index b598bf2..af13588 100644 --- a/reference_contract/src/index.ts +++ b/reference_contract/src/index.ts @@ -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 { 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({