Skip to content

Commit

Permalink
fix: unitary tests
Browse files Browse the repository at this point in the history
  • Loading branch information
elribonazo committed Oct 29, 2024
1 parent b91a4e7 commit 554abd6
Show file tree
Hide file tree
Showing 6 changed files with 38 additions and 63 deletions.
4 changes: 2 additions & 2 deletions src/apollo/Apollo.ts
Original file line number Diff line number Diff line change
Expand Up @@ -353,7 +353,7 @@ export default class Apollo implements ApolloInterface, KeyRestoration {
baseKey.keySpecification.set(KeyProperties.chainCode, Buffer.from(Uint8Array.from(hdKey.chainCode)).toString("hex"));
baseKey.keySpecification.set(KeyProperties.derivationPath, Buffer.from(defaultPath).toString("hex"));
baseKey.keySpecification.set(KeyProperties.index, derivationIndex);
if (defaultPath) {
if (derivationParam) {
const privateKey = baseKey.derive(defaultPath);
return privateKey;
}
Expand Down Expand Up @@ -401,7 +401,7 @@ export default class Apollo implements ApolloInterface, KeyRestoration {
baseKey.keySpecification.set(KeyProperties.derivationPath, Buffer.from(defaultPath).toString("hex"));
baseKey.keySpecification.set(KeyProperties.index, derivationIndex);

if (defaultPath) {
if (derivationParam) {
const privateKey = baseKey.derive(defaultPath);
return privateKey;
}
Expand Down
15 changes: 12 additions & 3 deletions src/castor/Castor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import { PeerDIDResolver } from "./resolver/PeerDIDResolver";
import { PeerDIDCreate } from "../peer-did/PeerDIDCreate";
import { LongFormPrismDIDResolver } from "./resolver/LongFormPrismDIDResolver";
import {
CastorError,
VerificationMethod as DIDDocumentVerificationMethod,
VerificationMethods as DIDDocumentVerificationMethods,
getUsageId,
Expand Down Expand Up @@ -137,14 +138,22 @@ export default class Castor implements CastorInterface {
for (const [index, authenticationKey] of authenticationKeys.entries()) {
const pk = "publicKey" in authenticationKey ? authenticationKey.publicKey : authenticationKey
const prismDIDPublicKey = new PrismDIDPublicKey(
getUsageId(Usage.AUTHENTICATION_KEY, index),
Usage.AUTHENTICATION_KEY,
getUsageId(Usage.ISSUING_KEY, index),
Usage.ISSUING_KEY,
pk,
)
didPublicKeys.push(prismDIDPublicKey.toProto())
}
}
} else {
const additionalPk = new PrismDIDPublicKey(
getUsageId(Usage.ISSUING_KEY),
Usage.ISSUING_KEY,
masterPublicKey,
)

didPublicKeys.push(additionalPk.toProto())

}

const didCreationData =
new Protos.io.iohk.atala.prism.protos.CreateDIDOperation.DIDCreationData({
Expand Down
26 changes: 4 additions & 22 deletions tests/apollo/Apollo.createPrivateKey.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,27 +20,6 @@ describe("Apollo", () => {
const seedHex = "947877896c61a5c64f266adbebbc69a2a01f1a2cfbf72c08a11c693d0429ccded34bdc0c28b5be910a5095b97e7bc6e3e209527ce8e75f9964d25cd6f6ad63e0";

describe("Secp256k1", () => {
it("default - creates a new key", () => {
const result = apollo.createPrivateKey({
[KeyProperties.type]: KeyTypes.EC,
[KeyProperties.curve]: Curve.SECP256K1,
[KeyProperties.seed]: seedHex,
});

expect(result).to.be.an.instanceOf(Secp256k1PrivateKey);
expect(result.raw).to.eql(Uint8Array.from([232, 19, 52, 112, 248, 184, 7, 231, 180, 5, 168, 209, 33, 77, 26, 108, 130, 201, 137, 168, 15, 197, 29, 152, 88, 235, 87, 76, 73, 255, 159, 229]));
expect(result.type).to.eq(KeyTypes.EC);
expect(result.curve).to.eq(Curve.SECP256K1);

expect(result.getProperty(KeyProperties.curve)).to.eq(Curve.SECP256K1);
expect(result.getProperty(KeyProperties.chainCode)).to.eq("7e9952eb18d135283fd633180e31b202a5ec87e3e37cc66c6836f18bdf9684b2");

// no derivationPath provided, defaults to `m/29'/29'/0'/4'/0'` hexed
expect(result.getProperty(KeyProperties.derivationPath)).to.eq("6d2f3239272f3239272f30272f34272f3027");

// no index provided, defaults to 0
expect(result.getProperty(KeyProperties.index)).to.eq("0");
});

it("KeyProperties.type - missing - throws", () => {
const sut = () => apollo.createPrivateKey({
Expand Down Expand Up @@ -101,8 +80,11 @@ describe("Apollo", () => {
[KeyProperties.curve]: Curve.SECP256K1,
[KeyProperties.seed]: fixture.seed,
});
assert.equal(master.to.String("hex"), fixture.raw);


const derivationPath = DerivationPath.fromPath(fixture.path.toString(), [DeprecatedDerivationPath, PrismDerivationPath])

const child = master.isDerivable()
? master.derive(derivationPath.toString())
: null;
Expand All @@ -114,7 +96,7 @@ describe("Apollo", () => {
[KeyProperties.derivationPath]: fixture.path
});

assert.equal(master.to.String("hex"), fixture.raw);

assert.equal(child?.to.String("hex"), fixture.derived);
assert.equal(derived.to.String("hex"), fixture.derived);
});
Expand Down
1 change: 1 addition & 0 deletions tests/apollo/keys/Ed25519.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,7 @@ describe("Keys", () => {
const raw1 = (derived as PrivateKey).getEncoded().toString();
const raw2 = withDerivationPath.getEncoded().toString();
const raw3 = privateKey.getEncoded().toString();

expect(raw1).to.equal(raw2);
expect(raw1).to.not.equal(raw3);
});
Expand Down
34 changes: 16 additions & 18 deletions tests/castor/PrismDID.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,11 @@ const apollo = new Apollo();
const castor = new Castor(apollo);

describe("PrismDID", () => {
const secpDid = "did:prism:74f299ab7e5b2d127802b6a7093f5b9dd642e95aadf858cade6ec9af6929a487:CscBCsQBElwKB21hc3RlcjAQAUJPCglzZWNwMjU2azESIP0gMhTAVOk7SgWRluzmeJIjtm2-YMc6AbrD3ePKJQj-GiDZlsa5pQuXGzKvgK10D8SzuDvh79u5oMB7-ZeJNAh-ixJkCg9hdXRoZW50aWNhdGlvbjAQBEJPCglzZWNwMjU2azESIP0gMhTAVOk7SgWRluzmeJIjtm2-YMc6AbrD3ePKJQj-GiDZlsa5pQuXGzKvgK10D8SzuDvh79u5oMB7-ZeJNAh-iw";
const secpDid = "did:prism:767105eeba99753517bd0c8d4334678354771e711169eef832b4dce05c9ee29d:CsABCr0BElwKB21hc3RlcjAQAUJPCglzZWNwMjU2azESIP0gMhTAVOk7SgWRluzmeJIjtm2-YMc6AbrD3ePKJQj-GiDZlsa5pQuXGzKvgK10D8SzuDvh79u5oMB7-ZeJNAh-ixJdCghpc3N1aW5nMBACQk8KCXNlY3AyNTZrMRIg_SAyFMBU6TtKBZGW7OZ4kiO2bb5gxzoBusPd48olCP4aINmWxrmlC5cbMq-ArXQPxLO4O-Hv27mgwHv5l4k0CH6L";
const secpMultibase = "zSXxpYB6edvxvWxRTo3kMUoTTQVHpbNnXo2Z1AjLA78iqLdK2kVo5xw9rGg8uoEgmhxYahNur3RvV7HnaktWBqkXt";
const ed25519Did = "did:prism:30dc8276c5facc040dab037dd8c5c6d0e3218720a1e1c241c0341fcfea83cc60:Co0CCooCElwKB21hc3RlcjAQAUJPCglzZWNwMjU2azESIP0gMhTAVOk7SgWRluzmeJIjtm2-YMc6AbrD3ePKJQj-GiDZlsa5pQuXGzKvgK10D8SzuDvh79u5oMB7-ZeJNAh-ixJkCg9hdXRoZW50aWNhdGlvbjAQBEJPCglzZWNwMjU2azESIP0gMhTAVOk7SgWRluzmeJIjtm2-YMc6AbrD3ePKJQj-GiDZlsa5pQuXGzKvgK10D8SzuDvh79u5oMB7-ZeJNAh-ixJECghpc3N1aW5nMBACSjYKB0VkMjU1MTkSK2RtNWYyR2RSNUJhSHBSeEI4YlRFbHZFXzBnSUMycDQwNE1zeDlzd0o5MTQ";
const ed25519Did = "did:prism:94be823b0f7be2cc66449fc41844517c4778fb965273a82dea349639f4a1aaef:CpwBCpkBElwKB21hc3RlcjAQAUJPCglzZWNwMjU2azESIP0gMhTAVOk7SgWRluzmeJIjtm2-YMc6AbrD3ePKJQj-GiDZlsa5pQuXGzKvgK10D8SzuDvh79u5oMB7-ZeJNAh-ixI5Cghpc3N1aW5nMBACSisKB0VkMjU1MTkSIHZuX9hnUeQWh6UcQfG0xJbxP9ICAtqeNODLMfbMCfde";
const ed25519Multibase = "z8yJizaEga14wo4pHUDBXCnrp5A9WbFDuk5DZnPq5mCHK";
const x25519Did = "did:prism:a5cce445f3e013fc2fa76914742b8b493b436251a29727ea57a4708b80fa3a68:CowCCokCElwKB21hc3RlcjAQAUJPCglzZWNwMjU2azESIP0gMhTAVOk7SgWRluzmeJIjtm2-YMc6AbrD3ePKJQj-GiDZlsa5pQuXGzKvgK10D8SzuDvh79u5oMB7-ZeJNAh-ixJkCg9hdXRoZW50aWNhdGlvbjAQBEJPCglzZWNwMjU2azESIP0gMhTAVOk7SgWRluzmeJIjtm2-YMc6AbrD3ePKJQj-GiDZlsa5pQuXGzKvgK10D8SzuDvh79u5oMB7-ZeJNAh-ixJDCghpc3N1aW5nMBACSjUKBlgyNTUxORIrX1BqSGVmRmg5SDdxSDNWdDdNTzhWRU4tRjJQbFdjWHpkeHc2TFBreEVHRQ";
const x25519Did = "did:prism:54c88d4d9cf4fc7504e612940f4522f7f6ec1af784fdacf2861590522e4a5f6b:CpsBCpgBElwKB21hc3RlcjAQAUJPCglzZWNwMjU2azESIP0gMhTAVOk7SgWRluzmeJIjtm2-YMc6AbrD3ePKJQj-GiDZlsa5pQuXGzKvgK10D8SzuDvh79u5oMB7-ZeJNAh-ixI4Cghpc3N1aW5nMBACSioKBlgyNTUxORIg_PjHefFh9H7qH3Vt7MO8VEN-F2PlWcXzdxw6LPkxEGE";
const x25519Multibase = "zJ2VmASEaRF41F4BQSydGNi7zd5ud5YhqXxTKicPGd5FN";

describe("PrismDidPublicKey", () => {
Expand Down Expand Up @@ -166,7 +166,7 @@ describe("PrismDID", () => {
});

const masterKeyId = getUsageId(Usage.MASTER_KEY);
const authKeyId = getUsageId(Usage.AUTHENTICATION_KEY);
const issuingKeyId = getUsageId(Usage.ISSUING_KEY);

const testVerificationMethod = (sut: any, didStr: string, keyId: string, keyMultibase: string, curve: Curve) => {
expect(sut).to.be.instanceOf(VerificationMethod);
Expand Down Expand Up @@ -194,9 +194,9 @@ describe("PrismDID", () => {
// authentication key correctly encoded > decoded
const authProp = sut.coreProperties.at(1) as Authentication;
expect(authProp).to.be.instanceOf(Authentication);
expect(authProp.urls[0]).to.eq(`${secpDid}#${authKeyId}`);
expect(authProp.urls[0]).to.eq(`${secpDid}#${issuingKeyId}`);
const authvm0 = authProp.verificationMethods.at(0);
testVerificationMethod(authvm0, secpDid, authKeyId, secpMultibase, Curve.SECP256K1);
testVerificationMethod(authvm0, secpDid, issuingKeyId, secpMultibase, Curve.SECP256K1);

// no services given - so empty
const services = sut.coreProperties.at(2) as Services;
Expand All @@ -212,44 +212,42 @@ describe("PrismDID", () => {
testVerificationMethod(vm0, secpDid, masterKeyId, secpMultibase, Curve.SECP256K1);

const vm1 = verificationMethods.values.at(1);
testVerificationMethod(vm1, secpDid, authKeyId, secpMultibase, Curve.SECP256K1);
testVerificationMethod(vm1, secpDid, issuingKeyId, secpMultibase, Curve.SECP256K1);
});

test("issuing keys", async () => {
const expectedDid = "did:prism:44a6f5f04609666ce54770e5d9ab67801b41d8e66e4e1cfdf9a2e75ef5bda3e2:CtICCs8CElwKB21hc3RlcjAQAUJPCglzZWNwMjU2azESIP0gMhTAVOk7SgWRluzmeJIjtm2-YMc6AbrD3ePKJQj-GiDZlsa5pQuXGzKvgK10D8SzuDvh79u5oMB7-ZeJNAh-ixJkCg9hdXRoZW50aWNhdGlvbjAQBEJPCglzZWNwMjU2azESIP0gMhTAVOk7SgWRluzmeJIjtm2-YMc6AbrD3ePKJQj-GiDZlsa5pQuXGzKvgK10D8SzuDvh79u5oMB7-ZeJNAh-ixJECghpc3N1aW5nMBACSjYKB0VkMjU1MTkSK2RtNWYyR2RSNUJhSHBSeEI4YlRFbHZFXzBnSUMycDQwNE1zeDlzd0o5MTQSQwoIaXNzdWluZzEQAko1CgZYMjU1MTkSK19QakhlZkZoOUg3cUgzVnQ3TU84VkVOLUYyUGxXY1h6ZHh3NkxQa3hFR0U";
const expectedDid = "did:prism:025f0ca4df07792c061b1a1558a9ad34fba61b5dc44dd9f0bbdae296579f67a9:CtYBCtMBElwKB21hc3RlcjAQAUJPCglzZWNwMjU2azESIP0gMhTAVOk7SgWRluzmeJIjtm2-YMc6AbrD3ePKJQj-GiDZlsa5pQuXGzKvgK10D8SzuDvh79u5oMB7-ZeJNAh-ixI5Cghpc3N1aW5nMBACSisKB0VkMjU1MTkSIHZuX9hnUeQWh6UcQfG0xJbxP9ICAtqeNODLMfbMCfdeEjgKCGlzc3VpbmcxEAJKKgoGWDI1NTE5EiD8-Md58WH0fuofdW3sw7xUQ34XY-VZxfN3HDos-TEQYQ";
const prismDid = await castor.createPrismDID(Fixtures.Keys.secp256K1, [], [ed25519, x25519]);
const sut = await castor.resolveDID(prismDid.toString());

expect(sut.coreProperties).to.be.an("array").to.have.length(6);
//Lowering to 5 as we are no longer creating the MasterKey additionally as an Issuing key
expect(sut.coreProperties).to.be.an("array").to.have.length(5);

// index 2 & 3 should be issuing keys

// no services given - so empty
const services = sut.coreProperties.at(4) as Services;
const services = sut.coreProperties.at(3) as Services;
expect(services).to.be.instanceOf(Services);
expect(services.values).to.be.empty;

// 2 issuing keys given - 4 total
const verificationMethods = sut.coreProperties.at(5) as VerificationMethods;
const verificationMethods = sut.coreProperties.at(4) as VerificationMethods;
expect(verificationMethods).to.be.instanceOf(VerificationMethods);
expect(verificationMethods.values).to.have.length(4);
expect(verificationMethods.values).to.have.length(3);

const vm0 = verificationMethods.values.at(0);
testVerificationMethod(vm0, expectedDid, masterKeyId, secpMultibase, Curve.SECP256K1);

const vm1 = verificationMethods.values.at(1);
testVerificationMethod(vm1, expectedDid, authKeyId, secpMultibase, Curve.SECP256K1);

const vm2 = verificationMethods.values.at(2);
const vm2 = verificationMethods.values.at(1);
testVerificationMethod(vm2, expectedDid, getUsageId(Usage.ISSUING_KEY, 0), ed25519Multibase, Curve.ED25519);

const vm3 = verificationMethods.values.at(3);
const vm3 = verificationMethods.values.at(2);
testVerificationMethod(vm3, expectedDid, getUsageId(Usage.ISSUING_KEY, 1), x25519Multibase, Curve.X25519);
});
});

it("Should correctly create a prismDID from an existing HexKey", async () => {
const didExample = "did:prism:03425669b4d84b21a323c60bb41601eb22906b1a6427be3126277420faa6d1f3:CscBCsQBElwKB21hc3RlcjAQAUJPCglzZWNwMjU2azESIDS5zeYUkLCSAJLI6aLXRTPRxstCLPUEI6TgBrAVCHkwGiDk-ffklrHIFW7pKkT8i-YksXi-XXi5h31czUMaVClcpxJkCg9hdXRoZW50aWNhdGlvbjAQBEJPCglzZWNwMjU2azESIDS5zeYUkLCSAJLI6aLXRTPRxstCLPUEI6TgBrAVCHkwGiDk-ffklrHIFW7pKkT8i-YksXi-XXi5h31czUMaVClcpw";
const didExample = "did:prism:569cd0802f6f4293ff114ab7fa631ebf2532ae51ae96d1f515d37717e97b5b2c:CsABCr0BElwKB21hc3RlcjAQAUJPCglzZWNwMjU2azESIDS5zeYUkLCSAJLI6aLXRTPRxstCLPUEI6TgBrAVCHkwGiDk-ffklrHIFW7pKkT8i-YksXi-XXi5h31czUMaVClcpxJdCghpc3N1aW5nMBACQk8KCXNlY3AyNTZrMRIgNLnN5hSQsJIAksjpotdFM9HGy0Is9QQjpOAGsBUIeTAaIOT59-SWscgVbukqRPyL5iSxeL5deLmHfVzNQxpUKVyn";
const resolvedDID = await castor.resolveDID(didExample);

const pubHex = "0434b9cde61490b0920092c8e9a2d74533d1c6cb422cf50423a4e006b015087930e4f9f7e496b1c8156ee92a44fc8be624b178be5d78b9877d5ccd431a54295ca7";
Expand Down
21 changes: 3 additions & 18 deletions tests/pollux/Pollux.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -905,17 +905,8 @@ describe("Pollux", () => {
disclosureFrame: {},
privateKey: sk
});

const [header, payload, signature] = credential.replace("~", "").split(".");
const [onlySignature, ...disclosures] = signature.split(",");

const parseCredential = await pollux.parseCredential(
Buffer.from(JSON.stringify({
protected: header,
payload: payload,
signature: onlySignature,
disclosures: disclosures
})),
Buffer.from(credential),
{
type: CredentialType.SDJWT
}
Expand Down Expand Up @@ -993,15 +984,9 @@ describe("Pollux", () => {
const pr = new PresentationRequest(
AttachmentFormats.SDJWT, Fixtures.Credentials.SDJWT.presentationRequest
);
const [header, payload, signature] = credential.replace("~", "").split(".");
const [onlySignature, ...disclosures] = signature.split(",");

const parseCredential = await pollux.parseCredential(
Buffer.from(JSON.stringify({
protected: header,
payload: payload,
signature: onlySignature,
disclosures: disclosures
})),
Buffer.from(credential),
{
type: CredentialType.SDJWT
}
Expand Down

0 comments on commit 554abd6

Please sign in to comment.