@openentity/sdk 0.3.0 → 0.5.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +0 -3
- package/README.md +4 -0
- package/dist/chunk-BYNZITFU.js +2093 -0
- package/dist/index.d.ts +183 -1
- package/dist/index.js +81 -1779
- package/dist/node.d.ts +16 -0
- package/dist/node.js +56 -0
- package/package.json +6 -2
package/dist/node.d.ts
ADDED
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import { type KeyPair } from "./index.ts";
|
|
2
|
+
export declare const identityFile: () => string;
|
|
3
|
+
export interface Identity {
|
|
4
|
+
kp: KeyPair;
|
|
5
|
+
did: string;
|
|
6
|
+
seed: Uint8Array;
|
|
7
|
+
source: "keychain" | "file";
|
|
8
|
+
}
|
|
9
|
+
/** The existing identity, or null. Never creates one. */
|
|
10
|
+
declare function load(): Promise<Identity | null>;
|
|
11
|
+
declare function loadOrCreate(): Promise<Identity>;
|
|
12
|
+
export declare const Identity: {
|
|
13
|
+
load: typeof load;
|
|
14
|
+
loadOrCreate: typeof loadOrCreate;
|
|
15
|
+
};
|
|
16
|
+
export {};
|
package/dist/node.js
ADDED
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
import {
|
|
2
|
+
keyPairFromSeed
|
|
3
|
+
} from "./chunk-BYNZITFU.js";
|
|
4
|
+
|
|
5
|
+
// src/node.ts
|
|
6
|
+
import { execFileSync } from "node:child_process";
|
|
7
|
+
import { chmodSync, existsSync, mkdirSync, openSync, readFileSync, writeSync, closeSync, constants } from "node:fs";
|
|
8
|
+
import { homedir } from "node:os";
|
|
9
|
+
import { dirname, join } from "node:path";
|
|
10
|
+
var SERVICE = "openentity";
|
|
11
|
+
var ACCOUNT = "identity";
|
|
12
|
+
var run = (cmd, args, input) => {
|
|
13
|
+
try {
|
|
14
|
+
return execFileSync(cmd, args, { input, encoding: "utf8", timeout: 1e4, stdio: ["pipe", "pipe", "ignore"] }).trim();
|
|
15
|
+
} catch {
|
|
16
|
+
return null;
|
|
17
|
+
}
|
|
18
|
+
};
|
|
19
|
+
var keychainOn = () => process.env.OPENENTITY_KEYCHAIN !== "off";
|
|
20
|
+
var keychainGet = () => !keychainOn() ? null : process.platform === "darwin" ? run("security", ["find-generic-password", "-s", SERVICE, "-a", ACCOUNT, "-w"]) : process.platform === "linux" ? run("secret-tool", ["lookup", "service", SERVICE, "account", ACCOUNT]) : null;
|
|
21
|
+
var keychainSet = (seedHex) => !keychainOn() ? false : process.platform === "darwin" ? run("security", ["add-generic-password", "-U", "-s", SERVICE, "-a", ACCOUNT, "-w", seedHex]) !== null : process.platform === "linux" ? run("secret-tool", ["store", "--label=Open Entity key", "service", SERVICE, "account", ACCOUNT], seedHex) !== null : false;
|
|
22
|
+
var identityFile = () => join(process.env.OPENENTITY_HOME || join(homedir(), ".openentity"), "identity.key");
|
|
23
|
+
var unhex = (h) => Uint8Array.from(h.match(/../g), (x) => parseInt(x, 16));
|
|
24
|
+
var hex = (b) => Array.from(b, (x) => x.toString(16).padStart(2, "0")).join("");
|
|
25
|
+
var make = async (seedHex, source) => {
|
|
26
|
+
const seed = unhex(seedHex), kp = await keyPairFromSeed(seed);
|
|
27
|
+
return { kp, did: kp.did, seed, source };
|
|
28
|
+
};
|
|
29
|
+
async function load() {
|
|
30
|
+
const fromKeychain = keychainGet();
|
|
31
|
+
if (fromKeychain) return make(fromKeychain, "keychain");
|
|
32
|
+
return existsSync(identityFile()) ? make(readFileSync(identityFile(), "utf8").trim(), "file") : null;
|
|
33
|
+
}
|
|
34
|
+
async function loadOrCreate() {
|
|
35
|
+
const found = await load();
|
|
36
|
+
if (found) return found;
|
|
37
|
+
const file = identityFile();
|
|
38
|
+
const seedHex = hex(crypto.getRandomValues(new Uint8Array(32)));
|
|
39
|
+
if (keychainSet(seedHex)) return make(seedHex, "keychain");
|
|
40
|
+
mkdirSync(dirname(file), { recursive: true, mode: 448 });
|
|
41
|
+
try {
|
|
42
|
+
const fd = openSync(file, constants.O_WRONLY | constants.O_CREAT | constants.O_EXCL, 384);
|
|
43
|
+
writeSync(fd, seedHex);
|
|
44
|
+
closeSync(fd);
|
|
45
|
+
} catch (e) {
|
|
46
|
+
if (e.code !== "EEXIST") throw e;
|
|
47
|
+
return make(readFileSync(file, "utf8").trim(), "file");
|
|
48
|
+
}
|
|
49
|
+
chmodSync(file, 384);
|
|
50
|
+
return make(seedHex, "file");
|
|
51
|
+
}
|
|
52
|
+
var Identity = { load, loadOrCreate };
|
|
53
|
+
export {
|
|
54
|
+
Identity,
|
|
55
|
+
identityFile
|
|
56
|
+
};
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@openentity/sdk",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.5.1",
|
|
4
4
|
"description": "Reference TypeScript SDK for the Open Entity Identity Standard (OEIS-1)",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"type": "module",
|
|
@@ -8,6 +8,10 @@
|
|
|
8
8
|
".": {
|
|
9
9
|
"types": "./dist/index.d.ts",
|
|
10
10
|
"default": "./dist/index.js"
|
|
11
|
+
},
|
|
12
|
+
"./node": {
|
|
13
|
+
"types": "./dist/node.d.ts",
|
|
14
|
+
"default": "./dist/node.js"
|
|
11
15
|
}
|
|
12
16
|
},
|
|
13
17
|
"dependencies": {
|
|
@@ -40,7 +44,7 @@
|
|
|
40
44
|
"access": "public"
|
|
41
45
|
},
|
|
42
46
|
"scripts": {
|
|
43
|
-
"build": "esbuild src/index.ts --bundle --packages=external --format=esm --platform=neutral --target=es2022 --
|
|
47
|
+
"build": "esbuild src/index.ts src/node.ts --bundle --packages=external --format=esm --platform=neutral --target=es2022 --splitting --outdir=dist && tsc -p tsconfig.build.json",
|
|
44
48
|
"lint": "tsc --noEmit",
|
|
45
49
|
"typecheck": "tsc --noEmit",
|
|
46
50
|
"test": "vitest run",
|