Skip to content

Commit

Permalink
add generate key response and basic key generation tests
Browse files Browse the repository at this point in the history
  • Loading branch information
finn-tbd committed Oct 20, 2023
1 parent d2e7dc7 commit a232d0f
Show file tree
Hide file tree
Showing 9 changed files with 1,145 additions and 3 deletions.
28 changes: 28 additions & 0 deletions openapi.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -68,12 +68,30 @@ paths:
/crypto/generate-key/secp256k1:
post:
operationId: crypto_generate_key_secp256k1
responses:
"200":
content:
application/json:
schema:
$ref: "#/components/schemas/GenerateKeyResponse"
/crypto/generate-key/ed25519:
post:
operationId: crypto_generate_key_ed25519
responses:
"200":
content:
application/json:
schema:
$ref: "#/components/schemas/GenerateKeyResponse"
/crypto/generate-key/secp256r1:
post:
operationId: crypto_generate_key_secp256r1
responses:
"200":
content:
application/json:
schema:
$ref: "#/components/schemas/GenerateKeyResponse"
/crypto/verify/secp256k1:
post:
operationId: crypto_verify_secp256k1
Expand Down Expand Up @@ -601,3 +619,13 @@ components:
directive:
type: string
enum: [required, allowed, disallowed]
GenerateKeyResponse:
type: object
required:
- private
- public
properties:
private:
type: string
public:
type: string
39 changes: 39 additions & 0 deletions openapi/openapi.go

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

44 changes: 44 additions & 0 deletions sdks/web5-js/crypto.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
import { Request, Response } from "express";
import { LocalKms, KeyManager, Web5ManagedAgent } from "@web5/agent";
import { paths } from "./openapi.js";
import { Convert } from "@web5/common";

// @ts-ignore
const mockAgent: Web5ManagedAgent = {};
const localKms = new LocalKms({ kmsName: "memory", agent: mockAgent });
const keyManager = new KeyManager({
kms: { memory: localKms },
agent: mockAgent,
});

export async function cryptoSecp256k1Generate(req: Request, res: Response) {
const key = await keyManager.generateKey({
kms: "memory",
algorithm: { name: "ECDSA", namedCurve: "secp256k1" },
keyUsages: ["sign", "verify"],
});

const resp: paths["/crypto/generate-key/secp256k1"]["post"]["responses"]["200"]["content"]["application/json"] =
{
public: Convert.uint8Array(key.publicKey.material!).toBase64Url(),
private: null, //Convert.uint8Array(key.privateKey.material!).toBase64Url(), // private key material here is null?
};

res.json(resp);
}

export async function cryptoEd25519Generate(req: Request, res: Response) {
const keyPair = await keyManager.generateKey({
kms: "memory",
algorithm: { name: "EdDSA", namedCurve: "Ed25519" },
keyUsages: ["sign", "verify"],
});

const resp: paths["/crypto/generate-key/ed25519"]["post"]["responses"]["200"]["content"]["application/json"] =
{
public: Convert.uint8Array(keyPair.publicKey.material!).toBase64Url(),
private: null, //Convert.uint8Array(keyPair.privateKey.material!).toBase64Url(),
};

res.json(resp);
}
12 changes: 11 additions & 1 deletion sdks/web5-js/main.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,14 @@
import express from "express";
import { credentialIssue } from "./credentials.js";
import { didIonCreate } from "./did-ion.js";
import { encoderBase58Decode, encoderBase58Encode, encoderBase64Decode, encoderBase64Encode, encoderSha256Encode } from "./encoders.js"
import {
encoderBase58Decode,
encoderBase58Encode,
encoderBase64Decode,
encoderBase64Encode,
encoderSha256Encode,
} from "./encoders.js";
import { cryptoSecp256k1Generate, cryptoEd25519Generate } from "./crypto.js";
import type * as http from "http";
import type { Request, Response } from "express";
import { paths } from "./openapi.js"; // generated with npx openapi-typescript .web5-component/openapi.yaml -o .web5-component/openapi.d.ts
Expand All @@ -15,6 +22,9 @@ app.post("/did-ion/create", didIonCreate);

app.post("/credentials/issue", credentialIssue);

app.post("/crypto/generate-key/secp256k1", cryptoSecp256k1Generate);
app.post("/crypto/generate-key/ed25519", cryptoEd25519Generate);

app.post("/encoders/base64/encode", encoderBase64Encode);
app.post("/encoders/base64/decode", encoderBase64Decode);
// app.post("/encoders/base58/encode", encoderBase58Encode);
Expand Down
25 changes: 25 additions & 0 deletions sdks/web5-js/openapi.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -265,6 +265,10 @@ export interface components {
/** @enum {string} */
directive?: "required" | "allowed" | "disallowed";
};
GenerateKeyResponse: {
private: string;
public: string;
};
};
responses: never;
parameters: never;
Expand Down Expand Up @@ -333,10 +337,31 @@ export interface operations {
};
};
crypto_generate_key_secp256k1: {
responses: {
200: {
content: {
"application/json": components["schemas"]["GenerateKeyResponse"];
};
};
};
};
crypto_generate_key_ed25519: {
responses: {
200: {
content: {
"application/json": components["schemas"]["GenerateKeyResponse"];
};
};
};
};
crypto_generate_key_secp256r1: {
responses: {
200: {
content: {
"application/json": components["schemas"]["GenerateKeyResponse"];
};
};
};
};
crypto_verify_secp256k1: {
};
Expand Down
Loading

0 comments on commit a232d0f

Please sign in to comment.