From b3ad55e2706111e069312a2cfc39202fba961e57 Mon Sep 17 00:00:00 2001 From: Tashi D Gyeltshen Date: Thu, 2 Nov 2023 14:32:11 -0400 Subject: [PATCH] Add test to check if "publicKeyMultibase" is 35 bytes in length and multibase base58-btc encoded. --- tests/10-create.js | 39 ++++++++++++++++++++++++++++++++++++++- tests/helpers.js | 6 ++++++ 2 files changed, 44 insertions(+), 1 deletion(-) diff --git a/tests/10-create.js b/tests/10-create.js index 9141e11..41a4cd7 100644 --- a/tests/10-create.js +++ b/tests/10-create.js @@ -2,7 +2,9 @@ * Copyright 2022 Digital Bazaar, Inc. All Rights Reserved */ -import {bs58Decode, createInitialVc, getPublicKeyBytes} from './helpers.js'; +import { + bs58Decode, createInitialVc, getPublicKeyBytes, shouldBeBs58 +} from './helpers.js'; import chai from 'chai'; import { checkDataIntegrityProofFormat @@ -75,6 +77,41 @@ describe('eddsa-2022 (create)', function() { ).should.equal(true, 'Expected at least one proof to have "type" ' + 'property value "Multikey".'); }); + it('Dereferencing the "verificationMethod" MUST result in an ' + + 'object containing a type property with "Multikey" value.', + function() { + verificationMethodDocuments.should.not.eql([], 'Expected ' + + 'at least one "verificationMethodDocument".'); + verificationMethodDocuments.some( + verificationMethodDocument => + verificationMethodDocument?.type === 'Multikey' + ).should.equal(true, 'Expected at least one proof to have "type" ' + + 'property value "Multikey".'); + }); + it('The "publicKeyMultibase" value of the verification method MUST ' + + 'be 35 bytes in length and starts with the base-58-btc prefix (z).', + async function() { + verificationMethodDocuments.should.not.eql([], 'Expected ' + + 'at least one "verificationMethodDocument".'); + for(const verificationMethodDocument of verificationMethodDocuments) { + const multibase = 'z'; + const {publicKeyMultibase} = verificationMethodDocument; + const isMultibaseEncoded = + publicKeyMultibase.startsWith(multibase) && + shouldBeBs58(publicKeyMultibase); + isMultibaseEncoded.should.equal( + true, + 'Expected "publicKeyMultibase" value of the verification ' + + 'method to be multibase base58-btc encoded value' + ); + const publicKeyMultibaseBytes = bs58Decode({ + id: publicKeyMultibase + }); + publicKeyMultibaseBytes.byteLength.should.equal(35, 'Expected ' + + '"publicKeyMultibase" value of the verification method to ' + + 'be 35 bytes in length.'); + } + }); it('"proofValue" field when decoded to raw bytes, MUST be 64 bytes ' + 'in length if the associated public key is 32 bytes or 114 bytes ' + 'in length if the public key is 57 bytes.', async function() { diff --git a/tests/helpers.js b/tests/helpers.js index 20aa076..ab4e1b2 100644 --- a/tests/helpers.js +++ b/tests/helpers.js @@ -52,3 +52,9 @@ export const getPublicKeyBytes = async ({did}) => { export const bs58Decode = ({id}) => decoder.decode(id); export const bs58Encode = data => encoder.encode(data); + +// RegExp with bs58 characters in it +const bs58 = + /^[123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz]+$/; +// assert something is entirely bs58 encoded +export const shouldBeBs58 = s => bs58.test(s);