Skip to content

Commit

Permalink
feat: Add Cryptography key abstraction for signable, verifiable keys,…
Browse files Browse the repository at this point in the history
… and also private and public base impls (hyperledger#67)
  • Loading branch information
elribonazo authored Aug 10, 2023
1 parent 02199fc commit 61d9dbd
Show file tree
Hide file tree
Showing 54 changed files with 1,290 additions and 1,138 deletions.
28 changes: 16 additions & 12 deletions demos/browser/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

71 changes: 42 additions & 29 deletions demos/browser/src/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -74,10 +74,18 @@ function KeyPair({curve = SDK.Domain.Curve.SECP256K1}: { curve?: SDK.Domain.Curv
if (!mnemonics) return;

const seed = apollo.createSeed(mnemonics, "my-secret");
const keyPair = apollo.createKeyPairFromKeyCurve({
curve,
}, seed);
setKeyPair(keyPair);

const privateKey = apollo.createPrivateKey({
type: SDK.Domain.KeyTypes.EC,
curve:curve,
seed: Buffer.from(seed.value).toString("hex"),
});

setKeyPair({
curve: curve,
privateKey: privateKey,
publicKey: privateKey.publicKey()
});
}

return (
Expand All @@ -101,7 +109,7 @@ function KeyPair({curve = SDK.Domain.Curve.SECP256K1}: { curve?: SDK.Domain.Curv
{keyPair ? (
<div>
<p>
<b>Curve:</b> {keyPair.keyCurve.curve}
<b>Curve:</b> {keyPair.curve}
</p>
<p>
<b>Public key:</b>{" "}
Expand Down Expand Up @@ -129,12 +137,10 @@ function Signatures({keyPair}: { keyPair: SDK.Domain.KeyPair }) {
const [isSignatureValid, setIsSignatureValid] = React.useState<boolean | undefined>(undefined);

function signData() {
const helloWorldSig = apollo.signStringMessage(
keyPair.privateKey,
"hello world"
);

setSignatureEncoded(jose.base64url.encode(helloWorldSig.value));
if (keyPair.privateKey.isSignable() ) {
const helloWorldSig = keyPair.privateKey.sign(Buffer.from( "hello world"))
setSignatureEncoded(jose.base64url.encode(helloWorldSig));
}
}

function verifySignature() {
Expand All @@ -143,11 +149,10 @@ function Signatures({keyPair}: { keyPair: SDK.Domain.KeyPair }) {
let isValid;

try {
isValid = apollo.verifySignature(
keyPair.publicKey,
new TextEncoder().encode("hello world"),
jose.base64url.decode(signatureEncoded)
);
if (keyPair.publicKey.canVerify()) {
isValid = keyPair.publicKey.verify(Buffer.from("hello world"), Buffer.from(jose.base64url.decode(signatureEncoded)))
}

} catch (e) {
console.warn("Failed to validate signature", e);
isValid = false;
Expand All @@ -156,7 +161,7 @@ function Signatures({keyPair}: { keyPair: SDK.Domain.KeyPair }) {
setIsSignatureValid(isValid);
}

if (keyPair.keyCurve.curve === SDK.Domain.Curve.X25519) {
if (keyPair.curve === SDK.Domain.Curve.X25519) {
return <b>Signatures not supported for X25519 keys!</b>;
}

Expand Down Expand Up @@ -194,10 +199,15 @@ function Dids() {
if (!mnemonics) return;

const seed = apollo.createSeed(mnemonics, "my-secret");
const keyPair = apollo.createKeyPairFromKeyCurve({
curve: SDK.Domain.Curve.SECP256K1,
}, seed);
const prismDID = await castor.createPrismDID(keyPair.publicKey, [

const privateKey = apollo.createPrivateKey({
type: SDK.Domain.KeyTypes.EC,
curve:SDK.Domain.Curve.SECP256K1,
seed: Buffer.from(seed.value).toString("hex"),
});


const prismDID = await castor.createPrismDID(privateKey.publicKey(), [
exampleService,
]);

Expand All @@ -215,15 +225,18 @@ function Dids() {
async function createPeerDid() {
if (!mnemonics) return;

const seed = apollo.createSeed(mnemonics, "my-secret");
const authKeyPair = apollo.createKeyPairFromKeyCurve({
curve: Domain.Curve.ED25519,
}, seed);
const keyAgreementKeyPair = apollo.createKeyPairFromKeyCurve({
curve: Domain.Curve.X25519,
}, seed);
const authPrivateKey = apollo.createPrivateKey({
type: SDK.Domain.KeyTypes.EC,
curve:SDK.Domain.Curve.ED25519,
});

const keyAgreementPrivateKey = apollo.createPrivateKey({
type: SDK.Domain.KeyTypes.EC,
curve:SDK.Domain.Curve.ED25519,
});

const peerDID = await castor.createPeerDID(
[authKeyPair, keyAgreementKeyPair],
[authPrivateKey.publicKey(), keyAgreementPrivateKey.publicKey()],
[exampleService]
);

Expand Down
Loading

0 comments on commit 61d9dbd

Please sign in to comment.