Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add import key logic #13

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file modified bun.lockb
Binary file not shown.
6 changes: 5 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@othent/kms",
"version": "1.0.8",
"version": "1.0.10",
"description": "Arweave Oauth Othent wallets enabled Google Key Management Service.",
"main": "dist/index.js",
"module": "dist/index.mjs",
Expand Down Expand Up @@ -39,12 +39,16 @@
"homepage": "https://othent.io",
"dependencies": {
"@auth0/auth0-spa-js": "^2.1.2",
"@types/node-forge": "^1.3.11",
"arweave": "^1.14.4",
"arweave-mnemonic-keys": "^0.0.9",
"axios": "^1.6.0",
"base64-js": "^1.5.1",
"buffer": "^6.0.3",
"crypto": "^1.0.1",
"jwk-to-pem": "^2.0.5",
"jwt-decode": "^4.0.0",
"node-forge": "^1.3.1",
"pem-jwk": "^2.0.0",
"tmp-promise": "^3.0.3",
"warp-arbundles": "^1.0.4"
Expand Down
12 changes: 11 additions & 1 deletion src/lib/mapping/connect.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { login } from "../auth/login";
import { createUser } from "../operations/createUser";
import { createUserWithKey } from "../operations/createUserWithKey";
import { getJWT } from "../auth/getJWT";
import { userDetails } from "../auth/userDetails";
import { UserDetailsReturnProps } from "../../types/auth/userDetails";
Expand All @@ -8,11 +9,20 @@ import { UserDetailsReturnProps } from "../../types/auth/userDetails";
* Connect the users account, this is the same as login/signup in one function.
* @returns The the users details.
*/
export async function connect(): Promise<UserDetailsReturnProps> {
export async function connect(
seedPhrase?: boolean,
): Promise<UserDetailsReturnProps> {
const user = await login();

if (user.authSystem === "KMS" && user.owner && user.walletAddress) {
return user;
} else if (seedPhrase) {
const mnemonicSeedPhrase = await createUserWithKey();
const userDetailsJWT = await getJWT();
localStorage.setItem("id_token", JSON.stringify(userDetailsJWT));
const userInfo = await userDetails();
userInfo.mnemonicSeedPhrase = mnemonicSeedPhrase;
return userInfo;
} else {
await createUser();
const userDetailsJWT = await getJWT();
Expand Down
30 changes: 30 additions & 0 deletions src/lib/operations/createUserWithKey.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import { api } from "./api";
import { encodeToken } from "../auth/encodeToken";
import { generateKey, formatKey, encryptKey } from "../utils/kmsUtils";

export async function createUserWithKey(): Promise<any> {
// TODO remove walletAddress later
const { mnemonic, JWK, walletAddress } = await generateKey();

const formattedKey = formatKey(JWK);

const pem = "we need to ask the server here for import job key";

const encryptedKey = encryptKey(pem, formattedKey);

const encodedData = await encodeToken({ importedKey: encryptedKey });

try {
const createUserRequest = (
await api.post("/create-user", { encodedData })
).data.data;

if (!createUserRequest) {
throw new Error("Error creating user with key on server.");
}

return mnemonic;
} catch (e) {
throw new Error("Error creating user with key on server.");
}
}
69 changes: 69 additions & 0 deletions src/lib/utils/kmsUtils.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
// import { generateMnemonic, getKeyFromMnemonic } from 'arweave-mnemonic-keys'
import { ownerToAddress } from "./arweaveUtils";
import JWktoPem from "jwk-to-pem";
import forge from "node-forge";
import crypto from "crypto";

interface JWKInterface {
kty: string;
e: string;
n: string;
d?: string | undefined;
p?: string | undefined;
q?: string | undefined;
dp?: string | undefined;
dq?: string | undefined;
qi?: string | undefined;
}

export async function generateKey(): Promise<{
mnemonic: string;
JWK: JWKInterface;
walletAddress: string;
}> {
// const mnemonic = await generateMnemonic() // while testing
// const JWK = await getKeyFromMnemonic(mnemonic) // while testing

const mnemonic =
"crash buffalo kit mule arena try soup custom round enter enforce nasty";

const JWK = {
kty: "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kXhkK5G7NeaCC4tyADw3SF53eN-jAZzCGoriKaE8vBvfML9AohzzyfWLRW4X4-hdh3H1eXDCH8lMpTo24xdlOZIRYZ7fphZRluGzQusaAltxXvenA3Sv0JF4RKc4xFaN5qBl4_oXF62CfQcDoU43aCbqeAdVCYMu5Dom62UpRUcYz6N66ZVZpozl3y2uSeaLWoBPKoWWOrJv4d6RwC6luyfBcA4kBF1BrHJ0qOSMN7CGtHyu4p8mUGA6d2jbPwyj6Esje-RIH3GsinZ1Q",
dq: "AfdJABYxEFvpDKk_jjzZqUp7m2Mqxk1ZxtocPCyI9Qmq57nSDvG4lg_VvVTHWATn5ODfzTljf6gxlqqKMVoMu10CGRbesTuThQvfQ3ErBCF7IArlcCNgZPbz31A76ie1HwgvH7EYUFzmxig8h02pOSN33fooAlUMJZFLEQW6U8sV198B5uh4SuBRHOB5c1ik5MYmBygzbm0W6dhN2CTXHKukuRvFvGePvKjbf440wpUzjJKMtDLyrxwQFhleTIr5PR15FHeQSH98_UdKrwyN6lLAp2CuR8CvPPtYJghywTVI0Kqf0c9h9Y9x0HsHu4yHX_6SX49pyvSZLo6yDzu6Bw",
qi: "DXqJxy0a7Yriv8JxytDLToL_4_Q-XE035dbHFPrcbpT9PNXP0uvJ7NM66GfigBuHIcobfZfyIXmmRlVDtKZ-DD0lsOSLjNOItZsqsA7jyRyUK9ZXucWO_TflQuOvi17LOgRHAyKuBPNGjZ4fHvnwkt6O7cvCtMtfSGnpeQ1Lmh2Np_ZjXln8E3s2iH-0aL4y_HsBFs91frOzTgp9Y7yxko0EXnxtWC_31VuuqdlcMRIOXKAf9C8_Bg1xcQS-uxT2pnI-wClR27lMnC-sLLIW1Th6I1FxMFx526TF2Urd_RayRawyydswk_gj3k9CUgrDuN0jrTenGAZ67wqIQ660OA",
};

const walletAddress = await ownerToAddress(JWK.n);

return { mnemonic, JWK, walletAddress };
}

export function formatKey(JWK: any) {
const pemKey = JWktoPem(JWK, { private: true });

const privateKey = forge.pki.privateKeyFromPem(pemKey);
const der = forge.asn1
.toDer(forge.pki.privateKeyToAsn1(privateKey))
.getBytes();

const derBuffer = Buffer.from(der, "binary");

return derBuffer;
}

export function encryptKey(jobPem: any, targetKey: any) {
return crypto.publicEncrypt(
{
key: jobPem,
oaepHash: "sha256",
padding: crypto.constants.RSA_PKCS1_OAEP_PADDING,
},
targetKey,
);
}
1 change: 1 addition & 0 deletions src/types/auth/userDetails.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,5 @@ export interface UserDetailsReturnProps {
sid?: string;
nonce?: string;
data?: any;
mnemonicSeedPhrase?: any;
}