@learncard/didkey-plugin 1.1.46 → 1.2.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.
@@ -0,0 +1,9 @@
1
+ /**
2
+ * Derives the Ed25519 `did:key` for a seed without DIDKit (no WASM, no wallet).
3
+ *
4
+ * Same seed handling and result as `getDidKeyPlugin(learnCard, key, 'key')`'s `did()`:
5
+ * the 32 seed bytes are the Ed25519 secret, and the DID is
6
+ * `did:key:z` + base58btc(0xed01 ‖ publicKey).
7
+ */
8
+ export declare const getEd25519DidKeyFromSeed: (key: string) => Promise<string>;
9
+ //# sourceMappingURL=didKeyFromSeed.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"didKeyFromSeed.d.ts","sourceRoot":"","sources":["../src/didKeyFromSeed.ts"],"names":[],"mappings":"AAQA;;;;;;GAMG;AACH,eAAO,MAAM,wBAAwB,GAAU,KAAK,MAAM,KAAG,OAAO,CAAC,MAAM,CAS1E,CAAC"}
@@ -21,26 +21,11 @@ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: tru
21
21
  // src/index.ts
22
22
  var index_exports = {};
23
23
  __export(index_exports, {
24
- getDidKeyPlugin: () => getDidKeyPlugin
24
+ getDidKeyPlugin: () => getDidKeyPlugin,
25
+ getEd25519DidKeyFromSeed: () => getEd25519DidKeyFromSeed
25
26
  });
26
27
  module.exports = __toCommonJS(index_exports);
27
28
 
28
- // ../../../node_modules/hex-lite/dist/hex-lite.mjs
29
- function toUint8Array(str) {
30
- var s = 0, sl = str.length, bytes = [];
31
- if (sl % 2) {
32
- throw new Error("invalid hex:" + str);
33
- }
34
- for (; s < sl; s += 2) {
35
- bytes.push(parseInt(str.substr(s, 2), 16));
36
- }
37
- return new Uint8Array(bytes);
38
- }
39
- __name(toUint8Array, "toUint8Array");
40
-
41
- // src/index.ts
42
- var import_helpers = require("@learncard/helpers");
43
-
44
29
  // src/helpers.ts
45
30
  var ED25519_METHODS = ["key", "tz", "pkh:tz", "pkh:tezos", "pkh:sol", "pkh:solana"];
46
31
  var SECP256K1_METHODS = [
@@ -65,9 +50,23 @@ var getAlgorithmForDidMethod = /* @__PURE__ */ __name((didMethod) => {
65
50
  throw new Error("Unspported Did Method");
66
51
  }, "getAlgorithmForDidMethod");
67
52
 
68
- // src/index.ts
53
+ // ../../../node_modules/hex-lite/dist/hex-lite.mjs
54
+ function toUint8Array(str) {
55
+ var s = 0, sl = str.length, bytes = [];
56
+ if (sl % 2) {
57
+ throw new Error("invalid hex:" + str);
58
+ }
59
+ for (; s < sl; s += 2) {
60
+ bytes.push(parseInt(str.substr(s, 2), 16));
61
+ }
62
+ return new Uint8Array(bytes);
63
+ }
64
+ __name(toUint8Array, "toUint8Array");
65
+
66
+ // src/seed.ts
67
+ var import_helpers = require("@learncard/helpers");
69
68
  var _shortSeedWarned = false;
70
- var getDidKeyPlugin = /* @__PURE__ */ __name(async (learnCard, key, defaultDidMethod) => {
69
+ var didKeySeedToBytes = /* @__PURE__ */ __name((key) => {
71
70
  if (key.length === 0) throw new Error("Please don't use an empty string for a key!");
72
71
  if (!(0, import_helpers.isHex)(key)) throw new Error("Key must be a hexadecimal string!");
73
72
  if (key.length > 64) throw new Error("Key must be less than 64 characters");
@@ -84,23 +83,44 @@ var getDidKeyPlugin = /* @__PURE__ */ __name(async (learnCard, key, defaultDidMe
84
83
  );
85
84
  }
86
85
  const seed = key.padStart(64, "0");
87
- const seedBytes = toUint8Array(seed);
86
+ return { seed, seedBytes: toUint8Array(seed) };
87
+ }, "didKeySeedToBytes");
88
+
89
+ // src/didKeyFromSeed.ts
90
+ var import_ed25519 = require("@noble/ed25519");
91
+ var import_base = require("@scure/base");
92
+ var ED25519_PUB_MULTICODEC = [237, 1];
93
+ var getEd25519DidKeyFromSeed = /* @__PURE__ */ __name(async (key) => {
94
+ const { seedBytes } = didKeySeedToBytes(key);
95
+ const publicKey = await (0, import_ed25519.getPublicKeyAsync)(seedBytes);
96
+ const multicodecKey = new Uint8Array(ED25519_PUB_MULTICODEC.length + publicKey.length);
97
+ multicodecKey.set(ED25519_PUB_MULTICODEC);
98
+ multicodecKey.set(publicKey, ED25519_PUB_MULTICODEC.length);
99
+ return `did:key:z${import_base.base58.encode(multicodecKey)}`;
100
+ }, "getEd25519DidKeyFromSeed");
101
+
102
+ // src/index.ts
103
+ var getDidKeyPlugin = /* @__PURE__ */ __name(async (learnCard, key, defaultDidMethod) => {
104
+ const { seed, seedBytes } = didKeySeedToBytes(key);
88
105
  const memoizedDids = {};
89
106
  const keyPairs = {
90
- ed25519: learnCard.invoke.generateEd25519KeyFromBytes(seedBytes),
91
- secp256k1: learnCard.invoke.generateSecp256k1KeyFromBytes(seedBytes)
107
+ ed25519: learnCard.invoke.generateEd25519KeyFromBytes(seedBytes)
92
108
  };
109
+ const keypair = /* @__PURE__ */ __name((algorithm = "ed25519") => {
110
+ if (algorithm === "secp256k1") {
111
+ keyPairs.secp256k1 ?? (keyPairs.secp256k1 = learnCard.invoke.generateSecp256k1KeyFromBytes(seedBytes));
112
+ }
113
+ const pair = keyPairs[algorithm];
114
+ if (!pair) throw new Error("Unsupported algorithm");
115
+ return pair;
116
+ }, "keypair");
93
117
  const did = /* @__PURE__ */ __name((method = defaultDidMethod) => {
94
118
  if (!memoizedDids[method]) {
95
119
  const algorithm = getAlgorithmForDidMethod(method);
96
- memoizedDids[method] = learnCard.invoke.keyToDid(method, keyPairs[algorithm]);
120
+ memoizedDids[method] = learnCard.invoke.keyToDid(method, keypair(algorithm));
97
121
  }
98
122
  return memoizedDids[method];
99
123
  }, "did");
100
- const keypair = /* @__PURE__ */ __name((algorithm = "ed25519") => {
101
- if (!keyPairs[algorithm]) throw new Error("Unsupported algorithm");
102
- return keyPairs[algorithm];
103
- }, "keypair");
104
124
  return {
105
125
  name: "DID Key",
106
126
  displayName: "DID Key",
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "version": 3,
3
- "sources": ["../src/index.ts", "../../../../node_modules/hex-lite/dist/hex-lite.mjs", "../src/helpers.ts"],
4
- "sourcesContent": ["import { toUint8Array } from 'hex-lite';\nimport { isHex } from '@learncard/helpers';\nimport { JWKWithPrivateKey } from '@learncard/types';\nimport { LearnCard } from '@learncard/core';\n\nimport { getAlgorithmForDidMethod } from './helpers';\n\nimport { DidKeyPlugin, DependentMethods, Algorithm } from './types';\n\nexport * from './types';\n\nlet _shortSeedWarned = false;\n\n/**\n *\n * @group Plugins\n */\nexport const getDidKeyPlugin = async <DidMethod extends string>(\n learnCard: LearnCard<any, any, DependentMethods<DidMethod>>,\n key: string,\n defaultDidMethod: DidMethod\n): Promise<DidKeyPlugin<DidMethod>> => {\n if (key.length === 0) throw new Error(\"Please don't use an empty string for a key!\");\n if (!isHex(key)) throw new Error('Key must be a hexadecimal string!');\n if (key.length > 64) throw new Error('Key must be less than 64 characters');\n\n if (key.length < 64 && !_shortSeedWarned) {\n _shortSeedWarned = true;\n\n console.warn(\n 'Warning: A LearnCard has been initialized with a seed that is less than 32 bytes, and will be padded with zeroes'\n );\n console.warn(\n 'Please instantiate LearnCards using 32 bytes that have been randomly generated in a cryptographically secure fashion!'\n );\n console.warn(\n 'See https://app.gitbook.com/o/6uDv1QDlxaaZC7i8EaGb/s/FXvEJ9j3Vf3FW5Nc557n/learn-card-packages/learncard-core/instantiation#key-generation for details'\n );\n }\n\n const seed = key.padStart(64, '0');\n const seedBytes = toUint8Array(seed);\n\n const memoizedDids: Partial<Record<DidMethod, string>> = {};\n const keyPairs: Record<Algorithm, JWKWithPrivateKey> = {\n ed25519: learnCard.invoke.generateEd25519KeyFromBytes(seedBytes),\n secp256k1: learnCard.invoke.generateSecp256k1KeyFromBytes(seedBytes),\n };\n\n const did = (method = defaultDidMethod) => {\n if (!memoizedDids[method]) {\n const algorithm = getAlgorithmForDidMethod(method);\n memoizedDids[method] = learnCard.invoke.keyToDid(method, keyPairs[algorithm]);\n }\n\n return memoizedDids[method]!;\n };\n\n const keypair = (algorithm: Algorithm = 'ed25519') => {\n if (!keyPairs[algorithm]) throw new Error('Unsupported algorithm');\n\n return keyPairs[algorithm];\n };\n\n return {\n name: 'DID Key',\n displayName: 'DID Key',\n description: 'Generates dids and JWKs using 32 Secure Random Bytes of Entropy',\n id: {\n did: (_learnCard, method) => did(method as any),\n keypair: (_learnCard, algorithm) => keypair(algorithm as any),\n },\n methods: {\n getSubjectDid: (_learnCard, method = defaultDidMethod) => did(method),\n getSubjectKeypair: (_learnCard, algorithm = 'ed25519') => keypair(algorithm),\n getKey: () => seed,\n },\n };\n};\n", "function fromUint8Array(uint8Array) {\n return uint8Array.reduce(function (ag, v) { return ag += ('00' + v.toString(16)).slice(-2); }, '');\n}\n\nfunction toUint8Array(str) {\n var s = 0, sl = str.length, bytes = [];\n if (sl % 2) \n { throw new Error('invalid hex:' + str); }\n for (; s < sl; s += 2) {\n bytes.push(parseInt(str.substr(s, 2), 16));\n }\n return new Uint8Array(bytes);\n}\n\nfunction fromBuffer(buffer) {\n return fromUint8Array(new Uint8Array(buffer));\n}\n\nfunction toBuffer(str) {\n return toUint8Array(str).buffer;\n}\n\nexport { fromUint8Array, toUint8Array, fromBuffer, toBuffer };\n//# sourceMappingURL=hex-lite.mjs.map\n", "import { Algorithm } from './types';\n\nexport const ED25519_METHODS = ['key', 'tz', 'pkh:tz', 'pkh:tezos', 'pkh:sol', 'pkh:solana'];\n\nexport const SECP256K1_METHODS = [\n 'key',\n 'tz',\n 'ethr',\n 'pkh:tz',\n 'pkh:tezos',\n 'pkh:eth',\n 'pkh:celo',\n 'pkh:poly',\n 'pkh:btc',\n 'pkh:doge',\n 'pkh:eip155',\n 'pkh:bip122',\n];\n\nexport const getAlgorithmForDidMethod = <DidMethod extends string>(\n didMethod: DidMethod\n): Algorithm => {\n if (ED25519_METHODS.includes(didMethod)) return 'ed25519';\n if (\n SECP256K1_METHODS.includes(didMethod) ||\n didMethod.startsWith('pkh:eip155:') ||\n didMethod.startsWith('pkh:bip122:')\n ) {\n return 'secp256k1';\n }\n\n throw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Q;AAAA,MACJ;AAAA,IACJ;AACA,YAAQ;AAAA,MACJ;AAAA,IACJ;AAAA,EACJ;AAEA,QAAM,OAAO,IAAI,SAAS,IAAI,GAAG;AACjC,QAAM,YAAY,aAAa,IAAI;AAEnC,QAAM,eAAmD,CAAC;AAC1D,QAAM,WAAiD;AAAA,IACnD,SAAS,UAAU,OAAO,4BAA4B,SAAS;AAAA,IAC/D,WAAW,UAAU,OAAO,8BAA8B,SAAS;AAAA,EACvE;AAEA,QAAM,MAAM,wBAAC,SAAS,qBAAqB;AACvC,QAAI,CAAC,aAAa,MAAM,GAAG;AACvB,YAAM,YAAY,yBAAyB,MAAM;AACjD,mBAAa,MAAM,IAAI,UAAU,OAAO,SAAS,QAAQ,SAAS,SAAS,CAAC;AAAA,IAChF;AAEA,WAAO,aAAa,MAAM;AAAA,EAC9B,GAPY;AASZ,QAAM,UAAU,wBAAC,YAAuB,cAAc;AAClD,QAAI,CAAC,SAAS,SAAS,EAAG,OAAM,IAAI,MAAM,uBAAuB;AAEjE,WAAO,SAAS,SAAS;AAAA,EAC7B,GAJgB;AAMhB,SAAO;AAAA,IACH,MAAM;AAAA,IACN,aAAa;AAAA,IACb,aAAa;AAAA,IACb,IAAI;AAAA,MACA,KAAK,wBAAC,YAAY,WAAW,IAAI,MAAa,GAAzC;AAAA,MACL,SAAS,wBAAC,YAAY,cAAc,QAAQ,SAAgB,GAAnD;AAAA,IACb;AAAA,IACA,SAAS;AAAA,MACL,eAAe,wBAAC,YAAY,SAAS,qBAAqB,IAAI,MAAM,GAArD;AAAA,MACf,mBAAmB,wBAAC,YAAY,YAAY,cAAc,QAAQ,SAAS,GAAxD;AAAA,MACnB,QAAQ,6BAAM,MAAN;AAAA,IACZ;AAAA,EACJ;AACJ,GA7D+B;",
3
+ "sources": ["../src/index.ts", "../src/helpers.ts", "../../../../node_modules/hex-lite/dist/hex-lite.mjs", "../src/seed.ts", "../src/didKeyFromSeed.ts"],
4
+ "sourcesContent": ["import { JWKWithPrivateKey } from '@learncard/types';\nimport { LearnCard } from '@learncard/core';\n\nimport { getAlgorithmForDidMethod } from './helpers';\nimport { didKeySeedToBytes } from './seed';\n\nimport { DidKeyPlugin, DependentMethods, Algorithm } from './types';\n\nexport * from './types';\nexport { getEd25519DidKeyFromSeed } from './didKeyFromSeed';\n\n/**\n *\n * @group Plugins\n */\nexport const getDidKeyPlugin = async <DidMethod extends string>(\n learnCard: LearnCard<any, any, DependentMethods<DidMethod>>,\n key: string,\n defaultDidMethod: DidMethod\n): Promise<DidKeyPlugin<DidMethod>> => {\n const { seed, seedBytes } = didKeySeedToBytes(key);\n\n const memoizedDids: Partial<Record<DidMethod, string>> = {};\n // secp256k1 is derived on first use: did:key and most wallets only need ed25519.\n const keyPairs: Partial<Record<Algorithm, JWKWithPrivateKey>> = {\n ed25519: learnCard.invoke.generateEd25519KeyFromBytes(seedBytes),\n };\n\n const keypair = (algorithm: Algorithm = 'ed25519'): JWKWithPrivateKey => {\n if (algorithm === 'secp256k1') {\n keyPairs.secp256k1 ??= learnCard.invoke.generateSecp256k1KeyFromBytes(seedBytes);\n }\n\n const pair = keyPairs[algorithm];\n if (!pair) throw new Error('Unsupported algorithm');\n\n return pair;\n };\n\n const did = (method = defaultDidMethod) => {\n if (!memoizedDids[method]) {\n const algorithm = getAlgorithmForDidMethod(method);\n memoizedDids[method] = learnCard.invoke.keyToDid(method, keypair(algorithm));\n }\n\n return memoizedDids[method]!;\n };\n\n return {\n name: 'DID Key',\n displayName: 'DID Key',\n description: 'Generates dids and JWKs using 32 Secure Random Bytes of Entropy',\n id: {\n did: (_learnCard, method) => did(method as any),\n keypair: (_learnCard, algorithm) => keypair(algorithm as any),\n },\n methods: {\n getSubjectDid: (_learnCard, method = defaultDidMethod) => did(method),\n getSubjectKeypair: (_learnCard, algorithm = 'ed25519') => keypair(algorithm),\n getKey: () => seed,\n },\n };\n};\n", "import { Algorithm } from './types';\n\nexport const ED25519_METHODS = ['key', 'tz', 'pkh:tz', 'pkh:tezos', 'pkh:sol', 'pkh:solana'];\n\nexport const SECP256K1_METHODS = [\n 'key',\n 'tz',\n 'ethr',\n 'pkh:tz',\n 'pkh:tezos',\n 'pkh:eth',\n 'pkh:celo',\n 'pkh:poly',\n 'pkh:btc',\n 'pkh:doge',\n 'pkh:eip155',\n 'pkh:bip122',\n];\n\nexport const getAlgorithmForDidMethod = <DidMethod extends string>(\n didMethod: DidMethod\n): Algorithm => {\n if (ED25519_METHODS.includes(didMethod)) return 'ed25519';\n if (\n SECP256K1_METHODS.includes(didMethod) ||\n didMethod.startsWith('pkh:eip155:') ||\n didMethod.startsWith('pkh:bip122:')\n ) {\n return 'secp256k1';\n }\n\n throw new Error('Unspported Did Method');\n};\n", "function fromUint8Array(uint8Array) {\n return uint8Array.reduce(function (ag, v) { return ag += ('00' + v.toString(16)).slice(-2); }, '');\n}\n\nfunction toUint8Array(str) {\n var s = 0, sl = str.length, bytes = [];\n if (sl % 2) \n { throw new Error('invalid hex:' + str); }\n for (; s < sl; s += 2) {\n bytes.push(parseInt(str.substr(s, 2), 16));\n }\n return new Uint8Array(bytes);\n}\n\nfunction fromBuffer(buffer) {\n return fromUint8Array(new Uint8Array(buffer));\n}\n\nfunction toBuffer(str) {\n return toUint8Array(str).buffer;\n}\n\nexport { fromUint8Array, toUint8Array, fromBuffer, toBuffer };\n//# sourceMappingURL=hex-lite.mjs.map\n", "import { toUint8Array } from 'hex-lite';\nimport { isHex } from '@learncard/helpers';\n\nlet _shortSeedWarned = false;\n\n/**\n * Validates a hex key and turns it into the 32 seed bytes every did:key derivation uses.\n * Shorter keys are left-padded with zeroes (legacy keys can drop leading zeroes).\n */\nexport const didKeySeedToBytes = (key: string): { seed: string; seedBytes: Uint8Array } => {\n if (key.length === 0) throw new Error(\"Please don't use an empty string for a key!\");\n if (!isHex(key)) throw new Error('Key must be a hexadecimal string!');\n if (key.length > 64) throw new Error('Key must be less than 64 characters');\n\n if (key.length < 64 && !_shortSeedWarned) {\n _shortSeedWarned = true;\n\n console.warn(\n 'Warning: A LearnCard has been initialized with a seed that is less than 32 bytes, and will be padded with zeroes'\n );\n console.warn(\n 'Please instantiate LearnCards using 32 bytes that have been randomly generated in a cryptographically secure fashion!'\n );\n console.warn(\n 'See https://app.gitbook.com/o/6uDv1QDlxaaZC7i8EaGb/s/FXvEJ9j3Vf3FW5Nc557n/learn-card-packages/learncard-core/instantiation#key-generation for details'\n );\n }\n\n const seed = key.padStart(64, '0');\n\n return { seed, seedBytes: toUint8Array(seed) };\n};\n", "import { getPublicKeyAsync } from '@noble/ed25519';\nimport { base58 } from '@scure/base';\n\nimport { didKeySeedToBytes } from './seed';\n\n/** Multicodec varint prefix for an Ed25519 public key (0xed). */\nconst ED25519_PUB_MULTICODEC = [0xed, 0x01];\n\n/**\n * Derives the Ed25519 `did:key` for a seed without DIDKit (no WASM, no wallet).\n *\n * Same seed handling and result as `getDidKeyPlugin(learnCard, key, 'key')`'s `did()`:\n * the 32 seed bytes are the Ed25519 secret, and the DID is\n * `did:key:z` + base58btc(0xed01 \u2016 publicKey).\n */\nexport const getEd25519DidKeyFromSeed = async (key: string): Promise<string> => {\n const { seedBytes } = didKeySeedToBytes(key);\n const publicKey = await getPublicKeyAsync(seedBytes);\n\n const multicodecKey = new Uint8Array(ED25519_PUB_MULTICODEC.length + publicKey.length);\n multicodecKey.set(ED25519_PUB_MULTICODEC);\n multicodecKey.set(publicKey, ED25519_PUB_MULTICODEC.length);\n\n return `did:key:z${base58.encode(multicodecKey)}`;\n};\n"],
5
+ "mappings": ";;;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;;;ACEO,IAAM,kBAAkB,CAAC,OAAO,MAAM,UAAU,aAAa,WAAW,YAAY;AAEpF,IAAM,oBAAoB;AAAA,EAC7B;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AACJ;AAEO,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AAc,QAAQ,SAAS,GAAxD;AAAA,MACnB,QAAQ,6BAAM,MAAN;AAAA,IACZ;AAAA,EACJ;AACJ,GA/C+B;",
6
6
  "names": []
7
7
  }
@@ -1,2 +1,2 @@
1
- "use strict";var d=Object.defineProperty;var m=Object.getOwnPropertyDescriptor;var k=Object.getOwnPropertyNames;var D=Object.prototype.hasOwnProperty;var t=(e,r)=>d(e,"name",{value:r,configurable:!0});var w=(e,r)=>{for(var o in r)d(e,o,{get:r[o],enumerable:!0})},b=(e,r,o,a)=>{if(r&&typeof r=="object"||typeof r=="function")for(let s of k(r))!D.call(e,s)&&s!==o&&d(e,s,{get:()=>r[s],enumerable:!(a=m(r,s))||a.enumerable});return e};var K=e=>b(d({},"__esModule",{value:!0}),e);var P={};w(P,{getDidKeyPlugin:()=>x});module.exports=K(P);function y(e){var r=0,o=e.length,a=[];if(o%2)throw new Error("invalid hex:"+e);for(;r<o;r+=2)a.push(parseInt(e.substr(r,2),16));return new Uint8Array(a)}t(y,"toUint8Array");var f=require("@learncard/helpers");var E=["key","tz","pkh:tz","pkh:tezos","pkh:sol","pkh:solana"],A=["key","tz","ethr","pkh:tz","pkh:tezos","pkh:eth","pkh:celo","pkh:poly","pkh:btc","pkh:doge","pkh:eip155","pkh:bip122"],g=t(e=>{if(E.includes(e))return"ed25519";if(A.includes(e)||e.startsWith("pkh:eip155:")||e.startsWith("pkh:bip122:"))return"secp256k1";throw new Error("Unspported Did Method")},"getAlgorithmForDidMethod");var u=!1,x=t(async(e,r,o)=>{if(r.length===0)throw new Error("Please don't use an empty string for a key!");if(!(0,f.isHex)(r))throw new Error("Key must be a hexadecimal string!");if(r.length>64)throw new Error("Key must be less than 64 characters");r.length<64&&!u&&(u=!0,console.warn("Warning: A LearnCard has been initialized with a seed that is less than 32 bytes, and will be padded with zeroes"),console.warn("Please instantiate LearnCards using 32 bytes that have been randomly generated in a cryptographically secure fashion!"),console.warn("See https://app.gitbook.com/o/6uDv1QDlxaaZC7i8EaGb/s/FXvEJ9j3Vf3FW5Nc557n/learn-card-packages/learncard-core/instantiation#key-generation for details"));let a=r.padStart(64,"0"),s=y(a),h={},p={ed25519:e.invoke.generateEd25519KeyFromBytes(s),secp256k1:e.invoke.generateSecp256k1KeyFromBytes(s)},c=t((n=o)=>{if(!h[n]){let i=g(n);h[n]=e.invoke.keyToDid(n,p[i])}return h[n]},"did"),l=t((n="ed25519")=>{if(!p[n])throw new Error("Unsupported algorithm");return p[n]},"keypair");return{name:"DID Key",displayName:"DID Key",description:"Generates dids and JWKs using 32 Secure Random Bytes of Entropy",id:{did:t((n,i)=>c(i),"did"),keypair:t((n,i)=>l(i),"keypair")},methods:{getSubjectDid:t((n,i=o)=>c(i),"getSubjectDid"),getSubjectKeypair:t((n,i="ed25519")=>l(i),"getSubjectKeypair"),getKey:t(()=>a,"getKey")}}},"getDidKeyPlugin");
1
+ "use strict";var p=Object.defineProperty;var b=Object.getOwnPropertyDescriptor;var E=Object.getOwnPropertyNames;var x=Object.prototype.hasOwnProperty;var r=(e,t)=>p(e,"name",{value:t,configurable:!0});var A=(e,t)=>{for(var n in t)p(e,n,{get:t[n],enumerable:!0})},S=(e,t,n,s)=>{if(t&&typeof t=="object"||typeof t=="function")for(let a of E(t))!x.call(e,a)&&a!==n&&p(e,a,{get:()=>t[a],enumerable:!(s=b(t,a))||s.enumerable});return e};var P=e=>S(p({},"__esModule",{value:!0}),e);var M={};A(M,{getDidKeyPlugin:()=>v,getEd25519DidKeyFromSeed:()=>w});module.exports=P(M);var B=["key","tz","pkh:tz","pkh:tezos","pkh:sol","pkh:solana"],W=["key","tz","ethr","pkh:tz","pkh:tezos","pkh:eth","pkh:celo","pkh:poly","pkh:btc","pkh:doge","pkh:eip155","pkh:bip122"],m=r(e=>{if(B.includes(e))return"ed25519";if(W.includes(e)||e.startsWith("pkh:eip155:")||e.startsWith("pkh:bip122:"))return"secp256k1";throw new Error("Unspported Did Method")},"getAlgorithmForDidMethod");function f(e){var t=0,n=e.length,s=[];if(n%2)throw new Error("invalid hex:"+e);for(;t<n;t+=2)s.push(parseInt(e.substr(t,2),16));return new Uint8Array(s)}r(f,"toUint8Array");var D=require("@learncard/helpers");var u=!1,h=r(e=>{if(e.length===0)throw new Error("Please don't use an empty string for a key!");if(!(0,D.isHex)(e))throw new Error("Key must be a hexadecimal string!");if(e.length>64)throw new Error("Key must be less than 64 characters");e.length<64&&!u&&(u=!0,console.warn("Warning: A LearnCard has been initialized with a seed that is less than 32 bytes, and will be padded with zeroes"),console.warn("Please instantiate LearnCards using 32 bytes that have been randomly generated in a cryptographically secure fashion!"),console.warn("See https://app.gitbook.com/o/6uDv1QDlxaaZC7i8EaGb/s/FXvEJ9j3Vf3FW5Nc557n/learn-card-packages/learncard-core/instantiation#key-generation for details"));let t=e.padStart(64,"0");return{seed:t,seedBytes:f(t)}},"didKeySeedToBytes");var k=require("@noble/ed25519"),K=require("@scure/base");var l=[237,1],w=r(async e=>{let{seedBytes:t}=h(e),n=await(0,k.getPublicKeyAsync)(t),s=new Uint8Array(l.length+n.length);return s.set(l),s.set(n,l.length),`did:key:z${K.base58.encode(s)}`},"getEd25519DidKeyFromSeed");var v=r(async(e,t,n)=>{let{seed:s,seedBytes:a}=h(t),c={},d={ed25519:e.invoke.generateEd25519KeyFromBytes(a)},y=r((o="ed25519")=>{o==="secp256k1"&&(d.secp256k1??(d.secp256k1=e.invoke.generateSecp256k1KeyFromBytes(a)));let i=d[o];if(!i)throw new Error("Unsupported algorithm");return i},"keypair"),g=r((o=n)=>{if(!c[o]){let i=m(o);c[o]=e.invoke.keyToDid(o,y(i))}return c[o]},"did");return{name:"DID Key",displayName:"DID Key",description:"Generates dids and JWKs using 32 Secure Random Bytes of Entropy",id:{did:r((o,i)=>g(i),"did"),keypair:r((o,i)=>y(i),"keypair")},methods:{getSubjectDid:r((o,i=n)=>g(i),"getSubjectDid"),getSubjectKeypair:r((o,i="ed25519")=>y(i),"getSubjectKeypair"),getKey:r(()=>s,"getKey")}}},"getDidKeyPlugin");
2
2
  //# sourceMappingURL=didkey-plugin.cjs.production.min.cjs.map
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "version": 3,
3
- "sources": ["../src/index.ts", "../../../../node_modules/hex-lite/dist/hex-lite.mjs", "../src/helpers.ts"],
4
- "sourcesContent": ["import { toUint8Array } from 'hex-lite';\nimport { isHex } from '@learncard/helpers';\nimport { JWKWithPrivateKey } from '@learncard/types';\nimport { LearnCard } from '@learncard/core';\n\nimport { getAlgorithmForDidMethod } from './helpers';\n\nimport { DidKeyPlugin, DependentMethods, Algorithm } from './types';\n\nexport * from './types';\n\nlet _shortSeedWarned = false;\n\n/**\n *\n * @group Plugins\n */\nexport const getDidKeyPlugin = async <DidMethod extends string>(\n learnCard: LearnCard<any, any, DependentMethods<DidMethod>>,\n key: string,\n defaultDidMethod: DidMethod\n): Promise<DidKeyPlugin<DidMethod>> => {\n if (key.length === 0) throw new Error(\"Please don't use an empty string for a key!\");\n if (!isHex(key)) throw new Error('Key must be a hexadecimal string!');\n if (key.length > 64) throw new Error('Key must be less than 64 characters');\n\n if (key.length < 64 && !_shortSeedWarned) {\n _shortSeedWarned = true;\n\n console.warn(\n 'Warning: A LearnCard has been initialized with a seed that is less than 32 bytes, and will be padded with zeroes'\n );\n console.warn(\n 'Please instantiate LearnCards using 32 bytes that have been randomly generated in a cryptographically secure fashion!'\n );\n console.warn(\n 'See https://app.gitbook.com/o/6uDv1QDlxaaZC7i8EaGb/s/FXvEJ9j3Vf3FW5Nc557n/learn-card-packages/learncard-core/instantiation#key-generation for details'\n );\n }\n\n const seed = key.padStart(64, '0');\n const seedBytes = toUint8Array(seed);\n\n const memoizedDids: Partial<Record<DidMethod, string>> = {};\n const keyPairs: Record<Algorithm, JWKWithPrivateKey> = {\n ed25519: learnCard.invoke.generateEd25519KeyFromBytes(seedBytes),\n secp256k1: learnCard.invoke.generateSecp256k1KeyFromBytes(seedBytes),\n };\n\n const did = (method = defaultDidMethod) => {\n if (!memoizedDids[method]) {\n const algorithm = getAlgorithmForDidMethod(method);\n memoizedDids[method] = learnCard.invoke.keyToDid(method, keyPairs[algorithm]);\n }\n\n return memoizedDids[method]!;\n };\n\n const keypair = (algorithm: Algorithm = 'ed25519') => {\n if (!keyPairs[algorithm]) throw new Error('Unsupported algorithm');\n\n return keyPairs[algorithm];\n };\n\n return {\n name: 'DID Key',\n displayName: 'DID Key',\n description: 'Generates dids and JWKs using 32 Secure Random Bytes of Entropy',\n id: {\n did: (_learnCard, method) => did(method as any),\n keypair: (_learnCard, algorithm) => keypair(algorithm as any),\n },\n methods: {\n getSubjectDid: (_learnCard, method = defaultDidMethod) => did(method),\n getSubjectKeypair: (_learnCard, algorithm = 'ed25519') => keypair(algorithm),\n getKey: () => seed,\n },\n };\n};\n", "function fromUint8Array(uint8Array) {\n return uint8Array.reduce(function (ag, v) { return ag += ('00' + v.toString(16)).slice(-2); }, '');\n}\n\nfunction toUint8Array(str) {\n var s = 0, sl = str.length, bytes = [];\n if (sl % 2) \n { throw new Error('invalid hex:' + str); }\n for (; s < sl; s += 2) {\n bytes.push(parseInt(str.substr(s, 2), 16));\n }\n return new Uint8Array(bytes);\n}\n\nfunction fromBuffer(buffer) {\n return fromUint8Array(new Uint8Array(buffer));\n}\n\nfunction toBuffer(str) {\n return toUint8Array(str).buffer;\n}\n\nexport { fromUint8Array, toUint8Array, fromBuffer, toBuffer };\n//# sourceMappingURL=hex-lite.mjs.map\n", "import { Algorithm } from './types';\n\nexport const ED25519_METHODS = ['key', 'tz', 'pkh:tz', 'pkh:tezos', 'pkh:sol', 'pkh:solana'];\n\nexport const SECP256K1_METHODS = [\n 'key',\n 'tz',\n 'ethr',\n 'pkh:tz',\n 'pkh:tezos',\n 'pkh:eth',\n 'pkh:celo',\n 'pkh:poly',\n 'pkh:btc',\n 'pkh:doge',\n 'pkh:eip155',\n 'pkh:bip122',\n];\n\nexport const getAlgorithmForDidMethod = <DidMethod extends string>(\n didMethod: DidMethod\n): Algorithm => {\n if (ED25519_METHODS.includes(didMethod)) return 'ed25519';\n if (\n SECP256K1_METHODS.includes(didMethod) ||\n didMethod.startsWith('pkh:eip155:') ||\n didMethod.startsWith('pkh:bip122:')\n ) {\n return 'secp256k1';\n }\n\n throw new Error('Unspported Did Method');\n};\n"],
5
- "mappings": "4dAAA,IAAAA,EAAA,GAAAC,EAAAD,EAAA,qBAAAE,IAAA,eAAAC,EAAAH,GCIA,SAASI,EAAaC,EAAK,CACvB,IAAIC,EAAI,EAAGC,EAAKF,EAAI,OAAQG,EAAQ,CAAC,EACrC,GAAID,EAAK,EACH,MAAM,IAAI,MAAM,eAAiBF,CAAG,EAC1C,KAAOC,EAAIC,EAAID,GAAK,EAChBE,EAAM,KAAK,SAASH,EAAI,OAAOC,EAAG,CAAC,EAAG,EAAE,CAAC,EAE7C,OAAO,IAAI,WAAWE,CAAK,CAC/B,CARSC,EAAAL,EAAA,gBDHT,IAAAM,EAAsB,8BECf,IAAMC,EAAkB,CAAC,MAAO,KAAM,SAAU,YAAa,UAAW,YAAY,EAE9EC,EAAoB,CAC7B,MACA,KACA,OACA,SACA,YACA,UACA,WACA,WACA,UACA,WACA,aACA,YACJ,EAEaC,EAA2BC,EACpCC,GACY,CACZ,GAAIJ,EAAgB,SAASI,CAAS,EAAG,MAAO,UAChD,GACIH,EAAkB,SAASG,CAAS,GACpCA,EAAU,WAAW,aAAa,GAClCA,EAAU,WAAW,aAAa,EAElC,MAAO,YAGX,MAAM,IAAI,MAAM,uBAAuB,CAC3C,EAbwC,4BFRxC,IAAIC,EAAmB,GAMVC,EAAkBC,EAAA,MAC3BC,EACAC,EACAC,IACmC,CACnC,GAAID,EAAI,SAAW,EAAG,MAAM,IAAI,MAAM,6CAA6C,EACnF,GAAI,IAAC,SAAMA,CAAG,EAAG,MAAM,IAAI,MAAM,mCAAmC,EACpE,GAAIA,EAAI,OAAS,GAAI,MAAM,IAAI,MAAM,qCAAqC,EAEtEA,EAAI,OAAS,IAAM,CAACJ,IACpBA,EAAmB,GAEnB,QAAQ,KACJ,kHACJ,EACA,QAAQ,KACJ,uHACJ,EACA,QAAQ,KACJ,uJACJ,GAGJ,IAAMM,EAAOF,EAAI,SAAS,GAAI,GAAG,EAC3BG,EAAYC,EAAaF,CAAI,EAE7BG,EAAmD,CAAC,EACpDC,EAAiD,CACnD,QAASP,EAAU,OAAO,4BAA4BI,CAAS,EAC/D,UAAWJ,EAAU,OAAO,8BAA8BI,CAAS,CACvE,EAEMI,EAAMT,EAAA,CAACU,EAASP,IAAqB,CACvC,GAAI,CAACI,EAAaG,CAAM,EAAG,CACvB,IAAMC,EAAYC,EAAyBF,CAAM,EACjDH,EAAaG,CAAM,EAAIT,EAAU,OAAO,SAASS,EAAQF,EAASG,CAAS,CAAC,CAChF,CAEA,OAAOJ,EAAaG,CAAM,CAC9B,EAPY,OASNG,EAAUb,EAAA,CAACW,EAAuB,YAAc,CAClD,GAAI,CAACH,EAASG,CAAS,EAAG,MAAM,IAAI,MAAM,uBAAuB,EAEjE,OAAOH,EAASG,CAAS,CAC7B,EAJgB,WAMhB,MAAO,CACH,KAAM,UACN,YAAa,UACb,YAAa,kEACb,GAAI,CACA,IAAKX,EAAA,CAACc,EAAYJ,IAAWD,EAAIC,CAAa,EAAzC,OACL,QAASV,EAAA,CAACc,EAAYH,IAAcE,EAAQF,CAAgB,EAAnD,UACb,EACA,QAAS,CACL,cAAeX,EAAA,CAACc,EAAYJ,EAASP,IAAqBM,EAAIC,CAAM,EAArD,iBACf,kBAAmBV,EAAA,CAACc,EAAYH,EAAY,YAAcE,EAAQF,CAAS,EAAxD,qBACnB,OAAQX,EAAA,IAAMI,EAAN,SACZ,CACJ,CACJ,EA7D+B",
6
- "names": ["index_exports", "__export", "getDidKeyPlugin", "__toCommonJS", "toUint8Array", "str", "s", "sl", "bytes", "__name", "import_helpers", "ED25519_METHODS", "SECP256K1_METHODS", "getAlgorithmForDidMethod", "__name", "didMethod", "_shortSeedWarned", "getDidKeyPlugin", "__name", "learnCard", "key", "defaultDidMethod", "seed", "seedBytes", "toUint8Array", "memoizedDids", "keyPairs", "did", "method", "algorithm", "getAlgorithmForDidMethod", "keypair", "_learnCard"]
3
+ "sources": ["../src/index.ts", "../src/helpers.ts", "../../../../node_modules/hex-lite/dist/hex-lite.mjs", "../src/seed.ts", "../src/didKeyFromSeed.ts"],
4
+ "sourcesContent": ["import { JWKWithPrivateKey } from '@learncard/types';\nimport { LearnCard } from '@learncard/core';\n\nimport { getAlgorithmForDidMethod } from './helpers';\nimport { didKeySeedToBytes } from './seed';\n\nimport { DidKeyPlugin, DependentMethods, Algorithm } from './types';\n\nexport * from './types';\nexport { getEd25519DidKeyFromSeed } from './didKeyFromSeed';\n\n/**\n *\n * @group Plugins\n */\nexport const getDidKeyPlugin = async <DidMethod extends string>(\n learnCard: LearnCard<any, any, DependentMethods<DidMethod>>,\n key: string,\n defaultDidMethod: DidMethod\n): Promise<DidKeyPlugin<DidMethod>> => {\n const { seed, seedBytes } = didKeySeedToBytes(key);\n\n const memoizedDids: Partial<Record<DidMethod, string>> = {};\n // secp256k1 is derived on first use: did:key and most wallets only need ed25519.\n const keyPairs: Partial<Record<Algorithm, JWKWithPrivateKey>> = {\n ed25519: learnCard.invoke.generateEd25519KeyFromBytes(seedBytes),\n };\n\n const keypair = (algorithm: Algorithm = 'ed25519'): JWKWithPrivateKey => {\n if (algorithm === 'secp256k1') {\n keyPairs.secp256k1 ??= learnCard.invoke.generateSecp256k1KeyFromBytes(seedBytes);\n }\n\n const pair = keyPairs[algorithm];\n if (!pair) throw new Error('Unsupported algorithm');\n\n return pair;\n };\n\n const did = (method = defaultDidMethod) => {\n if (!memoizedDids[method]) {\n const algorithm = getAlgorithmForDidMethod(method);\n memoizedDids[method] = learnCard.invoke.keyToDid(method, keypair(algorithm));\n }\n\n return memoizedDids[method]!;\n };\n\n return {\n name: 'DID Key',\n displayName: 'DID Key',\n description: 'Generates dids and JWKs using 32 Secure Random Bytes of Entropy',\n id: {\n did: (_learnCard, method) => did(method as any),\n keypair: (_learnCard, algorithm) => keypair(algorithm as any),\n },\n methods: {\n getSubjectDid: (_learnCard, method = defaultDidMethod) => did(method),\n getSubjectKeypair: (_learnCard, algorithm = 'ed25519') => keypair(algorithm),\n getKey: () => seed,\n },\n };\n};\n", "import { Algorithm } from './types';\n\nexport const ED25519_METHODS = ['key', 'tz', 'pkh:tz', 'pkh:tezos', 'pkh:sol', 'pkh:solana'];\n\nexport const SECP256K1_METHODS = [\n 'key',\n 'tz',\n 'ethr',\n 'pkh:tz',\n 'pkh:tezos',\n 'pkh:eth',\n 'pkh:celo',\n 'pkh:poly',\n 'pkh:btc',\n 'pkh:doge',\n 'pkh:eip155',\n 'pkh:bip122',\n];\n\nexport const getAlgorithmForDidMethod = <DidMethod extends string>(\n didMethod: DidMethod\n): Algorithm => {\n if (ED25519_METHODS.includes(didMethod)) return 'ed25519';\n if (\n SECP256K1_METHODS.includes(didMethod) ||\n didMethod.startsWith('pkh:eip155:') ||\n didMethod.startsWith('pkh:bip122:')\n ) {\n return 'secp256k1';\n }\n\n throw new Error('Unspported Did Method');\n};\n", "function fromUint8Array(uint8Array) {\n return uint8Array.reduce(function (ag, v) { return ag += ('00' + v.toString(16)).slice(-2); }, '');\n}\n\nfunction toUint8Array(str) {\n var s = 0, sl = str.length, bytes = [];\n if (sl % 2) \n { throw new Error('invalid hex:' + str); }\n for (; s < sl; s += 2) {\n bytes.push(parseInt(str.substr(s, 2), 16));\n }\n return new Uint8Array(bytes);\n}\n\nfunction fromBuffer(buffer) {\n return fromUint8Array(new Uint8Array(buffer));\n}\n\nfunction toBuffer(str) {\n return toUint8Array(str).buffer;\n}\n\nexport { fromUint8Array, toUint8Array, fromBuffer, toBuffer };\n//# sourceMappingURL=hex-lite.mjs.map\n", "import { toUint8Array } from 'hex-lite';\nimport { isHex } from '@learncard/helpers';\n\nlet _shortSeedWarned = false;\n\n/**\n * Validates a hex key and turns it into the 32 seed bytes every did:key derivation uses.\n * Shorter keys are left-padded with zeroes (legacy keys can drop leading zeroes).\n */\nexport const didKeySeedToBytes = (key: string): { seed: string; seedBytes: Uint8Array } => {\n if (key.length === 0) throw new Error(\"Please don't use an empty string for a key!\");\n if (!isHex(key)) throw new Error('Key must be a hexadecimal string!');\n if (key.length > 64) throw new Error('Key must be less than 64 characters');\n\n if (key.length < 64 && !_shortSeedWarned) {\n _shortSeedWarned = true;\n\n console.warn(\n 'Warning: A LearnCard has been initialized with a seed that is less than 32 bytes, and will be padded with zeroes'\n );\n console.warn(\n 'Please instantiate LearnCards using 32 bytes that have been randomly generated in a cryptographically secure fashion!'\n );\n console.warn(\n 'See https://app.gitbook.com/o/6uDv1QDlxaaZC7i8EaGb/s/FXvEJ9j3Vf3FW5Nc557n/learn-card-packages/learncard-core/instantiation#key-generation for details'\n );\n }\n\n const seed = key.padStart(64, '0');\n\n return { seed, seedBytes: toUint8Array(seed) };\n};\n", "import { getPublicKeyAsync } from '@noble/ed25519';\nimport { base58 } from '@scure/base';\n\nimport { didKeySeedToBytes } from './seed';\n\n/** Multicodec varint prefix for an Ed25519 public key (0xed). */\nconst ED25519_PUB_MULTICODEC = [0xed, 0x01];\n\n/**\n * Derives the Ed25519 `did:key` for a seed without DIDKit (no WASM, no wallet).\n *\n * Same seed handling and result as `getDidKeyPlugin(learnCard, key, 'key')`'s `did()`:\n * the 32 seed bytes are the Ed25519 secret, and the DID is\n * `did:key:z` + base58btc(0xed01 \u2016 publicKey).\n */\nexport const getEd25519DidKeyFromSeed = async (key: string): Promise<string> => {\n const { seedBytes } = didKeySeedToBytes(key);\n const publicKey = await getPublicKeyAsync(seedBytes);\n\n const multicodecKey = new Uint8Array(ED25519_PUB_MULTICODEC.length + publicKey.length);\n multicodecKey.set(ED25519_PUB_MULTICODEC);\n multicodecKey.set(publicKey, ED25519_PUB_MULTICODEC.length);\n\n return `did:key:z${base58.encode(multicodecKey)}`;\n};\n"],
5
+ "mappings": "4dAAA,IAAAA,EAAA,GAAAC,EAAAD,EAAA,qBAAAE,EAAA,6BAAAC,IAAA,eAAAC,E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,EAArD,iBACf,kBAAmBb,EAAA,CAACe,EAAYL,EAAY,YAAcD,EAAQC,CAAS,EAAxD,qBACnB,OAAQV,EAAA,IAAMI,EAAN,SACZ,CACJ,CACJ,EA/C+B",
6
+ "names": ["index_exports", "__export", "getDidKeyPlugin", "getEd25519DidKeyFromSeed", "__toCommonJS", "ED25519_METHODS", "SECP256K1_METHODS", "getAlgorithmForDidMethod", "__name", "didMethod", "toUint8Array", "str", "s", "sl", "bytes", "__name", "import_helpers", "_shortSeedWarned", "didKeySeedToBytes", "__name", "key", "seed", "toUint8Array", "import_ed25519", "import_base", "ED25519_PUB_MULTICODEC", "getEd25519DidKeyFromSeed", "__name", "key", "seedBytes", "didKeySeedToBytes", "publicKey", "multicodecKey", "getDidKeyPlugin", "__name", "learnCard", "key", "defaultDidMethod", "seed", "seedBytes", "didKeySeedToBytes", "memoizedDids", "keyPairs", "keypair", "algorithm", "pair", "did", "method", "getAlgorithmForDidMethod", "_learnCard"]
7
7
  }
@@ -1,22 +1,6 @@
1
1
  var __defProp = Object.defineProperty;
2
2
  var __name = (target, value) => __defProp(target, "name", { value, configurable: true });
3
3
 
4
- // ../../../node_modules/hex-lite/dist/hex-lite.mjs
5
- function toUint8Array(str) {
6
- var s = 0, sl = str.length, bytes = [];
7
- if (sl % 2) {
8
- throw new Error("invalid hex:" + str);
9
- }
10
- for (; s < sl; s += 2) {
11
- bytes.push(parseInt(str.substr(s, 2), 16));
12
- }
13
- return new Uint8Array(bytes);
14
- }
15
- __name(toUint8Array, "toUint8Array");
16
-
17
- // src/index.ts
18
- import { isHex } from "@learncard/helpers";
19
-
20
4
  // src/helpers.ts
21
5
  var ED25519_METHODS = ["key", "tz", "pkh:tz", "pkh:tezos", "pkh:sol", "pkh:solana"];
22
6
  var SECP256K1_METHODS = [
@@ -41,9 +25,23 @@ var getAlgorithmForDidMethod = /* @__PURE__ */ __name((didMethod) => {
41
25
  throw new Error("Unspported Did Method");
42
26
  }, "getAlgorithmForDidMethod");
43
27
 
44
- // src/index.ts
28
+ // ../../../node_modules/hex-lite/dist/hex-lite.mjs
29
+ function toUint8Array(str) {
30
+ var s = 0, sl = str.length, bytes = [];
31
+ if (sl % 2) {
32
+ throw new Error("invalid hex:" + str);
33
+ }
34
+ for (; s < sl; s += 2) {
35
+ bytes.push(parseInt(str.substr(s, 2), 16));
36
+ }
37
+ return new Uint8Array(bytes);
38
+ }
39
+ __name(toUint8Array, "toUint8Array");
40
+
41
+ // src/seed.ts
42
+ import { isHex } from "@learncard/helpers";
45
43
  var _shortSeedWarned = false;
46
- var getDidKeyPlugin = /* @__PURE__ */ __name(async (learnCard, key, defaultDidMethod) => {
44
+ var didKeySeedToBytes = /* @__PURE__ */ __name((key) => {
47
45
  if (key.length === 0) throw new Error("Please don't use an empty string for a key!");
48
46
  if (!isHex(key)) throw new Error("Key must be a hexadecimal string!");
49
47
  if (key.length > 64) throw new Error("Key must be less than 64 characters");
@@ -60,23 +58,44 @@ var getDidKeyPlugin = /* @__PURE__ */ __name(async (learnCard, key, defaultDidMe
60
58
  );
61
59
  }
62
60
  const seed = key.padStart(64, "0");
63
- const seedBytes = toUint8Array(seed);
61
+ return { seed, seedBytes: toUint8Array(seed) };
62
+ }, "didKeySeedToBytes");
63
+
64
+ // src/didKeyFromSeed.ts
65
+ import { getPublicKeyAsync } from "@noble/ed25519";
66
+ import { base58 } from "@scure/base";
67
+ var ED25519_PUB_MULTICODEC = [237, 1];
68
+ var getEd25519DidKeyFromSeed = /* @__PURE__ */ __name(async (key) => {
69
+ const { seedBytes } = didKeySeedToBytes(key);
70
+ const publicKey = await getPublicKeyAsync(seedBytes);
71
+ const multicodecKey = new Uint8Array(ED25519_PUB_MULTICODEC.length + publicKey.length);
72
+ multicodecKey.set(ED25519_PUB_MULTICODEC);
73
+ multicodecKey.set(publicKey, ED25519_PUB_MULTICODEC.length);
74
+ return `did:key:z${base58.encode(multicodecKey)}`;
75
+ }, "getEd25519DidKeyFromSeed");
76
+
77
+ // src/index.ts
78
+ var getDidKeyPlugin = /* @__PURE__ */ __name(async (learnCard, key, defaultDidMethod) => {
79
+ const { seed, seedBytes } = didKeySeedToBytes(key);
64
80
  const memoizedDids = {};
65
81
  const keyPairs = {
66
- ed25519: learnCard.invoke.generateEd25519KeyFromBytes(seedBytes),
67
- secp256k1: learnCard.invoke.generateSecp256k1KeyFromBytes(seedBytes)
82
+ ed25519: learnCard.invoke.generateEd25519KeyFromBytes(seedBytes)
68
83
  };
84
+ const keypair = /* @__PURE__ */ __name((algorithm = "ed25519") => {
85
+ if (algorithm === "secp256k1") {
86
+ keyPairs.secp256k1 ?? (keyPairs.secp256k1 = learnCard.invoke.generateSecp256k1KeyFromBytes(seedBytes));
87
+ }
88
+ const pair = keyPairs[algorithm];
89
+ if (!pair) throw new Error("Unsupported algorithm");
90
+ return pair;
91
+ }, "keypair");
69
92
  const did = /* @__PURE__ */ __name((method = defaultDidMethod) => {
70
93
  if (!memoizedDids[method]) {
71
94
  const algorithm = getAlgorithmForDidMethod(method);
72
- memoizedDids[method] = learnCard.invoke.keyToDid(method, keyPairs[algorithm]);
95
+ memoizedDids[method] = learnCard.invoke.keyToDid(method, keypair(algorithm));
73
96
  }
74
97
  return memoizedDids[method];
75
98
  }, "did");
76
- const keypair = /* @__PURE__ */ __name((algorithm = "ed25519") => {
77
- if (!keyPairs[algorithm]) throw new Error("Unsupported algorithm");
78
- return keyPairs[algorithm];
79
- }, "keypair");
80
99
  return {
81
100
  name: "DID Key",
82
101
  displayName: "DID Key",
@@ -93,6 +112,7 @@ var getDidKeyPlugin = /* @__PURE__ */ __name(async (learnCard, key, defaultDidMe
93
112
  };
94
113
  }, "getDidKeyPlugin");
95
114
  export {
96
- getDidKeyPlugin
115
+ getDidKeyPlugin,
116
+ getEd25519DidKeyFromSeed
97
117
  };
98
118
  //# sourceMappingURL=didkey-plugin.esm.js.map
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "version": 3,
3
- "sources": ["../../../../node_modules/hex-lite/dist/hex-lite.mjs", "../src/index.ts", "../src/helpers.ts"],
4
- "sourcesContent": ["function fromUint8Array(uint8Array) {\n return uint8Array.reduce(function (ag, v) { return ag += ('00' + v.toString(16)).slice(-2); }, '');\n}\n\nfunction toUint8Array(str) {\n var s = 0, sl = str.length, bytes = [];\n if (sl % 2) \n { throw new Error('invalid hex:' + str); }\n for (; s < sl; s += 2) {\n bytes.push(parseInt(str.substr(s, 2), 16));\n }\n return new Uint8Array(bytes);\n}\n\nfunction fromBuffer(buffer) {\n return fromUint8Array(new Uint8Array(buffer));\n}\n\nfunction toBuffer(str) {\n return toUint8Array(str).buffer;\n}\n\nexport { fromUint8Array, toUint8Array, fromBuffer, toBuffer };\n//# sourceMappingURL=hex-lite.mjs.map\n", "import { toUint8Array } from 'hex-lite';\nimport { isHex } from '@learncard/helpers';\nimport { JWKWithPrivateKey } from '@learncard/types';\nimport { LearnCard } from '@learncard/core';\n\nimport { getAlgorithmForDidMethod } from './helpers';\n\nimport { DidKeyPlugin, DependentMethods, Algorithm } from './types';\n\nexport * from './types';\n\nlet _shortSeedWarned = false;\n\n/**\n *\n * @group Plugins\n */\nexport const getDidKeyPlugin = async <DidMethod extends string>(\n learnCard: LearnCard<any, any, DependentMethods<DidMethod>>,\n key: string,\n defaultDidMethod: DidMethod\n): Promise<DidKeyPlugin<DidMethod>> => {\n if (key.length === 0) throw new Error(\"Please don't use an empty string for a key!\");\n if (!isHex(key)) throw new Error('Key must be a hexadecimal string!');\n if (key.length > 64) throw new Error('Key must be less than 64 characters');\n\n if (key.length < 64 && !_shortSeedWarned) {\n _shortSeedWarned = true;\n\n console.warn(\n 'Warning: A LearnCard has been initialized with a seed that is less than 32 bytes, and will be padded with zeroes'\n );\n console.warn(\n 'Please instantiate LearnCards using 32 bytes that have been randomly generated in a cryptographically secure fashion!'\n );\n console.warn(\n 'See https://app.gitbook.com/o/6uDv1QDlxaaZC7i8EaGb/s/FXvEJ9j3Vf3FW5Nc557n/learn-card-packages/learncard-core/instantiation#key-generation for details'\n );\n }\n\n const seed = key.padStart(64, '0');\n const seedBytes = toUint8Array(seed);\n\n const memoizedDids: Partial<Record<DidMethod, string>> = {};\n const keyPairs: Record<Algorithm, JWKWithPrivateKey> = {\n ed25519: learnCard.invoke.generateEd25519KeyFromBytes(seedBytes),\n secp256k1: learnCard.invoke.generateSecp256k1KeyFromBytes(seedBytes),\n };\n\n const did = (method = defaultDidMethod) => {\n if (!memoizedDids[method]) {\n const algorithm = getAlgorithmForDidMethod(method);\n memoizedDids[method] = learnCard.invoke.keyToDid(method, keyPairs[algorithm]);\n }\n\n return memoizedDids[method]!;\n };\n\n const keypair = (algorithm: Algorithm = 'ed25519') => {\n if (!keyPairs[algorithm]) throw new Error('Unsupported algorithm');\n\n return keyPairs[algorithm];\n };\n\n return {\n name: 'DID Key',\n displayName: 'DID Key',\n description: 'Generates dids and JWKs using 32 Secure Random Bytes of Entropy',\n id: {\n did: (_learnCard, method) => did(method as any),\n keypair: (_learnCard, algorithm) => keypair(algorithm as any),\n },\n methods: {\n getSubjectDid: (_learnCard, method = defaultDidMethod) => did(method),\n getSubjectKeypair: (_learnCard, algorithm = 'ed25519') => keypair(algorithm),\n getKey: () => seed,\n },\n };\n};\n", "import { Algorithm } from './types';\n\nexport const ED25519_METHODS = ['key', 'tz', 'pkh:tz', 'pkh:tezos', 'pkh:sol', 'pkh:solana'];\n\nexport const SECP256K1_METHODS = [\n 'key',\n 'tz',\n 'ethr',\n 'pkh:tz',\n 'pkh:tezos',\n 'pkh:eth',\n 'pkh:celo',\n 'pkh:poly',\n 'pkh:btc',\n 'pkh:doge',\n 'pkh:eip155',\n 'pkh:bip122',\n];\n\nexport const getAlgorithmForDidMethod = <DidMethod extends string>(\n didMethod: DidMethod\n): Algorithm => {\n if (ED25519_METHODS.includes(didMethod)) return 'ed25519';\n if (\n SECP256K1_METHODS.includes(didMethod) ||\n didMethod.startsWith('pkh:eip155:') ||\n didMethod.startsWith('pkh:bip122:')\n ) {\n return 'secp256k1';\n }\n\n throw new Error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AAA,IACJ;AAAA,EACJ;AAEA,QAAM,OAAO,IAAI,SAAS,IAAI,GAAG;AACjC,QAAM,YAAY,aAAa,IAAI;AAEnC,QAAM,eAAmD,CAAC;AAC1D,QAAM,WAAiD;AAAA,IACnD,SAAS,UAAU,OAAO,4BAA4B,SAAS;AAAA,IAC/D,WAAW,UAAU,OAAO,8BAA8B,SAAS;AAAA,EACvE;AAEA,QAAM,MAAM,wBAAC,SAAS,qBAAqB;AACvC,QAAI,CAAC,aAAa,MAAM,GAAG;AACvB,YAAM,YAAY,yBAAyB,MAAM;AACjD,mBAAa,MAAM,IAAI,UAAU,OAAO,SAAS,QAAQ,SAAS,SAAS,CAAC;AAAA,IAChF;AAEA,WAAO,aAAa,MAAM;AAAA,EAC9B,GAPY;AASZ,QAAM,UAAU,wBAAC,YAAuB,cAAc;AAClD,QAAI,CAAC,SAAS,SAAS,EAAG,OAAM,IAAI,MAAM,uBAAuB;AAEjE,WAAO,SAAS,SAAS;AAAA,EAC7B,GAJgB;AAMhB,SAAO;AAAA,IACH,MAAM;AAAA,IACN,aAAa;AAAA,IACb,aAAa;AAAA,IACb,IAAI;AAAA,MACA,KAAK,wBAAC,YAAY,WAAW,IAAI,MAAa,GAAzC;AAAA,MACL,SAAS,wBAAC,YAAY,cAAc,QAAQ,SAAgB,GAAnD;AAAA,IACb;AAAA,IACA,SAAS;AAAA,MACL,eAAe,wBAAC,YAAY,SAAS,qBAAqB,IAAI,MAAM,GAArD;AAAA,MACf,mBAAmB,wBAAC,YAAY,YAAY,cAAc,QAAQ,SAAS,GAAxD;AAAA,MACnB,QAAQ,6BAAM,MAAN;AAAA,IACZ;AAAA,EACJ;AACJ,GA7D+B;",
3
+ "sources": ["../src/helpers.ts", "../../../../node_modules/hex-lite/dist/hex-lite.mjs", "../src/seed.ts", "../src/didKeyFromSeed.ts", "../src/index.ts"],
4
+ "sourcesContent": ["import { Algorithm } from './types';\n\nexport const ED25519_METHODS = ['key', 'tz', 'pkh:tz', 'pkh:tezos', 'pkh:sol', 'pkh:solana'];\n\nexport const SECP256K1_METHODS = [\n 'key',\n 'tz',\n 'ethr',\n 'pkh:tz',\n 'pkh:tezos',\n 'pkh:eth',\n 'pkh:celo',\n 'pkh:poly',\n 'pkh:btc',\n 'pkh:doge',\n 'pkh:eip155',\n 'pkh:bip122',\n];\n\nexport const getAlgorithmForDidMethod = <DidMethod extends string>(\n didMethod: DidMethod\n): Algorithm => {\n if (ED25519_METHODS.includes(didMethod)) return 'ed25519';\n if (\n SECP256K1_METHODS.includes(didMethod) ||\n didMethod.startsWith('pkh:eip155:') ||\n didMethod.startsWith('pkh:bip122:')\n ) {\n return 'secp256k1';\n }\n\n throw new Error('Unspported Did Method');\n};\n", "function fromUint8Array(uint8Array) {\n return uint8Array.reduce(function (ag, v) { return ag += ('00' + v.toString(16)).slice(-2); }, '');\n}\n\nfunction toUint8Array(str) {\n var s = 0, sl = str.length, bytes = [];\n if (sl % 2) \n { throw new Error('invalid hex:' + str); }\n for (; s < sl; s += 2) {\n bytes.push(parseInt(str.substr(s, 2), 16));\n }\n return new Uint8Array(bytes);\n}\n\nfunction fromBuffer(buffer) {\n return fromUint8Array(new Uint8Array(buffer));\n}\n\nfunction toBuffer(str) {\n return toUint8Array(str).buffer;\n}\n\nexport { fromUint8Array, toUint8Array, fromBuffer, toBuffer };\n//# sourceMappingURL=hex-lite.mjs.map\n", "import { toUint8Array } from 'hex-lite';\nimport { isHex } from '@learncard/helpers';\n\nlet _shortSeedWarned = false;\n\n/**\n * Validates a hex key and turns it into the 32 seed bytes every did:key derivation uses.\n * Shorter keys are left-padded with zeroes (legacy keys can drop leading zeroes).\n */\nexport const didKeySeedToBytes = (key: string): { seed: string; seedBytes: Uint8Array } => {\n if (key.length === 0) throw new Error(\"Please don't use an empty string for a key!\");\n if (!isHex(key)) throw new Error('Key must be a hexadecimal string!');\n if (key.length > 64) throw new Error('Key must be less than 64 characters');\n\n if (key.length < 64 && !_shortSeedWarned) {\n _shortSeedWarned = true;\n\n console.warn(\n 'Warning: A LearnCard has been initialized with a seed that is less than 32 bytes, and will be padded with zeroes'\n );\n console.warn(\n 'Please instantiate LearnCards using 32 bytes that have been randomly generated in a cryptographically secure fashion!'\n );\n console.warn(\n 'See https://app.gitbook.com/o/6uDv1QDlxaaZC7i8EaGb/s/FXvEJ9j3Vf3FW5Nc557n/learn-card-packages/learncard-core/instantiation#key-generation for details'\n );\n }\n\n const seed = key.padStart(64, '0');\n\n return { seed, seedBytes: toUint8Array(seed) };\n};\n", "import { getPublicKeyAsync } from '@noble/ed25519';\nimport { base58 } from '@scure/base';\n\nimport { didKeySeedToBytes } from './seed';\n\n/** Multicodec varint prefix for an Ed25519 public key (0xed). */\nconst ED25519_PUB_MULTICODEC = [0xed, 0x01];\n\n/**\n * Derives the Ed25519 `did:key` for a seed without DIDKit (no WASM, no wallet).\n *\n * Same seed handling and result as `getDidKeyPlugin(learnCard, key, 'key')`'s `did()`:\n * the 32 seed bytes are the Ed25519 secret, and the DID is\n * `did:key:z` + base58btc(0xed01 \u2016 publicKey).\n */\nexport const getEd25519DidKeyFromSeed = async (key: string): Promise<string> => {\n const { seedBytes } = didKeySeedToBytes(key);\n const publicKey = await getPublicKeyAsync(seedBytes);\n\n const multicodecKey = new Uint8Array(ED25519_PUB_MULTICODEC.length + publicKey.length);\n multicodecKey.set(ED25519_PUB_MULTICODEC);\n multicodecKey.set(publicKey, ED25519_PUB_MULTICODEC.length);\n\n return `did:key:z${base58.encode(multicodecKey)}`;\n};\n", "import { JWKWithPrivateKey } from '@learncard/types';\nimport { LearnCard } from '@learncard/core';\n\nimport { getAlgorithmForDidMethod } from './helpers';\nimport { didKeySeedToBytes } from './seed';\n\nimport { DidKeyPlugin, DependentMethods, Algorithm } from './types';\n\nexport * from './types';\nexport { getEd25519DidKeyFromSeed } from './didKeyFromSeed';\n\n/**\n *\n * @group Plugins\n */\nexport const getDidKeyPlugin = async <DidMethod extends string>(\n learnCard: LearnCard<any, any, DependentMethods<DidMethod>>,\n key: string,\n defaultDidMethod: DidMethod\n): Promise<DidKeyPlugin<DidMethod>> => {\n const { seed, seedBytes } = didKeySeedToBytes(key);\n\n const memoizedDids: Partial<Record<DidMethod, string>> = {};\n // secp256k1 is derived on first use: did:key and most wallets only need ed25519.\n const keyPairs: Partial<Record<Algorithm, JWKWithPrivateKey>> = {\n ed25519: learnCard.invoke.generateEd25519KeyFromBytes(seedBytes),\n };\n\n const keypair = (algorithm: Algorithm = 'ed25519'): JWKWithPrivateKey => {\n if (algorithm === 'secp256k1') {\n keyPairs.secp256k1 ??= learnCard.invoke.generateSecp256k1KeyFromBytes(seedBytes);\n }\n\n const pair = keyPairs[algorithm];\n if (!pair) throw new Error('Unsupported algorithm');\n\n return pair;\n };\n\n const did = (method = defaultDidMethod) => {\n if (!memoizedDids[method]) {\n const algorithm = getAlgorithmForDidMethod(method);\n memoizedDids[method] = learnCard.invoke.keyToDid(method, keypair(algorithm));\n }\n\n return memoizedDids[method]!;\n };\n\n return {\n name: 'DID Key',\n displayName: 'DID Key',\n description: 'Generates dids and JWKs using 32 Secure Random Bytes of Entropy',\n id: {\n did: (_learnCard, method) => did(method as any),\n keypair: (_learnCard, algorithm) => keypair(algorithm as any),\n },\n methods: {\n getSubjectDid: (_learnCard, method = defaultDidMethod) => did(method),\n getSubjectKeypair: (_learnCard, algorithm = 'ed25519') => keypair(algorithm),\n getKey: () => seed,\n },\n };\n};\n"],
5
+ "mappings": ";;;;AAEO,IAAM,kBAAkB,CAAC,OAAO,MAAM,UAAU,aAAa,WAAW,YAAY;AAEpF,IAAM,oBAAoB;AAAA,EAC7B;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AACJ;AAEO,IAAM,2BAA2B,wBACpC,cACY;AACZ,MAAI,gBAAgB,SAAS,SAAS,EAAG,QAAO;AAChD,MACI,kBAAkB,SAAS,SAAS,KACpC,UAA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6
  "names": []
7
7
  }
package/dist/index.d.cts CHANGED
@@ -1,6 +1,7 @@
1
1
  import { LearnCard } from '@learncard/core';
2
2
  import { DidKeyPlugin, DependentMethods } from './types';
3
3
  export * from './types';
4
+ export { getEd25519DidKeyFromSeed } from './didKeyFromSeed';
4
5
  /**
5
6
  *
6
7
  * @group Plugins
package/dist/index.d.ts CHANGED
@@ -1,6 +1,7 @@
1
1
  import { LearnCard } from '@learncard/core';
2
2
  import { DidKeyPlugin, DependentMethods } from './types';
3
3
  export * from './types';
4
+ export { getEd25519DidKeyFromSeed } from './didKeyFromSeed';
4
5
  /**
5
6
  *
6
7
  * @group Plugins
@@ -1 +1 @@
1
- {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAGA,OAAO,EAAE,SAAS,EAAE,MAAM,iBAAiB,CAAC;AAI5C,OAAO,EAAE,YAAY,EAAE,gBAAgB,EAAa,MAAM,SAAS,CAAC;AAEpE,cAAc,SAAS,CAAC;AAIxB;;;GAGG;AACH,eAAO,MAAM,eAAe,GAAU,SAAS,SAAS,MAAM,EAC1D,WAAW,SAAS,CAAC,GAAG,EAAE,GAAG,EAAE,gBAAgB,CAAC,SAAS,CAAC,CAAC,EAC3D,KAAK,MAAM,EACX,kBAAkB,SAAS,KAC5B,OAAO,CAAC,YAAY,CAAC,SAAS,CAAC,CAyDjC,CAAC"}
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,SAAS,EAAE,MAAM,iBAAiB,CAAC;AAK5C,OAAO,EAAE,YAAY,EAAE,gBAAgB,EAAa,MAAM,SAAS,CAAC;AAEpE,cAAc,SAAS,CAAC;AACxB,OAAO,EAAE,wBAAwB,EAAE,MAAM,kBAAkB,CAAC;AAE5D;;;GAGG;AACH,eAAO,MAAM,eAAe,GAAU,SAAS,SAAS,MAAM,EAC1D,WAAW,SAAS,CAAC,GAAG,EAAE,GAAG,EAAE,gBAAgB,CAAC,SAAS,CAAC,CAAC,EAC3D,KAAK,MAAM,EACX,kBAAkB,SAAS,KAC5B,OAAO,CAAC,YAAY,CAAC,SAAS,CAAC,CA2CjC,CAAC"}
package/dist/seed.d.ts ADDED
@@ -0,0 +1,9 @@
1
+ /**
2
+ * Validates a hex key and turns it into the 32 seed bytes every did:key derivation uses.
3
+ * Shorter keys are left-padded with zeroes (legacy keys can drop leading zeroes).
4
+ */
5
+ export declare const didKeySeedToBytes: (key: string) => {
6
+ seed: string;
7
+ seedBytes: Uint8Array;
8
+ };
9
+ //# sourceMappingURL=seed.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"seed.d.ts","sourceRoot":"","sources":["../src/seed.ts"],"names":[],"mappings":"AAKA;;;GAGG;AACH,eAAO,MAAM,iBAAiB,GAAI,KAAK,MAAM,KAAG;IAAE,IAAI,EAAE,MAAM,CAAC;IAAC,SAAS,EAAE,UAAU,CAAA;CAsBpF,CAAC"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@learncard/didkey-plugin",
3
- "version": "1.1.46",
3
+ "version": "1.2.1",
4
4
  "description": "Derives did:key and other DIDKit-supported DIDs and keypairs from a seed.",
5
5
  "type": "module",
6
6
  "main": "./dist/index.cjs",
@@ -39,7 +39,8 @@
39
39
  "url": "https://github.com/learningeconomy/LearnCard/issues"
40
40
  },
41
41
  "devDependencies": {
42
- "@learncard/types": "6.1.1",
42
+ "@learncard/didkit-plugin": "1.11.1",
43
+ "@learncard/types": "6.3.0",
43
44
  "@types/node": "^17.0.31",
44
45
  "aqu": "0.4.3",
45
46
  "esbuild": "^0.27.1",
@@ -48,8 +49,10 @@
48
49
  },
49
50
  "types": "./dist/index.d.ts",
50
51
  "dependencies": {
51
- "@learncard/core": "9.4.46",
52
- "@learncard/helpers": "^1.8.2",
52
+ "@learncard/core": "10.0.1",
53
+ "@learncard/helpers": "^1.8.4",
54
+ "@noble/ed25519": "^2.0.0",
55
+ "@scure/base": "1.2.6",
53
56
  "hex-lite": "^1.5.0"
54
57
  },
55
58
  "sideEffects": false
@@ -0,0 +1,25 @@
1
+ import { getPublicKeyAsync } from '@noble/ed25519';
2
+ import { base58 } from '@scure/base';
3
+
4
+ import { didKeySeedToBytes } from './seed';
5
+
6
+ /** Multicodec varint prefix for an Ed25519 public key (0xed). */
7
+ const ED25519_PUB_MULTICODEC = [0xed, 0x01];
8
+
9
+ /**
10
+ * Derives the Ed25519 `did:key` for a seed without DIDKit (no WASM, no wallet).
11
+ *
12
+ * Same seed handling and result as `getDidKeyPlugin(learnCard, key, 'key')`'s `did()`:
13
+ * the 32 seed bytes are the Ed25519 secret, and the DID is
14
+ * `did:key:z` + base58btc(0xed01 ‖ publicKey).
15
+ */
16
+ export const getEd25519DidKeyFromSeed = async (key: string): Promise<string> => {
17
+ const { seedBytes } = didKeySeedToBytes(key);
18
+ const publicKey = await getPublicKeyAsync(seedBytes);
19
+
20
+ const multicodecKey = new Uint8Array(ED25519_PUB_MULTICODEC.length + publicKey.length);
21
+ multicodecKey.set(ED25519_PUB_MULTICODEC);
22
+ multicodecKey.set(publicKey, ED25519_PUB_MULTICODEC.length);
23
+
24
+ return `did:key:z${base58.encode(multicodecKey)}`;
25
+ };
package/src/index.ts CHANGED
@@ -1,15 +1,13 @@
1
- import { toUint8Array } from 'hex-lite';
2
- import { isHex } from '@learncard/helpers';
3
1
  import { JWKWithPrivateKey } from '@learncard/types';
4
2
  import { LearnCard } from '@learncard/core';
5
3
 
6
4
  import { getAlgorithmForDidMethod } from './helpers';
5
+ import { didKeySeedToBytes } from './seed';
7
6
 
8
7
  import { DidKeyPlugin, DependentMethods, Algorithm } from './types';
9
8
 
10
9
  export * from './types';
11
-
12
- let _shortSeedWarned = false;
10
+ export { getEd25519DidKeyFromSeed } from './didKeyFromSeed';
13
11
 
14
12
  /**
15
13
  *
@@ -20,48 +18,34 @@ export const getDidKeyPlugin = async <DidMethod extends string>(
20
18
  key: string,
21
19
  defaultDidMethod: DidMethod
22
20
  ): Promise<DidKeyPlugin<DidMethod>> => {
23
- if (key.length === 0) throw new Error("Please don't use an empty string for a key!");
24
- if (!isHex(key)) throw new Error('Key must be a hexadecimal string!');
25
- if (key.length > 64) throw new Error('Key must be less than 64 characters');
21
+ const { seed, seedBytes } = didKeySeedToBytes(key);
26
22
 
27
- if (key.length < 64 && !_shortSeedWarned) {
28
- _shortSeedWarned = true;
23
+ const memoizedDids: Partial<Record<DidMethod, string>> = {};
24
+ // secp256k1 is derived on first use: did:key and most wallets only need ed25519.
25
+ const keyPairs: Partial<Record<Algorithm, JWKWithPrivateKey>> = {
26
+ ed25519: learnCard.invoke.generateEd25519KeyFromBytes(seedBytes),
27
+ };
29
28
 
30
- console.warn(
31
- 'Warning: A LearnCard has been initialized with a seed that is less than 32 bytes, and will be padded with zeroes'
32
- );
33
- console.warn(
34
- 'Please instantiate LearnCards using 32 bytes that have been randomly generated in a cryptographically secure fashion!'
35
- );
36
- console.warn(
37
- 'See https://app.gitbook.com/o/6uDv1QDlxaaZC7i8EaGb/s/FXvEJ9j3Vf3FW5Nc557n/learn-card-packages/learncard-core/instantiation#key-generation for details'
38
- );
39
- }
29
+ const keypair = (algorithm: Algorithm = 'ed25519'): JWKWithPrivateKey => {
30
+ if (algorithm === 'secp256k1') {
31
+ keyPairs.secp256k1 ??= learnCard.invoke.generateSecp256k1KeyFromBytes(seedBytes);
32
+ }
40
33
 
41
- const seed = key.padStart(64, '0');
42
- const seedBytes = toUint8Array(seed);
34
+ const pair = keyPairs[algorithm];
35
+ if (!pair) throw new Error('Unsupported algorithm');
43
36
 
44
- const memoizedDids: Partial<Record<DidMethod, string>> = {};
45
- const keyPairs: Record<Algorithm, JWKWithPrivateKey> = {
46
- ed25519: learnCard.invoke.generateEd25519KeyFromBytes(seedBytes),
47
- secp256k1: learnCard.invoke.generateSecp256k1KeyFromBytes(seedBytes),
37
+ return pair;
48
38
  };
49
39
 
50
40
  const did = (method = defaultDidMethod) => {
51
41
  if (!memoizedDids[method]) {
52
42
  const algorithm = getAlgorithmForDidMethod(method);
53
- memoizedDids[method] = learnCard.invoke.keyToDid(method, keyPairs[algorithm]);
43
+ memoizedDids[method] = learnCard.invoke.keyToDid(method, keypair(algorithm));
54
44
  }
55
45
 
56
46
  return memoizedDids[method]!;
57
47
  };
58
48
 
59
- const keypair = (algorithm: Algorithm = 'ed25519') => {
60
- if (!keyPairs[algorithm]) throw new Error('Unsupported algorithm');
61
-
62
- return keyPairs[algorithm];
63
- };
64
-
65
49
  return {
66
50
  name: 'DID Key',
67
51
  displayName: 'DID Key',
package/src/seed.ts ADDED
@@ -0,0 +1,32 @@
1
+ import { toUint8Array } from 'hex-lite';
2
+ import { isHex } from '@learncard/helpers';
3
+
4
+ let _shortSeedWarned = false;
5
+
6
+ /**
7
+ * Validates a hex key and turns it into the 32 seed bytes every did:key derivation uses.
8
+ * Shorter keys are left-padded with zeroes (legacy keys can drop leading zeroes).
9
+ */
10
+ export const didKeySeedToBytes = (key: string): { seed: string; seedBytes: Uint8Array } => {
11
+ if (key.length === 0) throw new Error("Please don't use an empty string for a key!");
12
+ if (!isHex(key)) throw new Error('Key must be a hexadecimal string!');
13
+ if (key.length > 64) throw new Error('Key must be less than 64 characters');
14
+
15
+ if (key.length < 64 && !_shortSeedWarned) {
16
+ _shortSeedWarned = true;
17
+
18
+ console.warn(
19
+ 'Warning: A LearnCard has been initialized with a seed that is less than 32 bytes, and will be padded with zeroes'
20
+ );
21
+ console.warn(
22
+ 'Please instantiate LearnCards using 32 bytes that have been randomly generated in a cryptographically secure fashion!'
23
+ );
24
+ console.warn(
25
+ 'See https://app.gitbook.com/o/6uDv1QDlxaaZC7i8EaGb/s/FXvEJ9j3Vf3FW5Nc557n/learn-card-packages/learncard-core/instantiation#key-generation for details'
26
+ );
27
+ }
28
+
29
+ const seed = key.padStart(64, '0');
30
+
31
+ return { seed, seedBytes: toUint8Array(seed) };
32
+ };