Skip to content

Commit

Permalink
Move ensurePublicKeyEncoding to assertions & use keyType.
Browse files Browse the repository at this point in the history
  • Loading branch information
aljones15 committed Jul 5, 2024
1 parent 8871e5b commit 8882a67
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 24 deletions.
24 changes: 0 additions & 24 deletions test/EcdsaMultikey.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,7 @@
import * as base58 from 'base58-universal';
import * as EcdsaMultikey from '../lib/index.js';
import chai from 'chai';
import {getNamedCurveFromPublicMultikey} from '../lib/helpers.js';
import {
mockKey,
mockKeyEcdsaSecp256,
mockKeyEcdsaSecp384,
mockKeyEcdsaSecp521,
Expand Down Expand Up @@ -53,16 +51,6 @@ describe('EcdsaMultikey', () => {
});

describe('from', () => {
it('should auto-set key.id based on controller', async () => {
const {publicKeyMultibase} = mockKey;

const keyPair = await EcdsaMultikey.from(mockKey);

_ensurePublicKeyEncoding({keyPair, publicKeyMultibase});
expect(keyPair.id).to.equal(
'did:example:1234#zDnaeSMnptAKpH4AD41vTkwzjznW7yNetdRh9FJn8bJsbsdbw'
);
});

it('should error if publicKeyMultibase property is missing', async () => {
let error;
Expand Down Expand Up @@ -257,15 +245,3 @@ describe('EcdsaMultikey', () => {
});
});
});

function _ensurePublicKeyEncoding({keyPair, publicKeyMultibase}) {
keyPair.publicKeyMultibase.startsWith('z').should.be.true;
publicKeyMultibase.startsWith('z').should.be.true;
const decodedPubkey = base58.decode(publicKeyMultibase.slice(1));
const ecdsaCurve = getNamedCurveFromPublicMultikey({
publicMultikey: decodedPubkey
});
ecdsaCurve.should.equal('P-256');
const encodedPubkey = 'z' + base58.encode(decodedPubkey);
encodedPubkey.should.equal(keyPair.publicKeyMultibase);
}
22 changes: 22 additions & 0 deletions test/assertions.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import {stringToUint8Array} from './text-encoder.js';
import {CryptoKey} from '../lib/crypto.js';
import {webcrypto} from '../lib/crypto.js';
import {exportKeyPair} from '../lib/serialize.js';
import {getNamedCurveFromPublicMultikey} from '../lib/helpers.js';

chai.should();
const {expect} = chai;
Expand Down Expand Up @@ -168,3 +169,24 @@ export function testExport({curve}) {
expect(expectedSecretKey).to.deep.equal(secretKey);
});
}

export function testFrom({serializedKeyPair, id, keyType}) {
it('should auto-set key.id based on controller', async () => {
const {publicKeyMultibase} = serializedKeyPair;
const keyPair = await EcdsaMultikey.from(serializedKeyPair);
_ensurePublicKeyEncoding({keyPair, keyType, publicKeyMultibase});
expect(keyPair.id).to.equal(id);
});
}

function _ensurePublicKeyEncoding({keyPair, publicKeyMultibase, keyType}) {
keyPair.publicKeyMultibase.startsWith('z').should.be.true;
publicKeyMultibase.startsWith('z').should.be.true;
const decodedPubkey = base58.decode(publicKeyMultibase.slice(1));
const ecdsaCurve = getNamedCurveFromPublicMultikey({
publicMultikey: decodedPubkey
});
ecdsaCurve.should.equal(keyType);
const encodedPubkey = 'z' + base58.encode(decodedPubkey);
encodedPubkey.should.equal(keyPair.publicKeyMultibase);
}
4 changes: 4 additions & 0 deletions test/multikey.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import {multikeys} from './mock-data.js';
import {
testAlgorithm,
testExport,
testFrom,
testGenerate,
testSignVerify
} from './assertions.js';
Expand All @@ -30,6 +31,9 @@ describe('ecdsa-multikey', function() {
describe('sign and verify', function() {
testSignVerify({id, serializedKeyPair, keyType});
});
describe('from', function() {
testFrom({keyType, id, serializedKeyPair});
});
});
}
});

0 comments on commit 8882a67

Please sign in to comment.