@openentity/sdk 0.3.0 → 0.4.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/README.md +2 -0
- package/dist/chunk-IWY5EQIP.js +2012 -0
- package/dist/index.d.ts +152 -1
- package/dist/index.js +72 -1778
- package/dist/node.d.ts +15 -0
- package/dist/node.js +52 -0
- package/package.json +6 -2
package/dist/node.d.ts
ADDED
|
@@ -0,0 +1,15 @@
|
|
|
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 entity's key from the OS keychain, else `~/.openentity/identity.key` (mode 0600). A fresh key goes to the keychain
|
|
10
|
+
* when one answers, otherwise to the file. OPENENTITY_KEYCHAIN=off uses the file only. Same key file as the Python SDK. */
|
|
11
|
+
declare function loadOrCreate(): Promise<Identity>;
|
|
12
|
+
export declare const Identity: {
|
|
13
|
+
loadOrCreate: typeof loadOrCreate;
|
|
14
|
+
};
|
|
15
|
+
export {};
|
package/dist/node.js
ADDED
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
import {
|
|
2
|
+
keyPairFromSeed
|
|
3
|
+
} from "./chunk-IWY5EQIP.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
|
+
async function loadOrCreate() {
|
|
26
|
+
const make = async (seedHex2, source) => {
|
|
27
|
+
const seed = unhex(seedHex2), kp = await keyPairFromSeed(seed);
|
|
28
|
+
return { kp, did: kp.did, seed, source };
|
|
29
|
+
};
|
|
30
|
+
const file = identityFile();
|
|
31
|
+
const fromKeychain = keychainGet();
|
|
32
|
+
if (fromKeychain) return make(fromKeychain, "keychain");
|
|
33
|
+
if (existsSync(file)) return make(readFileSync(file, "utf8").trim(), "file");
|
|
34
|
+
const seedHex = hex(crypto.getRandomValues(new Uint8Array(32)));
|
|
35
|
+
if (keychainSet(seedHex)) return make(seedHex, "keychain");
|
|
36
|
+
mkdirSync(dirname(file), { recursive: true, mode: 448 });
|
|
37
|
+
try {
|
|
38
|
+
const fd = openSync(file, constants.O_WRONLY | constants.O_CREAT | constants.O_EXCL, 384);
|
|
39
|
+
writeSync(fd, seedHex);
|
|
40
|
+
closeSync(fd);
|
|
41
|
+
} catch (e) {
|
|
42
|
+
if (e.code !== "EEXIST") throw e;
|
|
43
|
+
return make(readFileSync(file, "utf8").trim(), "file");
|
|
44
|
+
}
|
|
45
|
+
chmodSync(file, 384);
|
|
46
|
+
return make(seedHex, "file");
|
|
47
|
+
}
|
|
48
|
+
var Identity = { loadOrCreate };
|
|
49
|
+
export {
|
|
50
|
+
Identity,
|
|
51
|
+
identityFile
|
|
52
|
+
};
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@openentity/sdk",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.4.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",
|