@owlmeans/did 0.1.0

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.
Files changed (90) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +552 -0
  3. package/build/.gitkeep +0 -0
  4. package/build/consts.d.ts +9 -0
  5. package/build/consts.d.ts.map +1 -0
  6. package/build/consts.js +9 -0
  7. package/build/consts.js.map +1 -0
  8. package/build/errors.d.ts +22 -0
  9. package/build/errors.d.ts.map +1 -0
  10. package/build/errors.js +41 -0
  11. package/build/errors.js.map +1 -0
  12. package/build/i18n/en.json +8 -0
  13. package/build/i18n/wallet-en.json +99 -0
  14. package/build/i18n.d.ts +2 -0
  15. package/build/i18n.d.ts.map +1 -0
  16. package/build/i18n.js +6 -0
  17. package/build/i18n.js.map +1 -0
  18. package/build/index.d.ts +9 -0
  19. package/build/index.d.ts.map +1 -0
  20. package/build/index.js +8 -0
  21. package/build/index.js.map +1 -0
  22. package/build/model.d.ts +4 -0
  23. package/build/model.d.ts.map +1 -0
  24. package/build/model.js +41 -0
  25. package/build/model.js.map +1 -0
  26. package/build/plugins/ed25519owl.d.ts +3 -0
  27. package/build/plugins/ed25519owl.d.ts.map +1 -0
  28. package/build/plugins/ed25519owl.js +23 -0
  29. package/build/plugins/ed25519owl.js.map +1 -0
  30. package/build/plugins/exports.d.ts +3 -0
  31. package/build/plugins/exports.d.ts.map +1 -0
  32. package/build/plugins/exports.js +3 -0
  33. package/build/plugins/exports.js.map +1 -0
  34. package/build/plugins/index.d.ts +2 -0
  35. package/build/plugins/index.d.ts.map +1 -0
  36. package/build/plugins/index.js +6 -0
  37. package/build/plugins/index.js.map +1 -0
  38. package/build/types.d.ts +64 -0
  39. package/build/types.d.ts.map +1 -0
  40. package/build/types.js +2 -0
  41. package/build/types.js.map +1 -0
  42. package/build/utils/index.d.ts +4 -0
  43. package/build/utils/index.d.ts.map +1 -0
  44. package/build/utils/index.js +4 -0
  45. package/build/utils/index.js.map +1 -0
  46. package/build/utils/key.d.ts +3 -0
  47. package/build/utils/key.d.ts.map +1 -0
  48. package/build/utils/key.js +24 -0
  49. package/build/utils/key.js.map +1 -0
  50. package/build/utils/mnemonic.d.ts +6 -0
  51. package/build/utils/mnemonic.d.ts.map +1 -0
  52. package/build/utils/mnemonic.js +15 -0
  53. package/build/utils/mnemonic.js.map +1 -0
  54. package/build/utils.d.ts +5 -0
  55. package/build/utils.d.ts.map +1 -0
  56. package/build/utils.js +24 -0
  57. package/build/utils.js.map +1 -0
  58. package/build/wallet/index.d.ts +2 -0
  59. package/build/wallet/index.d.ts.map +1 -0
  60. package/build/wallet/index.js +2 -0
  61. package/build/wallet/index.js.map +1 -0
  62. package/build/wallet/types.d.ts +30 -0
  63. package/build/wallet/types.d.ts.map +1 -0
  64. package/build/wallet/types.js +2 -0
  65. package/build/wallet/types.js.map +1 -0
  66. package/build/wallet.d.ts +3 -0
  67. package/build/wallet.d.ts.map +1 -0
  68. package/build/wallet.js +151 -0
  69. package/build/wallet.js.map +1 -0
  70. package/package.json +42 -0
  71. package/src/consts.ts +16 -0
  72. package/src/errors.ts +51 -0
  73. package/src/i18n/en.json +8 -0
  74. package/src/i18n/wallet-en.json +99 -0
  75. package/src/i18n.ts +7 -0
  76. package/src/index.ts +9 -0
  77. package/src/model.ts +52 -0
  78. package/src/plugins/ed25519owl.ts +35 -0
  79. package/src/plugins/exports.ts +3 -0
  80. package/src/plugins/index.ts +9 -0
  81. package/src/types.ts +89 -0
  82. package/src/utils/index.ts +4 -0
  83. package/src/utils/key.ts +29 -0
  84. package/src/utils/mnemonic.ts +24 -0
  85. package/src/utils.ts +34 -0
  86. package/src/wallet/index.ts +2 -0
  87. package/src/wallet/types.ts +43 -0
  88. package/src/wallet.ts +183 -0
  89. package/tsconfig.json +16 -0
  90. package/tsconfig.tsbuildinfo +1 -0
@@ -0,0 +1,151 @@
1
+ import { DIDInitializationError, DIDKeyError, DIDWalletError } from './errors.js';
2
+ import { generateMnemonic, toEntropy, toMnemonic, toSeed } from './utils/mnemonic.js';
3
+ import { KEY_OWL, MASTER } from './consts.js';
4
+ import { plugins } from './plugins/index.js';
5
+ import { ed25519owlPluginBuilder } from './plugins/ed25519owl.js';
6
+ import { produceKey } from './utils/key.js';
7
+ import { makeDidKeyModel } from './model.js';
8
+ import { mataToPath, matchMeta } from './utils.js';
9
+ export const makeWallet = async (store, opts) => {
10
+ if (await store.master.load(MASTER) == null) {
11
+ if (!opts?.force && !opts?.allowEmpty) {
12
+ throw new DIDInitializationError('master');
13
+ }
14
+ }
15
+ const type = opts?.type ?? KEY_OWL;
16
+ if (plugins[type] == null) {
17
+ if (opts?.allowCustomType) {
18
+ plugins[type] = ed25519owlPluginBuilder(type);
19
+ }
20
+ }
21
+ const wallet = {
22
+ store,
23
+ generate: async (opts) => {
24
+ const mnemonic = generateMnemonic(opts);
25
+ const seed = {
26
+ entropy: toEntropy(mnemonic),
27
+ seed: toSeed(mnemonic)
28
+ };
29
+ const master = produceKey(seed.seed, type);
30
+ await store.master.save({ id: MASTER, ...master, ...seed });
31
+ },
32
+ mnemonic: async (crash) => {
33
+ const master = await store.master.get(MASTER);
34
+ if (master.seed == null) {
35
+ if (crash) {
36
+ throw new DIDWalletError('mnemonic');
37
+ }
38
+ return false;
39
+ }
40
+ return toMnemonic(master.seed);
41
+ },
42
+ master: async () => makeDidKeyModel(await store.master.get(MASTER)),
43
+ add: async (key, meta) => {
44
+ if (key.keyPair == null || key.keyPair.privateKey == null) {
45
+ throw new DIDWalletError('key:non-managable');
46
+ }
47
+ const stored = await store.keys.load(key.exportAddress());
48
+ if (stored != null) {
49
+ throw new DIDWalletError('key:exists');
50
+ }
51
+ await store.keys.create({ id: key.exportAddress(), ...key.keyPair });
52
+ await store.meta.create({ ...meta, id: key.exportAddress() });
53
+ },
54
+ meta: async (key) => {
55
+ if (typeof key === 'string') {
56
+ if (null == await store.keys.load(key)) {
57
+ throw new DIDWalletError('no:key');
58
+ }
59
+ return store.meta.get(key);
60
+ }
61
+ return wallet.meta(key.exportAddress());
62
+ },
63
+ update: async (key, meta) => {
64
+ if (key.keyPair == null) {
65
+ throw new DIDWalletError('key:non-managable');
66
+ }
67
+ const did = key.exportAddress();
68
+ const returnKey = await store.keys.save({ id: did, ...key.keyPair });
69
+ const returnMeta = await store.meta.save({ ...meta, id: did });
70
+ return [makeDidKeyModel(returnKey), returnMeta];
71
+ },
72
+ get: async (did) => {
73
+ if (await store.keys.load(did) == null) {
74
+ throw new DIDWalletError('no:key');
75
+ }
76
+ return makeDidKeyModel(await store.keys.get(did));
77
+ },
78
+ find: async (meta) => {
79
+ const result = await store.meta.list(Object.fromEntries(Object.entries(meta).filter(([_, v]) => typeof v !== 'boolean')));
80
+ const keys = await Promise.all(result.items.filter(item => matchMeta(item, meta))
81
+ .map(async (meta) => store.keys.get(meta.id)));
82
+ return keys.map(key => makeDidKeyModel(key));
83
+ },
84
+ provide: async (meta) => {
85
+ const found = await wallet.find(meta);
86
+ if (found.length === 0) {
87
+ const path = mataToPath(meta);
88
+ const master = await wallet.master();
89
+ const key = master.derive(path);
90
+ if (meta.name == null) {
91
+ throw new DIDKeyError('no:name');
92
+ }
93
+ const metaToSave = {
94
+ ...meta, id: meta.id ?? key.exportAddress(), name: meta.name
95
+ };
96
+ await wallet.add(key, metaToSave);
97
+ return [key];
98
+ }
99
+ return found;
100
+ },
101
+ remove: async (did) => {
102
+ if (typeof did === 'object') {
103
+ if ("keyPair" in did) {
104
+ return wallet.remove(did.exportAddress());
105
+ }
106
+ const keys = await wallet.find(did);
107
+ const key = await keys.reduce(async (_, key) => {
108
+ return wallet.remove(key);
109
+ }, Promise.resolve(null));
110
+ if (key == null) {
111
+ throw new DIDWalletError('no:key');
112
+ }
113
+ return key;
114
+ }
115
+ const key = await store.keys.load(did);
116
+ if (key == null) {
117
+ throw new DIDWalletError('no:key');
118
+ }
119
+ await store.keys.delete(key);
120
+ const model = makeDidKeyModel(key);
121
+ await store.meta.delete(model.exportAddress());
122
+ return model;
123
+ },
124
+ all: async () => {
125
+ const result = [];
126
+ let more = false;
127
+ do {
128
+ const found = await store.keys.list({ pager: { page: Math.floor(result.length / 100), size: 100 } });
129
+ result.push(...found.items);
130
+ more = found.pager?.total != null && found.pager.total > result.length;
131
+ } while (more);
132
+ return result.map(key => makeDidKeyModel(key));
133
+ },
134
+ allMeta: async () => {
135
+ const result = [];
136
+ let more = false;
137
+ do {
138
+ // const found = await store.keys.list({ pager: { page: Math.floor(result.length / 100), size: 100 } })
139
+ const found = await store.meta.list({ pager: { page: Math.floor(result.length / 100), size: 100 } });
140
+ result.push(...found.items);
141
+ more = found.pager?.total != null && found.pager.total > result.length;
142
+ } while (more);
143
+ return result;
144
+ }
145
+ };
146
+ if (opts?.force) {
147
+ await wallet.generate(opts.mnemonic);
148
+ }
149
+ return wallet;
150
+ };
151
+ //# sourceMappingURL=wallet.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"wallet.js","sourceRoot":"","sources":["../src/wallet.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,sBAAsB,EAAE,WAAW,EAAE,cAAc,EAAE,MAAM,aAAa,CAAA;AAEjF,OAAO,EAAE,gBAAgB,EAAE,SAAS,EAAE,UAAU,EAAE,MAAM,EAAE,MAAM,qBAAqB,CAAA;AACrF,OAAO,EAAE,OAAO,EAAE,MAAM,EAAE,MAAM,aAAa,CAAA;AAC7C,OAAO,EAAE,OAAO,EAAE,MAAM,oBAAoB,CAAA;AAC5C,OAAO,EAAE,uBAAuB,EAAE,MAAM,yBAAyB,CAAA;AACjE,OAAO,EAAE,UAAU,EAAE,MAAM,gBAAgB,CAAA;AAC3C,OAAO,EAAE,eAAe,EAAE,MAAM,YAAY,CAAA;AAC5C,OAAO,EAAE,UAAU,EAAE,SAAS,EAAE,MAAM,YAAY,CAAA;AAElD,MAAM,CAAC,MAAM,UAAU,GAAG,KAAK,EAAE,KAAe,EAAE,IAA2B,EAAE,EAAE;IAC/E,IAAI,MAAM,KAAK,CAAC,MAAM,C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Q,CAAC,IAAI,CAAC,QAAQ,CAAC,CAAA;IACtC,CAAC;IAED,OAAO,MAAM,CAAA;AACf,CAAC,CAAA"}
package/package.json ADDED
@@ -0,0 +1,42 @@
1
+ {
2
+ "name": "@owlmeans/did",
3
+ "version": "0.1.0",
4
+ "type": "module",
5
+ "scripts": {
6
+ "build": "tsc -b",
7
+ "dev": "sleep 153 && nodemon -e ts,tsx,json --watch src --exec \"tsc -p ./tsconfig.json\"",
8
+ "watch": "tsc -b -w --preserveWatchOutput --pretty"
9
+ },
10
+ "main": "build/index.js",
11
+ "module": "build/index.js",
12
+ "types": "build/index.d.ts",
13
+ "exports": {
14
+ ".": {
15
+ "import": "./build/index.js",
16
+ "require": "./build/index.js",
17
+ "default": "./build/index.js",
18
+ "module": "./build/index.js",
19
+ "types": "./build/index.d.ts"
20
+ }
21
+ },
22
+ "dependencies": {
23
+ "@noble/curves": "^1.6.0",
24
+ "@noble/hashes": "^1.5.0",
25
+ "@owlmeans/auth": "^0.1.0",
26
+ "@owlmeans/basic-keys": "^0.1.0",
27
+ "@owlmeans/error": "^0.1.0",
28
+ "@owlmeans/i18n": "^0.1.0",
29
+ "@owlmeans/resource": "^0.1.0",
30
+ "@scure/base": "^1.1.9",
31
+ "@scure/bip39": "^1.4.0"
32
+ },
33
+ "devDependencies": {
34
+ "nodemon": "^3.1.7",
35
+ "npm-check": "^6.0.1",
36
+ "typescript": "^5.6.3"
37
+ },
38
+ "private": false,
39
+ "publishConfig": {
40
+ "access": "public"
41
+ }
42
+ }
package/src/consts.ts ADDED
@@ -0,0 +1,16 @@
1
+
2
+ export const KEY_OWL = 'owlmk'
3
+
4
+ export const KP_SEP = '/'
5
+
6
+ export const MAX_DEPTH = 6
7
+
8
+ export const PROFILE_PREFIX = 'profile'
9
+
10
+ export const ENTITY_PREFIX = 'entity'
11
+
12
+ export const SERVICE_PREFIX = 'service'
13
+
14
+ export const PREFIX_SEP = '.'
15
+
16
+ export const MASTER = '_master_key'
package/src/errors.ts ADDED
@@ -0,0 +1,51 @@
1
+ import { ResilientError } from '@owlmeans/error'
2
+
3
+ export class DIDError extends ResilientError {
4
+ public static override typeName = 'DIDError'
5
+
6
+ constructor(message: string = 'error') {
7
+ super(DIDError.typeName, `did:${message}`)
8
+ }
9
+ }
10
+
11
+ export class DIDKeyError extends DIDError {
12
+ public static override typeName = `${DIDError.typeName}:Key`
13
+
14
+ constructor(message: string = 'error') {
15
+ super(`wallet:${message}`)
16
+ this.type = DIDKeyError.typeName
17
+ }
18
+ }
19
+
20
+ export class DIDWalletError extends DIDError {
21
+ public static override typeName = `${DIDError.typeName}:Wallet`
22
+
23
+ constructor(message: string = 'error') {
24
+ super(`wallet:${message}`)
25
+ this.type = DIDWalletError.typeName
26
+ }
27
+ }
28
+
29
+ export class DIDWalletPermissionError extends DIDWalletError {
30
+ public static override typeName = `${DIDWalletError.typeName}:Permission`
31
+
32
+ constructor(message: string = 'error') {
33
+ super(`permission:${message}`)
34
+ this.type = DIDWalletPermissionError.typeName
35
+ }
36
+ }
37
+
38
+ export class DIDInitializationError extends DIDWalletError {
39
+ public static override typeName = `${DIDWalletError.typeName}:Initialization`
40
+
41
+ constructor(message: string = 'error') {
42
+ super(`init:${message}`)
43
+ this.type = DIDInitializationError.typeName
44
+ }
45
+ }
46
+
47
+ ResilientError.registerErrorClass(DIDError)
48
+ ResilientError.registerErrorClass(DIDKeyError)
49
+ ResilientError.registerErrorClass(DIDWalletError)
50
+ ResilientError.registerErrorClass(DIDWalletPermissionError)
51
+ ResilientError.registerErrorClass(DIDInitializationError)
@@ -0,0 +1,8 @@
1
+ {
2
+ "address": {
3
+ "hint": "DID address"
4
+ },
5
+ "master": {
6
+ "title": "Master key"
7
+ }
8
+ }
@@ -0,0 +1,99 @@
1
+ {
2
+ "createKey": {
3
+ "title": "Create new key",
4
+ "description": "Some orgnanization or service asks you to create a new crypto key",
5
+ "unknown-reason": "Be cautious! We do not know why you are asked to create a new key.",
6
+ "proceed": {
7
+ "label": "Confirm"
8
+ },
9
+ "cancel": {
10
+ "label": "Cancel"
11
+ }
12
+ },
13
+ "getEntityKey": {
14
+ "title": "Get public key for an ortnaization",
15
+ "description": "Some service asks you to provide a public key and related data for an orgnanization profile",
16
+ "unknown-reason": "Be cautious! We do not know why you are asked to provide this key.",
17
+ "proceed": {
18
+ "label": "Confirm"
19
+ },
20
+ "cancel": {
21
+ "label": "Cancel"
22
+ }
23
+ },
24
+ "sign": {
25
+ "title": "Sign with the key",
26
+ "description": "Some service asks you to sign a document with your key",
27
+ "unknown-reason": "Be cautious! We do not know why you are asked to make this signature.",
28
+ "proceed": {
29
+ "label": "Confirm"
30
+ },
31
+ "cancel": {
32
+ "label": "Cancel"
33
+ }
34
+ },
35
+ "masterSign": {
36
+ "title": "Sign with the primary key",
37
+ "description": "Some service asks you to sign a document with your primary key, that most likely should be used only with OwlMeans services",
38
+ "unknown-reason": "Be cautious! We do not know why you are asked to make this signature.",
39
+ "proceed": {
40
+ "label": "Confirm"
41
+ },
42
+ "cancel": {
43
+ "label": "Cancel"
44
+ }
45
+ },
46
+ "getMasterDid": {
47
+ "title": "Get your primary DID",
48
+ "description": "Some service asks for your primary decentralized address, that most likely should be shared only with OwlMeans services",
49
+ "unknown-reason": "Be cautious! We do not know why you are asked to share your primary DID.",
50
+ "proceed": {
51
+ "label": "Confirm"
52
+ },
53
+ "cancel": {
54
+ "label": "Cancel"
55
+ }
56
+ },
57
+ "selectKey": {
58
+ "title": "Choose a key",
59
+ "description": "Some service asks you to choose a key to get its public detials",
60
+ "unknown-reason": "Be cautious! We do not know why you are asked to provide public info of one of your keys.",
61
+ "proceed": {
62
+ "label": "Confirm"
63
+ },
64
+ "cancel": {
65
+ "label": "Cancel"
66
+ }
67
+ },
68
+ "getPublicDetails": {
69
+ "title": "Get public key",
70
+ "description": "Some service asks for a public key linked to your organization profile",
71
+ "unknown-reason": "Be cautious! We do not know why you are asked to share your public key.",
72
+ "proceed": {
73
+ "label": "Confirm"
74
+ },
75
+ "cancel": {
76
+ "label": "Cancel"
77
+ }
78
+ },
79
+ "requestPermissions": {
80
+ "title": "Requested permissions",
81
+ "description": "Some service asks to perform an action sequence with your wallet",
82
+ "unknown-reason": "Be cautious! We do not know why you are asked to perform these actions.",
83
+ "proceed": {
84
+ "label": "Confirm"
85
+ },
86
+ "cancel": {
87
+ "label": "Cancel"
88
+ },
89
+ "method": {
90
+ "createKey": "Create new crypto key",
91
+ "getEntityKey": "Get an organization public key",
92
+ "sign": "Sign some data with your key",
93
+ "masterSign": "Sign data with your primary key",
94
+ "getMasterDid": "Get your primary ID",
95
+ "selectKey": "Get one of your public keys",
96
+ "getPublicDetails": "Get your public key"
97
+ }
98
+ }
99
+ }
package/src/i18n.ts ADDED
@@ -0,0 +1,7 @@
1
+ import { addI18nLib } from '@owlmeans/i18n'
2
+
3
+ import en from './i18n/en.json' with { type: 'json' }
4
+ import walletEn from './i18n/wallet-en.json' with { type: 'json' }
5
+
6
+ addI18nLib('en', 'keys', en, { ns: 'did' })
7
+ addI18nLib('en', 'wallet', walletEn, { ns: 'did' })
package/src/index.ts ADDED
@@ -0,0 +1,9 @@
1
+
2
+ export type * from './types.js'
3
+ export * from './i18n.js'
4
+ export * from './consts.js'
5
+ export * from './wallet.js'
6
+ export * from './model.js'
7
+ export * from './errors.js'
8
+ export * from './plugins/index.js'
9
+ export * from './wallet/index.js'
package/src/model.ts ADDED
@@ -0,0 +1,52 @@
1
+ import { makeKeyPairModel, plugins, type KeyPair } from '@owlmeans/basic-keys'
2
+ import type { DIDKeyModel } from './types.js'
3
+ import { DIDKeyError } from './errors.js'
4
+ import { assertType, prepareKey } from '@owlmeans/basic-keys/utils'
5
+ import { base64 } from '@scure/base'
6
+ import { KP_SEP, MAX_DEPTH } from './consts.js'
7
+
8
+ export const makeDidKeyModel: (input?: KeyPair | string) => DIDKeyModel = input => {
9
+ if (typeof input == 'string') {
10
+ const plugin = plugins[input]
11
+ if (plugin != null && (plugin.fromSeed == null || plugin.derive == null)) {
12
+ throw new DIDKeyError(`non-derivable:${input}`)
13
+ }
14
+ }
15
+ const model: DIDKeyModel = makeKeyPairModel(input) as DIDKeyModel
16
+
17
+ model.derive = path => {
18
+ if (model.keyPair == null) {
19
+ throw new DIDKeyError('no:keypair')
20
+ }
21
+ assertType(model.keyPair?.type)
22
+ if (model.keyPair.privateKey == null) {
23
+ throw new DIDKeyError('no:pk')
24
+ }
25
+
26
+ const items = path.split(KP_SEP).filter(item => item != null && item !== '')
27
+
28
+ if (items.length > MAX_DEPTH) {
29
+ throw new DIDKeyError('too:deep')
30
+ }
31
+
32
+ const item = items.shift()
33
+ if (item == null) {
34
+ throw new DIDKeyError('no:path')
35
+ }
36
+
37
+ const newKey = [model.keyPair.type, base64.encode(
38
+ plugins[model.keyPair.type].derive!(prepareKey(model.keyPair.privateKey), item)
39
+ )].join(':')
40
+
41
+ const derived = makeDidKeyModel(newKey)
42
+ if (derived.keyPair == null) {
43
+ throw new DIDKeyError('derived:missgenerated')
44
+ }
45
+ derived.keyPair.path = path
46
+ derived.keyPair.parent = model.exportAddress()
47
+
48
+ return items.length > 0 ? derived.derive(items.join(KP_SEP)) : derived
49
+ }
50
+
51
+ return model
52
+ }
@@ -0,0 +1,35 @@
1
+ import { ed25519 } from '@noble/curves/ed25519'
2
+ import type { KeyPlugin } from '@owlmeans/basic-keys/plugins'
3
+ import { KEY_OWL } from '../consts.js'
4
+ import { base58, utf8 } from '@scure/base'
5
+ import { concatBytes, randomBytes } from '@noble/hashes/utils'
6
+ import { hmac } from '@noble/hashes/hmac'
7
+ import { sha512 } from '@noble/hashes/sha512'
8
+ import { keccak_256 } from '@noble/hashes/sha3'
9
+
10
+ export const ed25519owlPluginBuilder = (type: string = KEY_OWL): KeyPlugin => {
11
+ const key: KeyPlugin = {
12
+ type: type,
13
+
14
+ random: () => key.fromSeed!(randomBytes(32)),
15
+
16
+ derive: (pk, path) =>
17
+ hmac(sha512, pk.slice(32), concatBytes(pk.slice(0, 32), utf8.decode(path))),
18
+
19
+ fromSeed: (seed: Uint8Array) => hmac(sha512, utf8.decode(type), seed),
20
+
21
+ sign: (data, pk) => ed25519.sign(data, pk.slice(0, 32)),
22
+
23
+ verify: (data, signature, pub) => ed25519.verify(signature, data, pub),
24
+
25
+ toPublic: pk => ed25519.getPublicKey(pk.slice(0, 32)),
26
+
27
+ toAdress: pub => base58.encode(keccak_256(pub.slice(4)).slice(-20)),
28
+
29
+ encrypt: () => { throw new Error(`${key.type}:encryption-support`) },
30
+
31
+ decrypt: () => { throw new Error(`${key.type}:encryption-support`) }
32
+ }
33
+
34
+ return key
35
+ }
@@ -0,0 +1,3 @@
1
+
2
+ export * from './index.js'
3
+ export * from './ed25519owl.js'
@@ -0,0 +1,9 @@
1
+
2
+ import { plugins as basicPlugins } from '@owlmeans/basic-keys'
3
+ import { ed25519owlPluginBuilder } from './ed25519owl.js'
4
+
5
+ const owlPluginInstance = ed25519owlPluginBuilder()
6
+
7
+ basicPlugins[owlPluginInstance.type] = owlPluginInstance
8
+
9
+ export const plugins = basicPlugins
package/src/types.ts ADDED
@@ -0,0 +1,89 @@
1
+ import type { Profile } from '@owlmeans/auth'
2
+ import type { KeyPair, KeyPairModel } from '@owlmeans/basic-keys'
3
+ import type { Resource, ResourceRecord } from '@owlmeans/resource'
4
+
5
+ export interface DIDWallet {
6
+ store: DIDStore
7
+
8
+ generate: (opts?: MnemonicOptions) => Promise<void>
9
+
10
+ mnemonic: (crash?: boolean) => Promise<string | false>
11
+
12
+ master: () => Promise<DIDKeyModel>
13
+
14
+ add: (key: DIDKeyModel, meta: KeyMeta) => Promise<void>
15
+
16
+ meta: (key: string | DIDKeyModel) => Promise<KeyMeta>
17
+
18
+ update: (key: DIDKeyModel, meta: KeyMeta) => Promise<[DIDKeyModel, KeyMeta]>
19
+
20
+ get: (did: string) => Promise<DIDKeyModel | null>
21
+
22
+ find: (meta: Partial<KeyMeta>) => Promise<DIDKeyModel[]>
23
+
24
+ provide: (mate: Partial<KeyMeta>) => Promise<DIDKeyModel[]>
25
+
26
+ remove: (did: string | KeyMeta | DIDKeyModel) => Promise<DIDKeyModel>
27
+
28
+ all: () => Promise<DIDKeyModel[]>
29
+
30
+ allMeta: () => Promise<KeyMeta[]>
31
+ }
32
+
33
+ export interface MakeDIDWalletOptions {
34
+ force?: boolean
35
+ allowEmpty?: boolean
36
+ mnemonic?: MnemonicOptions
37
+ type?: string
38
+ allowCustomType?: boolean
39
+ }
40
+
41
+ export interface DIDStore {
42
+ master: MasterResource
43
+ keys: KeyPairResource
44
+ meta: KeyMetaResource
45
+ }
46
+
47
+ export interface MasterResource extends Resource<KeySeedRecord> {
48
+ }
49
+
50
+ export interface KeyPairResource extends Resource<KeyPairRecord> {
51
+ }
52
+
53
+ export interface KeyMetaResource extends Resource<KeyMetaRecord> {
54
+ }
55
+
56
+ export interface KeyPairRecord extends ResourceRecord, DIDKeyPair {
57
+ }
58
+
59
+ export interface KeySeedRecord extends ResourceRecord, KeySeed {
60
+ }
61
+
62
+ export interface KeyMetaRecord extends ResourceRecord, KeyMeta {
63
+ id: string
64
+ }
65
+
66
+ export interface KeySeed extends DIDKeyPair {
67
+ entropy?: string
68
+ seed?: string
69
+ }
70
+
71
+ export interface KeyMeta extends Partial<Profile> {
72
+ id: string
73
+ name: string
74
+ entityId?: string
75
+ }
76
+
77
+ export interface MnemonicOptions {
78
+ size?: number
79
+ }
80
+
81
+ export interface DIDKeyPair extends KeyPair {
82
+ path?: string
83
+ parent?: string
84
+ }
85
+
86
+ export interface DIDKeyModel extends KeyPairModel {
87
+ keyPair?: DIDKeyPair
88
+ derive: (path: string) => DIDKeyModel
89
+ }
@@ -0,0 +1,4 @@
1
+
2
+ export * from './mnemonic.js'
3
+ export * from './key.js'
4
+ export * from '../utils.js'
@@ -0,0 +1,29 @@
1
+
2
+ import { assertType } from '@owlmeans/basic-keys/utils'
3
+ import { KEY_OWL } from '../consts.js'
4
+ import { base64 } from '@scure/base'
5
+ import { plugins } from '../plugins/index.js'
6
+ import { DIDKeyError } from '../errors.js'
7
+ import type { DIDKeyPair } from '../types.js'
8
+
9
+ export const produceKey = (seed: string | Uint8Array, type: string = KEY_OWL) => {
10
+ if (typeof seed === 'string') {
11
+ seed = base64.decode(seed)
12
+ }
13
+ assertType(type)
14
+
15
+ if (plugins[type].fromSeed == null) {
16
+ throw new DIDKeyError(`non-derivable:${type}`)
17
+ }
18
+
19
+ const privateKey = plugins[type].fromSeed(seed)
20
+ const publicKey = plugins[type].toPublic(privateKey)
21
+ const keyPair: DIDKeyPair = {
22
+ privateKey: base64.encode(privateKey),
23
+ publicKey: base64.encode(publicKey),
24
+ address: plugins[type].toAdress(publicKey),
25
+ type,
26
+ }
27
+
28
+ return keyPair
29
+ }
@@ -0,0 +1,24 @@
1
+ import type { MnemonicOptions } from '../types.js'
2
+ import { generateMnemonic as generate, mnemonicToSeedSync, entropyToMnemonic } from '@scure/bip39'
3
+ import { hex, base64 } from '@scure/base'
4
+ import { wordlist } from '@scure/bip39/wordlists/english'
5
+
6
+ export const generateMnemonic = (opts?: MnemonicOptions): string => {
7
+ const size = opts?.size ?? 18
8
+
9
+ if (size < 12 || size > 24) {
10
+ throw new SyntaxError('Mnemonic size should be between 12 and 24')
11
+ }
12
+
13
+ const strength = Math.round(256 * size / 24)
14
+ return generate(wordlist as any, strength + (32 - strength % 32))
15
+ }
16
+
17
+ export const toSeed = (mnemonic: string): string =>
18
+ base64.encode(mnemonicToSeedSync(mnemonic))
19
+
20
+ export const toEntropy = (mnemonic: string): string =>
21
+ hex.encode(mnemonicToSeedSync(mnemonic))
22
+
23
+ export const toMnemonic = (seed: string): string =>
24
+ entropyToMnemonic(hex.decode(seed), wordlist as any)