@owlmeans/basic-keys 0.1.18-rc.47 → 0.1.18-rc.48

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 (49) hide show
  1. package/README.md +12 -12
  2. package/agent-meta/manifest.json +2 -2
  3. package/agent-meta/skills/basic-keys/SKILL.md +13 -13
  4. package/build/auth/types.d.ts +10 -0
  5. package/build/auth/types.d.ts.map +1 -0
  6. package/build/auth/types.js +2 -0
  7. package/build/auth/types.js.map +1 -0
  8. package/build/auth.d.ts +3 -4
  9. package/build/auth.d.ts.map +1 -1
  10. package/build/auth.js +43 -39
  11. package/build/auth.js.map +1 -1
  12. package/build/bin.js +0 -0
  13. package/build/helper/types.d.ts +11 -0
  14. package/build/helper/types.d.ts.map +1 -0
  15. package/build/helper/types.js +2 -0
  16. package/build/helper/types.js.map +1 -0
  17. package/build/helper.d.ts +4 -1
  18. package/build/helper.d.ts.map +1 -1
  19. package/build/helper.js +21 -15
  20. package/build/helper.js.map +1 -1
  21. package/build/index.d.ts +2 -0
  22. package/build/index.d.ts.map +1 -1
  23. package/build/index.js.map +1 -1
  24. package/build/keypair.js +2 -2
  25. package/build/keypair.js.map +1 -1
  26. package/build/model.d.ts +2 -2
  27. package/build/model.d.ts.map +1 -1
  28. package/build/model.js +16 -16
  29. package/build/model.js.map +1 -1
  30. package/build/utils/types.d.ts +16 -0
  31. package/build/utils/types.d.ts.map +1 -0
  32. package/build/utils/types.js +2 -0
  33. package/build/utils/types.js.map +1 -0
  34. package/build/utils.d.ts +3 -4
  35. package/build/utils.d.ts.map +1 -1
  36. package/build/utils.js +25 -21
  37. package/build/utils.js.map +1 -1
  38. package/package.json +3 -3
  39. package/src/auth/types.ts +17 -0
  40. package/src/auth.ts +51 -44
  41. package/src/helper/types.ts +11 -0
  42. package/src/helper.ts +26 -16
  43. package/src/index.ts +2 -0
  44. package/src/keypair.ts +2 -2
  45. package/src/model.ts +17 -17
  46. package/src/utils/types.ts +15 -0
  47. package/src/utils.ts +27 -20
  48. package/tests/auth.spec.ts +5 -5
  49. package/tests/keys.spec.ts +3 -3
package/README.md CHANGED
@@ -12,7 +12,7 @@ ED25519 and XChaCha20 cryptographic key operations: generation, signing, verific
12
12
  ## Installation
13
13
 
14
14
  ```bash
15
- bun add @owlmeans/basic-keys@^0.1.18-rc.47
15
+ bun add @owlmeans/basic-keys@^0.1.18-rc.48
16
16
  ```
17
17
 
18
18
  ## Usage
@@ -32,19 +32,19 @@ const address = pair.exportAddress() // derive address from public key
32
32
  Load from an existing public key:
33
33
 
34
34
  ```typescript
35
- import { fromPubKey, matchAddress } from '@owlmeans/basic-keys'
35
+ import { keyHelper } from '@owlmeans/basic-keys'
36
36
 
37
- const model = fromPubKey('ed25519:ABC123...')
38
- const isMatch = matchAddress(knownAddress, publicKeyStr)
37
+ const model = keyHelper.fromPubKey('ed25519:ABC123...')
38
+ const isMatch = keyHelper.matchAddress(knownAddress, publicKeyStr)
39
39
  ```
40
40
 
41
41
  Pack authentication credentials for a request:
42
42
 
43
43
  ```typescript
44
- import { packAuthCredentials, unpackAuthCredentials } from '@owlmeans/basic-keys'
44
+ import { authCredentialsHelper } from '@owlmeans/basic-keys'
45
45
 
46
- const signed = await packAuthCredentials(authObj, extraData, keyPairModel)
47
- const { isValid, extras } = await unpackAuthCredentials(signed, keyPairModel)
46
+ const signed = await authCredentialsHelper.packAuthCredentials(authObj, extraData, keyPairModel)
47
+ const { isValid, extras } = await authCredentialsHelper.unpackAuthCredentials(signed, keyPairModel)
48
48
  ```
49
49
 
50
50
  ## API
@@ -53,19 +53,19 @@ const { isValid, extras } = await unpackAuthCredentials(signed, keyPairModel)
53
53
 
54
54
  Creates a `KeyPairModel` from an existing `KeyPair` object, encoded private key string, or algorithm type string. Generates a new ED25519 key pair when called with no arguments.
55
55
 
56
- ### `fromPubKey(pubKey, type?): KeyPairModel`
56
+ ### `keyHelper.fromPubKey(pubKey, type?): KeyPairModel`
57
57
 
58
58
  Creates a verify-only `KeyPairModel` from a public key string. Supports `"type:key"` format.
59
59
 
60
- ### `matchAddress(address, pubKey): boolean`
60
+ ### `keyHelper.matchAddress(address, pubKey): boolean`
61
61
 
62
62
  Returns true if `address` was derived from `pubKey`.
63
63
 
64
- ### `packAuthCredentials(auth, extra, signer): Promise<AuthCredentials>`
64
+ ### `authCredentialsHelper.packAuthCredentials(auth, extra, signer): Promise<AuthCredentials>`
65
65
 
66
66
  Signs auth credentials, optionally embedding extra data in the credential field.
67
67
 
68
- ### `unpackAuthCredentials(auth, verifier?): Promise<UnpackedAuthCredentials>`
68
+ ### `authCredentialsHelper.unpackAuthCredentials(auth, verifier?): Promise<UnpackedAuthCredentials>`
69
69
 
70
70
  Extracts and optionally verifies signed auth credentials. Returns `isValid` boolean when a verifier is provided.
71
71
 
@@ -89,7 +89,7 @@ This package ships embedded agent skills under `agent-meta/`. After installing y
89
89
  your project's skill store (`.agents/skills/`):
90
90
 
91
91
  ```sh
92
- npx @owlmeans/agent-skills@^0.1.18-rc.48
92
+ npx @owlmeans/agent-skills@^0.1.18-rc.49
93
93
  ```
94
94
 
95
95
  The embedded files are version-matched to this package release. Do not edit them
@@ -1,8 +1,8 @@
1
1
  {
2
2
  "schemaVersion": 2,
3
3
  "package": "@owlmeans/basic-keys",
4
- "version": "0.1.18-rc.47",
5
- "generatedAt": "2026-10-05T08:32:19.086Z",
4
+ "version": "0.1.18-rc.48",
5
+ "generatedAt": "2026-10-05T21:03:06.361Z",
6
6
  "canonicalRepo": "https://github.com/owlmeans/common",
7
7
  "entries": [
8
8
  {
@@ -8,7 +8,7 @@ user-invocable: false
8
8
  # @owlmeans/basic-keys
9
9
 
10
10
  **Layer:** Core
11
- **Install:** `"@owlmeans/basic-keys": "^0.1.18-rc.47"` in `dependencies`
11
+ **Install:** `"@owlmeans/basic-keys": "^0.1.18-rc.48"` in `dependencies`
12
12
 
13
13
  ## Key Exports
14
14
 
@@ -18,9 +18,9 @@ user-invocable: false
18
18
  | `KeyPair` | Key pair shape: `{ privateKey, publicKey, address, type }`, keys base64 |
19
19
  | `KeyPairModel` | `sign` / `verify` / `encrypt` / `decrypt` / `dcrpt` / `export*` |
20
20
  | `KeyType` | `ED25519` (`'ed25519'`, signing) and `XCHACHA` (`'xchacha'`, encryption) |
21
- | `fromPubKey(key, type?)`, `matchAddress(address, pubKey)` | Verify-only model from a public key; address check |
22
- | `packAuthCredentials(auth, extra, signer)` | Sign a credential payload into `AuthCredentials.credential` |
23
- | `unpackAuthCredentials(auth, verifier?)` | Split that back into `{ unsigned, signature, extras, isValid }` |
21
+ | `keyHelper.fromPubKey(key, type?)`, `keyHelper.matchAddress(address, pubKey)` | Verify-only model from a public key; address check |
22
+ | `authCredentialsHelper.packAuthCredentials(auth, extra, signer)` | Sign a credential payload into `AuthCredentials.credential` |
23
+ | `authCredentialsHelper.unpackAuthCredentials(auth, verifier?)` | Split that back into `{ unsigned, signature, extras, isValid }` |
24
24
  | `plugins` | The key-type registry, keyed by type string |
25
25
 
26
26
  ## Subpath Exports
@@ -29,7 +29,7 @@ user-invocable: false
29
29
  again, and the **`KeyPlugin` type**. `KeyPlugin` is *not* on the root surface: the root re-exports
30
30
  only the registry value, so a package implementing a new key type imports the type from
31
31
  `@owlmeans/basic-keys/plugins`.
32
- - `./utils` — `assertType`, `prepareKey`, `prepareData`, `toAddress`
32
+ - `./utils` — `keyUtils`: `assertType`, `prepareKey`, `prepareData`, `toAddress`
33
33
 
34
34
  ## Usage
35
35
 
@@ -68,9 +68,9 @@ Plain `Error` with a `basic.keys:<code>` message — this package does **not** u
68
68
  Not every failure comes back as one of these, so do not match on a code where the underlying
69
69
  library throws first:
70
70
 
71
- - Signing with a **verify-only** model does not raise `basic.keys:missing-pk`. `fromPubKey` stores
72
- `privateKey: ''`, which passes the null guard, and `@noble/curves` rejects it as
73
- `private key of length 32 expected, got 0`.
71
+ - Signing with a **verify-only** model does not raise `basic.keys:missing-pk`.
72
+ `keyHelper.fromPubKey` stores `privateKey: ''`, which passes the null guard, and `@noble/curves`
73
+ rejects it as `private key of length 32 expected, got 0`.
74
74
  - A string input that is neither a key type nor valid base64 fails in the decoder
75
75
  (`Found a character that cannot be part of a valid base64 string`), never with a `basic.keys:`
76
76
  code.
@@ -96,11 +96,11 @@ Callers that tolerate undecryptable values must catch — e.g. `@owlmeans/mongo`
96
96
 
97
97
  ## Signed credentials
98
98
 
99
- `packAuthCredentials` canonicalizes the unsigned credentials plus your extras, signs them, and
100
- returns `AuthCredentials` whose `credential` holds the signature (alone, or folded into the extras).
101
- `unpackAuthCredentials` reverses it and — given a verifier — reports `isValid`. Either side accepts
102
- a `KeyPairModel` or a bare sign/verify function, so a caller that holds only a remote signer works
103
- the same way.
99
+ `authCredentialsHelper.packAuthCredentials` canonicalizes the unsigned credentials plus your
100
+ extras, signs them, and returns `AuthCredentials` whose `credential` holds the signature (alone, or
101
+ folded into the extras). `authCredentialsHelper.unpackAuthCredentials` reverses it and — given a
102
+ verifier — reports `isValid`. Either side accepts a `KeyPairModel` or a bare sign/verify function,
103
+ so a caller that holds only a remote signer works the same way.
104
104
 
105
105
  ## Depends On
106
106
 
@@ -0,0 +1,10 @@
1
+ import type { AuthCredentials } from '@owlmeans/auth';
2
+ import type { KeyPairModel, PayloadSigner, PayloadVerifier, UnpackedAuthCredentials, UnsignedAuthCredentials } from '../types.js';
3
+ /** Signed authentication credentials: the signature packed into `credential` with any extras. */
4
+ export interface AuthCredentialsHelper {
5
+ /** Sign `auth` (plus canonicalized `extra`) and pack the signature into `credential`. */
6
+ packAuthCredentials: <T extends {} | undefined>(auth: UnsignedAuthCredentials, extra: T, signer: KeyPairModel | PayloadSigner) => Promise<AuthCredentials>;
7
+ /** Take packed credentials apart and, given a verifier, check the signature. */
8
+ unpackAuthCredentials: <T extends {} | undefined>(auth: AuthCredentials, verifier?: KeyPairModel | PayloadVerifier) => Promise<UnpackedAuthCredentials<T>>;
9
+ }
10
+ //# sourceMappingURL=types.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"types.d.ts","sourceRoot":"","sources":["../../src/auth/types.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,eAAe,EAAE,MAAM,gBAAgB,CAAA;AACrD,OAAO,KAAK,EAAE,YAAY,EAAE,aAAa,EAAE,eAAe,EAAE,uBAAuB,EAAE,uBAAuB,EAAE,MAAM,aAAa,CAAA;AAEjI,iGAAiG;AACjG,MAAM,WAAW,qBAAqB;IACpC,yFAAyF;IACzF,mBAAmB,EAAE,CAAC,CAAC,SAAS,EAAE,GAAG,SAAS,EAC5C,IAAI,EAAE,uBAAuB,EAC7B,KAAK,EAAE,CAAC,EACR,MAAM,EAAE,YAAY,GAAG,aAAa,KACjC,OAAO,CAAC,eAAe,CAAC,CAAA;IAC7B,gFAAgF;IAChF,qBAAqB,EAAE,CAAC,CAAC,SAAS,EAAE,GAAG,SAAS,EAC9C,IAAI,EAAE,eAAe,EACrB,QAAQ,CAAC,EAAE,YAAY,GAAG,eAAe,KACtC,OAAO,CAAC,uBAAuB,CAAC,CAAC,CAAC,CAAC,CAAA;CACzC"}
@@ -0,0 +1,2 @@
1
+ export {};
2
+ //# sourceMappingURL=types.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"types.js","sourceRoot":"","sources":["../../src/auth/types.ts"],"names":[],"mappings":""}
package/build/auth.d.ts CHANGED
@@ -1,5 +1,4 @@
1
- import type { AuthCredentials } from '@owlmeans/auth';
2
- import type { KeyPairModel, PayloadSigner, PayloadVerifier, UnpackedAuthCredentials, UnsignedAuthCredentials } from './types.js';
3
- export declare const packAuthCredentials: <T extends {} | undefined>(auth: UnsignedAuthCredentials, extra: T, signer: KeyPairModel | PayloadSigner) => Promise<AuthCredentials>;
4
- export declare const unpackAuthCredentials: <T extends {} | undefined>(auth: AuthCredentials, verifier?: KeyPairModel | PayloadVerifier) => Promise<UnpackedAuthCredentials<T>>;
1
+ import type { AuthCredentialsHelper } from './auth/types.js';
2
+ export declare const createAuthCredentialsHelper: () => AuthCredentialsHelper;
3
+ export declare const authCredentialsHelper: AuthCredentialsHelper;
5
4
  //# sourceMappingURL=auth.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"auth.d.ts","sourceRoot":"","sources":["../src/auth.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,eAAe,EAAE,MAAM,gBAAgB,CAAA;AACrD,OAAO,KAAK,EAAE,YAAY,EAAE,aAAa,EAAE,eAAe,EAAE,uBAAuB,EAAE,uBAAuB,EAAE,MAAM,YAAY,CAAA;AAGhI,eAAO,MAAM,mBAAmB,GAAU,CAAC,SAAS,EAAE,GAAG,SAAS,QAC1D,uBAAuB,SACtB,CAAC,UACA,YAAY,GAAG,aAAa,KACnC,OAAO,CAAC,eAAe,CAiBzB,CAAA;AAED,eAAO,MAAM,qBAAqB,GAAU,CAAC,SAAS,EAAE,GAAG,SAAS,QAC5D,eAAe,aACV,YAAY,GAAG,eAAe,KACxC,OAAO,CAAC,uBAAuB,CAAC,CAAC,CAAC,CA6BpC,CAAA"}
1
+ {"version":3,"file":"auth.d.ts","sourceRoot":"","sources":["../src/auth.ts"],"names":[],"mappings":"AAGA,OAAO,KAAK,EAAE,qBAAqB,EAAE,MAAM,iBAAiB,CAAA;AAE5D,eAAO,MAAM,2BAA2B,QAAO,qBA2D9C,CAAA;AAED,eAAO,MAAM,qBAAqB,uBAAgC,CAAA"}
package/build/auth.js CHANGED
@@ -1,45 +1,49 @@
1
1
  import canonicalize from 'canonicalize';
2
- export const packAuthCredentials = async (auth, extra, signer) => {
3
- const unsigned = {
4
- ...auth,
5
- ...(extra == null ? {} : { credential: canonicalize(extra) })
6
- };
7
- if (typeof signer !== 'function') {
8
- const _signer = signer;
9
- signer = async (payload) => await _signer.sign(payload);
10
- }
11
- const signature = await signer(unsigned);
12
- return {
13
- ...auth,
14
- credential: extra == null ? signature : canonicalize({ ...extra, signature }),
15
- };
16
- };
17
- export const unpackAuthCredentials = async (auth, verifier) => {
18
- let signature = '';
19
- let extras;
20
- try {
21
- extras = JSON.parse(auth.credential);
22
- if (typeof extras === 'object' && extras != null && "signature" in extras) {
23
- signature = extras.signature;
24
- delete extras.signature;
2
+ export const createAuthCredentialsHelper = () => {
3
+ const packAuthCredentials = async (auth, extra, signer) => {
4
+ const unsigned = {
5
+ ...auth,
6
+ ...(extra == null ? {} : { credential: canonicalize(extra) })
7
+ };
8
+ if (typeof signer !== 'function') {
9
+ const _signer = signer;
10
+ signer = async (payload) => await _signer.sign(payload);
25
11
  }
26
- }
27
- catch {
28
- signature = auth.credential;
29
- extras = undefined;
30
- }
31
- const unsigned = {
32
- ...auth,
33
- ...(extras == null ? {} : { credential: canonicalize(extras) })
12
+ const signature = await signer(unsigned);
13
+ return {
14
+ ...auth,
15
+ credential: extra == null ? signature : canonicalize({ ...extra, signature }),
16
+ };
34
17
  };
35
- let isValid = undefined;
36
- if (verifier != null) {
37
- if (typeof verifier !== 'function') {
38
- const _verifier = verifier;
39
- verifier = async (payload, signature) => await _verifier.verify(payload, signature);
18
+ const unpackAuthCredentials = async (auth, verifier) => {
19
+ let signature = '';
20
+ let extras;
21
+ try {
22
+ extras = JSON.parse(auth.credential);
23
+ if (typeof extras === 'object' && extras != null && "signature" in extras) {
24
+ signature = extras.signature;
25
+ delete extras.signature;
26
+ }
40
27
  }
41
- isValid = await verifier(unsigned, signature);
42
- }
43
- return { unsigned, signature, isValid, extras };
28
+ catch {
29
+ signature = auth.credential;
30
+ extras = undefined;
31
+ }
32
+ const unsigned = {
33
+ ...auth,
34
+ ...(extras == null ? {} : { credential: canonicalize(extras) })
35
+ };
36
+ let isValid = undefined;
37
+ if (verifier != null) {
38
+ if (typeof verifier !== 'function') {
39
+ const _verifier = verifier;
40
+ verifier = async (payload, signature) => await _verifier.verify(payload, signature);
41
+ }
42
+ isValid = await verifier(unsigned, signature);
43
+ }
44
+ return { unsigned, signature, isValid, extras };
45
+ };
46
+ return { packAuthCredentials, unpackAuthCredentials };
44
47
  };
48
+ export const authCredentialsHelper = createAuthCredentialsHelper();
45
49
  //# sourceMappingURL=auth.js.map
package/build/auth.js.map CHANGED
@@ -1 +1 @@
1
- {"version":3,"file":"auth.js","sourceRoot":"","sources":["../src/auth.ts"],"names":[],"mappings":"AAEA,OAAO,YAAY,MAAM,cAAc,CAAA;AAEvC,MAAM,CAAC,MAAM,mBAAmB,GAAG,KAAK,EACtC,IAA6B,EAC7B,KAAQ,EACR,MAAoC,EACV,EAAE;IAC5B,MAAM,QAAQ,GAA4B;QACxC,GAAG,IAAI;QACP,GAAG,CAAC,KAAK,IAAI,IAAI,CAAC,CAAC,CAAC,EAAE,CAAC,CAAC,CAAC,EAAE,UAAU,EAAE,YAAY,CAAC,KAAK,CAAW,EAAE,CAAC;KACxE,CAAA;IAED,IAAI,OAAO,MAAM,KAAK,UAAU,EAAE,CAAC;QACjC,MAAM,OAAO,GAAG,MAAM,CAAA;QACtB,MAAM,GAAG,KAAK,EAAC,OAAO,EAAC,EAAE,CAAC,MAAM,OAAO,CAAC,IAAI,CAAC,OAAO,CAAC,CAAA;IACvD,CAAC;IAED,MAAM,SAAS,GAAG,MAAM,MAAM,CAAC,QAAQ,CAAC,CAAA;IAExC,OAAO;QACL,GAAG,IAAI;QACP,UAAU,EAAE,KAAK,IAAI,IAAI,CAAC,CAAC,CAAC,SAAS,CAAC,CAAC,CAAC,YAAY,CAAC,EAAE,GAAG,KAAK,EAAE,SAAS,EAAE,CAAW;KACxF,CAAA;AACH,CAAC,CAAA;AAED,MAAM,CAAC,MAAM,qBAAqB,GAAG,KAAK,EACxC,IAAqB,EACrB,QAAyC,EACJ,EAAE;IACvC,IAAI,SAAS,GAAW,EAAE,CAAA;IAC1B,IAAI,MAAS,CAAA;IACb,IAAI,CAAC;QACH,MAAM,GAAG,IAAI,CAAC,KAAK,CAAC,IAAI,CAAC,UAAU,CAAC,CAAA;QACpC,IAAI,OAAO,MAAM,KAAK,QAAQ,IAAI,MAAM,IAAI,IAAI,IAAI,WAAW,IAAI,MAAM,EAAE,CAAC;YAC1E,SAA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,GAAwB,SAAS,CAAA;QAE5C,IAAI,QAAQ,IAAI,IAAI,EAAE,CAAC;YACrB,IAAI,OAAO,QAAQ,KAAK,UAAU,EAAE,CAAC;gBACnC,MAAM,SAAS,GAAG,QAAQ,CAAA;gBAC1B,QAAQ,GAAG,KAAK,EAAE,OAAO,EAAE,SAAS,EAAE,EAAE,CAAC,MAAM,SAAS,CAAC,MAAM,CAAC,OAAO,EAAE,SAAS,CAAC,CAAA;YACrF,CAAC;YAED,OAAO,GAAG,MAAM,QAAQ,CAAC,QAAQ,EAAE,SAAS,CAAC,CAAA;QAC/C,CAAC;QAED,OAAO,EAAE,QAAQ,EAAE,SAAS,EAAE,OAAO,EAAE,MAAM,EAAE,CAAA;IACjD,CAAC,CAAA;IAED,OAAO,EAAE,mBAAmB,EAAE,qBAAqB,EAAE,CAAA;AACvD,CAAC,CAAA;AAED,MAAM,CAAC,MAAM,qBAAqB,GAAG,2BAA2B,EAAE,CAAA"}
package/build/bin.js CHANGED
File without changes
@@ -0,0 +1,11 @@
1
+ import type { KeyPairModel } from '../types.js';
2
+ /** Key models built from what a peer publishes: its public key and its address. */
3
+ export interface KeyHelper {
4
+ /**
5
+ * A verify-only key model of a public key, `<type>:<key>` or a bare key (ed25519 by default).
6
+ */
7
+ fromPubKey: (pubKey: string, type?: string) => KeyPairModel;
8
+ /** Whether an address is the one the public key derives to. */
9
+ matchAddress: (address: string, pubKey: string) => boolean;
10
+ }
11
+ //# sourceMappingURL=types.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"types.d.ts","sourceRoot":"","sources":["../../src/helper/types.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,YAAY,EAAE,MAAM,aAAa,CAAA;AAE/C,mFAAmF;AACnF,MAAM,WAAW,SAAS;IACxB;;OAEG;IACH,UAAU,EAAE,CAAC,MAAM,EAAE,MAAM,EAAE,IAAI,CAAC,EAAE,MAAM,KAAK,YAAY,CAAA;IAC3D,+DAA+D;IAC/D,YAAY,EAAE,CAAC,OAAO,EAAE,MAAM,EAAE,MAAM,EAAE,MAAM,KAAK,OAAO,CAAA;CAC3D"}
@@ -0,0 +1,2 @@
1
+ export {};
2
+ //# sourceMappingURL=types.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"types.js","sourceRoot":"","sources":["../../src/helper/types.ts"],"names":[],"mappings":""}
package/build/helper.d.ts CHANGED
@@ -1,4 +1,7 @@
1
1
  import type { KeyPairModel } from './types.js';
2
+ import type { KeyHelper } from './helper/types.js';
3
+ export declare const createKeyHelper: () => KeyHelper;
4
+ export declare const keyHelper: KeyHelper;
5
+ /** @deprecated compat:factory-refactor — use `keyHelper.fromPubKey(…)` */
2
6
  export declare const fromPubKey: (pubKey: string, type?: string) => KeyPairModel;
3
- export declare const matchAddress: (address: string, pubKey: string) => boolean;
4
7
  //# sourceMappingURL=helper.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"helper.d.ts","sourceRoot":"","sources":["../src/helper.ts"],"names":[],"mappings":"AAGA,OAAO,KAAK,EAAW,YAAY,EAAE,MAAM,YAAY,CAAA;AAGvD,eAAO,MAAM,UAAU,WAAY,MAAM,SAAS,MAAM,KAAG,YAiB1D,CAAA;AAED,eAAO,MAAM,YAAY,YAAa,MAAM,UAAU,MAAM,KAAG,OACf,CAAA"}
1
+ {"version":3,"file":"helper.d.ts","sourceRoot":"","sources":["../src/helper.ts"],"names":[],"mappings":"AAGA,OAAO,KAAK,EAAW,YAAY,EAAE,MAAM,YAAY,CAAA;AAEvD,OAAO,KAAK,EAAE,SAAS,EAAE,MAAM,mBAAmB,CAAA;AAElD,eAAO,MAAM,eAAe,QAAO,SAwBlC,CAAA;AAED,eAAO,MAAM,SAAS,WAAoB,CAAA;AAE1C,0EAA0E;AAC1E,eAAO,MAAM,UAAU,WAAY,MAAM,SAAS,MAAM,KAAG,YAAkD,CAAA"}
package/build/helper.js CHANGED
@@ -1,22 +1,28 @@
1
1
  import { KeyType } from './consts.js';
2
2
  import { makeKeyPairModel } from './model.js';
3
3
  import { plugins } from './plugins/index.js';
4
- import { prepareKey } from './utils.js';
5
- export const fromPubKey = (pubKey, type) => {
6
- if (type == null) {
7
- [type, pubKey] = pubKey.includes(':') ? pubKey.split(':', 2) : [KeyType.ED25519, pubKey];
8
- if (pubKey == null) {
9
- pubKey = type;
10
- type = KeyType.ED25519;
4
+ import { keyUtils } from './utils.js';
5
+ export const createKeyHelper = () => {
6
+ const fromPubKey = (pubKey, type) => {
7
+ if (type == null) {
8
+ [type, pubKey] = pubKey.includes(':') ? pubKey.split(':', 2) : [KeyType.ED25519, pubKey];
9
+ if (pubKey == null) {
10
+ pubKey = type;
11
+ type = KeyType.ED25519;
12
+ }
11
13
  }
12
- }
13
- const keyPair = {
14
- privateKey: '',
15
- publicKey: pubKey,
16
- type,
17
- address: plugins[type].toAdress(prepareKey(pubKey))
14
+ const keyPair = {
15
+ privateKey: '',
16
+ publicKey: pubKey,
17
+ type,
18
+ address: plugins[type].toAdress(keyUtils.prepareKey(pubKey))
19
+ };
20
+ return makeKeyPairModel(keyPair);
18
21
  };
19
- return makeKeyPairModel(keyPair);
22
+ const matchAddress = (address, pubKey) => address === fromPubKey(pubKey).exportAddress();
23
+ return { fromPubKey, matchAddress };
20
24
  };
21
- export const matchAddress = (address, pubKey) => address === fromPubKey(pubKey).exportAddress();
25
+ export const keyHelper = createKeyHelper();
26
+ /** @deprecated compat:factory-refactor — use `keyHelper.fromPubKey(…)` */
27
+ export const fromPubKey = (pubKey, type) => keyHelper.fromPubKey(pubKey, type);
22
28
  //# sourceMappingURL=helper.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"helper.js","sourceRoot":"","sources":["../src/helper.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,OAAO,EAAE,MAAM,aAAa,CAAA;AACrC,OAAO,EAAE,gBAAgB,EAAE,MAAM,YAAY,CAAA;AAC7C,OAAO,EAAE,OAAO,EAAE,MAAM,oBAAoB,CAAA;AAE5C,OAAO,EAAE,UAAU,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IAAa,EAAgB,EAAE;QACjE,IAAI,IAAI,IAAI,IAAI,EAAE,CAAC;YACjB,CAAC,IAAI,EAAE,MAAM,CAAC,GAAG,MAAM,CAAC,QAAQ,CAAC,GAAG,CAAC,CAAC,CAAC,CAAC,MAAM,CAAC,KAAK,CAAC,GAAG,EAAE,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,OAAO,CAAC,OAAO,EAAE,MAAM,CAAC,CAAA;YACxF,IAAI,MAAM,IAAI,IAAI,EAAE,CAAC;gBACnB,MAAM,GAAG,IAAI,CAAA;gBACb,IAAI,GAAG,OAAO,CAAC,OAAO,CAAA;YACxB,CAAC;QACH,CAAC;QAED,MAAM,OAAO,GAAY;YACvB,UAAU,EAAE,EAAE;YACd,SAAS,EAAE,MAAM;YACjB,IAAI;YACJ,OAAO,EAAE,OAAO,CAAC,IAAI,CAAC,CAAC,QAAQ,CAAC,QAAQ,CAAC,UAAU,CAAC,MAAM,CAAC,CAAC;SAC7D,CAAA;QAED,OAAO,gBAAgB,CAAC,OAAO,CAAC,CAAA;IAClC,CAAC,CAAA;IAED,MAAM,YAAY,GAAG,CAAC,OAAe,EAAE,MAAc,EAAW,EAAE,CAChE,OAAO,KAAK,UAAU,CAAC,MAAM,CAAC,CAAC,aAAa,EAAE,CAAA;IAEhD,OAAO,EAAE,UAAU,EAAE,YAAY,EAAE,CAAA;AACrC,CAAC,CAAA;AAED,MAAM,CAAC,MAAM,SAAS,GAAG,eAAe,EAAE,CAAA;AAE1C,0EAA0E;AAC1E,MAAM,CAAC,MAAM,UAAU,GAAG,CAAC,MAAc,EAAE,IAAa,EAAgB,EAAE,CAAC,SAAS,CAAC,UAAU,CAAC,MAAM,EAAE,IAAI,CAAC,CAAA"}
package/build/index.d.ts CHANGED
@@ -1,4 +1,6 @@
1
1
  export type * from './types.js';
2
+ export type * from './helper/types.js';
3
+ export type * from './auth/types.js';
2
4
  export * from './consts.js';
3
5
  export * from './helper.js';
4
6
  export * from './keypair.js';
@@ -1 +1 @@
1
- {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,mBAAmB,YAAY,CAAA;AAE/B,cAAc,aAAa,CAAA;AAC3B,cAAc,aAAa,CAAA;AAC3B,cAAc,cAAc,CAAA;AAC5B,cAAc,YAAY,CAAA;AAC1B,cAAc,WAAW,CAAA;AACzB,cAAc,oBAAoB,CAAA"}
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,mBAAmB,YAAY,CAAA;AAC/B,mBAAmB,mBAAmB,CAAA;AACtC,mBAAmB,iBAAiB,CAAA;AAEpC,cAAc,aAAa,CAAA;AAC3B,cAAc,aAAa,CAAA;AAC3B,cAAc,cAAc,CAAA;AAC5B,cAAc,YAAY,CAAA;AAC1B,cAAc,WAAW,CAAA;AACzB,cAAc,oBAAoB,CAAA"}
@@ -1 +1 @@
1
- {"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAEA,cAAc,aAAa,CAAA;AAC3B,cAAc,aAAa,CAAA;AAC3B,cAAc,cAAc,CAAA;AAC5B,cAAc,YAAY,CAAA;AAC1B,cAAc,WAAW,CAAA;AACzB,cAAc,oBAAoB,CAAA"}
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAIA,cAAc,aAAa,CAAA;AAC3B,cAAc,aAAa,CAAA;AAC3B,cAAc,cAAc,CAAA;AAC5B,cAAc,YAAY,CAAA;AAC1B,cAAc,WAAW,CAAA;AACzB,cAAc,oBAAoB,CAAA"}
package/build/keypair.js CHANGED
@@ -1,7 +1,7 @@
1
1
  import { base64 } from '@scure/base';
2
2
  import { KeyType } from './consts.js';
3
3
  import { plugins } from './plugins/index.js';
4
- import { assertType } from './utils.js';
4
+ import { keyUtils } from './utils.js';
5
5
  export const inputToKeyPair = (input) => {
6
6
  let type = KeyType.ED25519;
7
7
  let keyPair = null;
@@ -29,7 +29,7 @@ export const inputToKeyPair = (input) => {
29
29
  }
30
30
  }
31
31
  if (keyPair == null) {
32
- assertType(type);
32
+ keyUtils.assertType(type);
33
33
  if (primaryKey == null) {
34
34
  primaryKey = plugins[type].random();
35
35
  }
@@ -1 +1 @@
1
- {"version":3,"file":"keypair.js","sourceRoot":"","sources":["../src/keypair.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,MAAM,EAAE,MAAM,aAAa,CAAA;AACpC,OAAO,EAAE,OAAO,EAAE,MAAM,aAAa,CAAA;AACrC,OAAO,EAAE,OAAO,EAAE,MAAM,oBAAoB,CAAA;AAC5C,OAAO,EAAE,UAAU,EAAE,MAAM,YAAY,CAAA;AAEvC,MAAM,CAAC,MAAM,cAAc,GAAG,CAAC,KAAwB,EAAW,EAAE;IAClE,IAAI,IAAI,GAAW,OAAO,CAAC,OAAO,CAAA;IAClC,IAAI,OAAO,GAAmB,IAAI,CAAA;IAClC,IAAI,UAAU,GAAsB,IAAI,CAAA;IACxC,IAAI,OAAO,KAAK,KAAK,QAAQ,EAAE,CAAC;QAC9B,OAAO,GAAG,KAAK,CAAA;IACjB,CAAC;SAAM,IAAI,OAAO,KAAK,KAAK,QAAQ,EAAE,CAAC;QACrC,IAAI,KAAK,IAAI,OAAO,EAAE,CAAC,CAAC,wCAAwC;YAC9D,IAAI,GAAG,KAAK,CAAA;QACd,CAAC;aAAM,CAAC;YACN,MAAM,CAAC,OAAO,EAAE,GAAG,CAAC,GAAG,KAAK,CAAC,KAAK,CAAC,GAAG,EAAE,CAAC,CAAC,CAAA;YAC1C,IAAI,GAAG,IAAI,IAAI,IAAI,OAAO,IAAI,IAAI,EAAE,CAAC;gBACnC,IAAI,GAAG,OAAO,CAAC,OAAO,CAAA;gBACtB,UAAU,GAAG,MAAM,CAAC,MAAM,CAAC,OAAO,CAAC,CAAA;YACrC,CAAC;iBAAM,IAAI,GAAG,IAAI,IAAI,EAAE,CAAC;gBACvB,IAAI,GAAG,OAAO,CAAA;gBACd,U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A;IACH,CAAC;IAED,OAAO,OAAO,CAAA;AAChB,CAAC,CAAA"}
package/build/model.d.ts CHANGED
@@ -1,3 +1,3 @@
1
- import type { KeyPairModelMaker } from './types.js';
2
- export declare const makeKeyPairModel: KeyPairModelMaker;
1
+ import type { KeyPair, KeyPairModel } from './types.js';
2
+ export declare const makeKeyPairModel: (input?: KeyPair | string) => KeyPairModel;
3
3
  //# sourceMappingURL=model.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"model.d.ts","sourceRoot":"","sources":["../src/model.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAgB,iBAAiB,EAAE,MAAM,YAAY,CAAA;AAMjE,eAAO,MAAM,gBAAgB,EAAE,iBAmH9B,CAAA"}
1
+ {"version":3,"file":"model.d.ts","sourceRoot":"","sources":["../src/model.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,OAAO,EAAE,YAAY,EAAE,MAAM,YAAY,CAAA;AAMvD,eAAO,MAAM,gBAAgB,WAAY,OAAO,GAAG,MAAM,KAAG,YAmH3D,CAAA"}
package/build/model.js CHANGED
@@ -1,59 +1,59 @@
1
1
  import { plugins } from './plugins/index.js';
2
2
  import { base64urlnopad, utf8 } from '@scure/base';
3
- import { assertType, prepareData, prepareKey } from './utils.js';
3
+ import { keyUtils } from './utils.js';
4
4
  import { inputToKeyPair } from './keypair.js';
5
- export const makeKeyPairModel = input => {
5
+ export const makeKeyPairModel = (input) => {
6
6
  const keyPair = inputToKeyPair(input);
7
7
  const _model = {
8
8
  keyPair,
9
9
  sign: async (data) => {
10
- data = prepareData(data);
11
- assertType(_model.keyPair?.type);
10
+ data = keyUtils.prepareData(data);
11
+ keyUtils.assertType(_model.keyPair?.type);
12
12
  if (_model.keyPair == null) {
13
13
  throw new Error('basic.keys:missing-keypair');
14
14
  }
15
15
  if (_model.keyPair.privateKey == null) {
16
16
  throw new Error('basic.keys:missing-pk');
17
17
  }
18
- return base64urlnopad.encode(plugins[_model.keyPair.type].sign(data, prepareKey(_model.keyPair.privateKey)));
18
+ return base64urlnopad.encode(plugins[_model.keyPair.type].sign(data, keyUtils.prepareKey(_model.keyPair.privateKey)));
19
19
  },
20
20
  verify: async (data, signature) => {
21
- data = prepareData(data);
22
- assertType(_model.keyPair?.type);
21
+ data = keyUtils.prepareData(data);
22
+ keyUtils.assertType(_model.keyPair?.type);
23
23
  const sig = base64urlnopad.decode(signature);
24
24
  if (_model.keyPair == null) {
25
25
  throw new Error('basic.keys:missing-keypair');
26
26
  }
27
- return plugins[_model.keyPair.type].verify(data, sig, prepareKey(_model.keyPair.publicKey));
27
+ return plugins[_model.keyPair.type].verify(data, sig, keyUtils.prepareKey(_model.keyPair.publicKey));
28
28
  },
29
29
  export: () => {
30
- assertType(_model.keyPair?.type);
30
+ keyUtils.assertType(_model.keyPair?.type);
31
31
  if (_model.keyPair == null) {
32
32
  throw new Error('basic.keys:missing-keypair');
33
33
  }
34
34
  return `${_model.keyPair.type}:${_model.keyPair.privateKey}`;
35
35
  },
36
36
  exportPublic: () => {
37
- assertType(_model.keyPair?.type);
37
+ keyUtils.assertType(_model.keyPair?.type);
38
38
  if (_model.keyPair == null) {
39
39
  throw new Error('basic.keys:missing-keypair');
40
40
  }
41
41
  return `${_model.keyPair.type}:${_model.keyPair.publicKey}`;
42
42
  },
43
43
  exportAddress: () => {
44
- assertType(_model.keyPair?.type);
44
+ keyUtils.assertType(_model.keyPair?.type);
45
45
  if (_model.keyPair == null) {
46
46
  throw new Error('basic.keys:missing-keypair');
47
47
  }
48
48
  return `${_model.keyPair.type}:${_model.keyPair.address}`;
49
49
  },
50
50
  encrypt: async (data) => {
51
- data = prepareData(data);
52
- assertType(_model.keyPair?.type);
51
+ data = keyUtils.prepareData(data);
52
+ keyUtils.assertType(_model.keyPair?.type);
53
53
  if (_model.keyPair == null) {
54
54
  throw new Error('basic.keys:missing-keypair');
55
55
  }
56
- return base64urlnopad.encode(plugins[_model.keyPair.type].encrypt(data, prepareKey(_model.keyPair.publicKey)));
56
+ return base64urlnopad.encode(plugins[_model.keyPair.type].encrypt(data, keyUtils.prepareKey(_model.keyPair.publicKey)));
57
57
  },
58
58
  decrypt: async (data) => {
59
59
  const plain = await _model.dcrpt(data);
@@ -68,11 +68,11 @@ export const makeKeyPairModel = input => {
68
68
  },
69
69
  dcrpt: async (data) => {
70
70
  data = data instanceof Uint8Array ? data : base64urlnopad.decode(data);
71
- assertType(_model.keyPair?.type);
71
+ keyUtils.assertType(_model.keyPair?.type);
72
72
  if (_model.keyPair == null) {
73
73
  throw new Error('basic.keys:missing-keypair');
74
74
  }
75
- return plugins[_model.keyPair.type].decrypt(data, prepareKey(_model.keyPair.privateKey));
75
+ return plugins[_model.keyPair.type].decrypt(data, keyUtils.prepareKey(_model.keyPair.privateKey));
76
76
  }
77
77
  };
78
78
  return _model;
@@ -1 +1 @@
1
- {"version":3,"file":"model.js","sourceRoot":"","sources":["../src/model.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,OAAO,EAAE,MAAM,oBAAoB,CAAA;AAC5C,OAAO,EAAE,cAAc,EAAE,IAAI,EAAE,MAAM,aAAa,CAAA;AAClD,OAAO,EAAE,UAAU,EAAE,WAAW,EAAE,UAAU,EAAE,MAAM,YAAY,CAAA;AAChE,OAAO,EAAE,cAAc,EAAE,MAAM,cAAc,CAAA;AAE7C,MAAM,CAAC,MAAM,gBAAgB,GAAsB,KAAK,CAAC,EAAE;IACzD,MAAM,OAAO,GAAG,cAAc,CAAC,KAAK,CAAC,CAAA;IAErC,MAAM,MAAM,GAAiB;QAC3B,OAAO;QAEP,IAAI,EAAE,KAAK,EAAE,IAAI,EAAE,EAAE;YACnB,IAAI,GAAG,WAAW,CAAC,IAAI,CAAC,CAAA;YACxB,UAAU,CAAC,MAAM,CAAC,OAAO,EAAE,IAAI,CAAC,CAAA;YAEhC,IAAI,MAAM,CAAC,OAAO,IAAI,IAAI,EAAE,CAAC;gBAC3B,MAAM,IAAI,KAAK,CAAC,4BAA4B,CAAC,CAAA;YAC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M,CAAC,OAAO,CAAC,IAAI,CAAC,CAAC,OAAO,CAClC,IAAkB,EAClB,QAAQ,CAAC,UAAU,CAAC,MAAM,CAAC,OAAO,CAAC,SAAS,CAAC,CAC9C,CACF,CAAA;QACH,CAAC;QAED,OAAO,EAAE,KAAK,EAAC,IAAI,EAAC,EAAE;YACpB,MAAM,KAAK,GAAG,MAAM,MAAM,CAAC,KAAK,CAAC,IAAI,CAAC,CAAA;YACtC,IAAI,CAAC;gBACH,sEAAsE;gBACtE,yEAAyE;gBACzE,OAAO,IAAI,CAAC,MAAM,CAAC,KAAK,CAAC,CAAA;YAC3B,CAAC;YAAC,MAAM,CAAC;gBACP,MAAM,IAAI,KAAK,CAAC,6BAA6B,CAAC,CAAA;YAChD,CAAC;QACH,CAAC;QAED,KAAK,EAAE,KAAK,EAAC,IAAI,EAAC,EAAE;YAClB,IAAI,GAAG,IAAI,YAAY,UAAU,CAAC,CAAC,CAAC,IAAI,CAAC,CAAC,CAAC,cAAc,CAAC,MAAM,CAAC,IAAc,CAAC,CAAA;YAChF,QAAQ,CAAC,UAAU,CAAC,MAAM,CAAC,OAAO,EAAE,IAAI,CAAC,CAAA;YAEzC,IAAI,MAAM,CAAC,OAAO,IAAI,IAAI,EAAE,CAAC;gBAC3B,MAAM,IAAI,KAAK,CAAC,4BAA4B,CAAC,CAAA;YAC/C,CAAC;YAED,OAAO,OAAO,CAAC,MAAM,CAAC,OAAO,CAAC,IAAI,CAAC,CAAC,OAAO,CACzC,IAAkB,EAClB,QAAQ,CAAC,UAAU,CAAC,MAAM,CAAC,OAAO,CAAC,UAAU,CAAC,CAC/C,CAAA;QACH,CAAC;KACF,CAAA;IAED,OAAO,MAAM,CAAA;AACf,CAAC,CAAA"}
@@ -0,0 +1,16 @@
1
+ /** The low-level key and payload primitives the key models and plugins share. */
2
+ export interface KeyUtils {
3
+ /** The 20-byte address of a public key: the tail of its keccak-256 digest. */
4
+ toAddress: (publicKey: Uint8Array) => Uint8Array;
5
+ /** A base64-encoded key as bytes. */
6
+ prepareKey: (key: string) => Uint8Array;
7
+ /**
8
+ * Bytes to sign, verify or encrypt: an object is canonicalized, a string UTF-8 encoded.
9
+ *
10
+ * @throws {Error} `basic.keys:sign-data-type` for anything else
11
+ */
12
+ prepareData: (data: unknown) => Uint8Array;
13
+ /** @throws {Error} `basic.keys:unknown-type` for a key type no plugin is registered for */
14
+ assertType: (type?: string) => void;
15
+ }
16
+ //# sourceMappingURL=types.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"types.d.ts","sourceRoot":"","sources":["../../src/utils/types.ts"],"names":[],"mappings":"AAAA,iFAAiF;AACjF,MAAM,WAAW,QAAQ;IACvB,8EAA8E;IAC9E,SAAS,EAAE,CAAC,SAAS,EAAE,UAAU,KAAK,UAAU,CAAA;IAChD,qCAAqC;IACrC,UAAU,EAAE,CAAC,GAAG,EAAE,MAAM,KAAK,UAAU,CAAA;IACvC;;;;OAIG;IACH,WAAW,EAAE,CAAC,IAAI,EAAE,OAAO,KAAK,UAAU,CAAA;IAC1C,2FAA2F;IAC3F,UAAU,EAAE,CAAC,IAAI,CAAC,EAAE,MAAM,KAAK,IAAI,CAAA;CACpC"}
@@ -0,0 +1,2 @@
1
+ export {};
2
+ //# sourceMappingURL=types.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"types.js","sourceRoot":"","sources":["../../src/utils/types.ts"],"names":[],"mappings":""}
package/build/utils.d.ts CHANGED
@@ -1,5 +1,4 @@
1
- export declare const toAddress: (publicKey: Uint8Array) => Uint8Array;
2
- export declare const prepareKey: (key: string) => Uint8Array;
3
- export declare const prepareData: (data: unknown) => Uint8Array;
4
- export declare const assertType: (type?: string) => void;
1
+ import type { KeyUtils } from './utils/types.js';
2
+ export declare const createKeyUtils: () => KeyUtils;
3
+ export declare const keyUtils: KeyUtils;
5
4
  //# sourceMappingURL=utils.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"utils.d.ts","sourceRoot":"","sources":["../src/utils.ts"],"names":[],"mappings":"AAKA,eAAO,MAAM,SAAS,cAAe,UAAU,KAAG,UACP,CAAA;AAE3C,eAAO,MAAM,UAAU,QAAS,MAAM,KAAG,UACrB,CAAA;AAEpB,eAAO,MAAM,WAAW,SAAU,OAAO,KAAG,UAiB3C,CAAA;AAED,eAAO,MAAM,UAAU,UAAW,MAAM,KAAG,IAI1C,CAAA"}
1
+ {"version":3,"file":"utils.d.ts","sourceRoot":"","sources":["../src/utils.ts"],"names":[],"mappings":"AAIA,OAAO,KAAK,EAAE,QAAQ,EAAE,MAAM,kBAAkB,CAAA;AAEhD,eAAO,MAAM,cAAc,QAAO,QAiCjC,CAAA;AAED,eAAO,MAAM,QAAQ,UAAmB,CAAA"}
package/build/utils.js CHANGED
@@ -2,28 +2,32 @@ import { keccak_256 } from '@noble/hashes/sha3.js';
2
2
  import { base64, utf8 } from '@scure/base';
3
3
  import { plugins } from './plugins/index.js';
4
4
  import canonicalize from 'canonicalize';
5
- export const toAddress = (publicKey) => keccak_256(publicKey.slice(4)).slice(-20);
6
- export const prepareKey = (key) => base64.decode(key);
7
- export const prepareData = (data) => {
8
- if (typeof data === 'object') {
5
+ export const createKeyUtils = () => {
6
+ const toAddress = (publicKey) => keccak_256(publicKey.slice(4)).slice(-20);
7
+ const prepareKey = (key) => base64.decode(key);
8
+ const prepareData = (data) => {
9
+ if (typeof data === 'object') {
10
+ if (!(data instanceof Uint8Array)) {
11
+ data = canonicalize(data);
12
+ }
13
+ }
14
+ if (typeof data === 'string') {
15
+ data = utf8.decode(data);
16
+ }
17
+ else if (!(data instanceof Uint8Array)) {
18
+ throw new Error('basic.keys:sign-data-type');
19
+ }
9
20
  if (!(data instanceof Uint8Array)) {
10
- data = canonicalize(data);
21
+ throw new Error('basic.keys:sign-data-type');
11
22
  }
12
- }
13
- if (typeof data === 'string') {
14
- data = utf8.decode(data);
15
- }
16
- else if (!(data instanceof Uint8Array)) {
17
- throw new Error('basic.keys:sign-data-type');
18
- }
19
- if (!(data instanceof Uint8Array)) {
20
- throw new Error('basic.keys:sign-data-type');
21
- }
22
- return data;
23
- };
24
- export const assertType = (type) => {
25
- if (type == null || !(type in plugins)) {
26
- throw new Error('basic.keys:unknown-type');
27
- }
23
+ return data;
24
+ };
25
+ const assertType = (type) => {
26
+ if (type == null || !(type in plugins)) {
27
+ throw new Error('basic.keys:unknown-type');
28
+ }
29
+ };
30
+ return { toAddress, prepareKey, prepareData, assertType };
28
31
  };
32
+ export const keyUtils = createKeyUtils();
29
33
  //# sourceMappingURL=utils.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"utils.js","sourceRoot":"","sources":["../src/utils.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,UAAU,EAAE,MAAM,uBAAuB,CAAA;AAClD,OAAO,EAAE,MAAM,EAAE,IAAI,EAAE,MAAM,aAAa,CAAA;AAC1C,OAAO,EAAE,OAAO,EAAE,MAAM,oBAAoB,CAAA;AAC5C,OAAO,YAAY,MAAM,cAAc,CAAA;AAEvC,MAAM,CAAC,MAAM,SAAS,GAAG,CAAC,SAAqB,EAAc,EAAE,CAC7D,UAAU,CAAC,SAAS,CAAC,KAAK,CAAC,CAAC,CAAC,CAAC,CAAC,KAAK,CAAC,CAAC,EAAE,CAAC,CAAA;AAE3C,MAAM,CAAC,MAAM,UAAU,GAAG,CAAC,GAAW,EAAc,EAAE,CACpD,MAAM,CAAC,MAAM,CAAC,GAAG,CAAC,CAAA;AAEpB,MAAM,CAAC,MAAM,WAAW,GAAG,CAAC,IAAa,EAAc,EAAE;IACvD,IAAI,OAAO,IAAI,KAAK,QAAQ,EAAE,CAAC;QAC7B,IAAI,CAAC,CAAC,IAAI,YAAY,UAAU,CAAC,EAAE,CAAC;YAClC,IAAI,GAAG,YAAY,CAAC,IAAI,CAAC,CAAA;QAC3B,CAAC;IACH,CAAC;IACD,IAAI,OAAO,IAAI,KAAK,QAAQ,EAAE,CAAC;QAC7B,IAAI,GAAG,IAAI,CAAC,MAAM,CAAC,IAAI,CAAC,CAA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AU,CAAC,EAAE,CAAC;gBAClC,IAAI,GAAG,YAAY,CAAC,IAAI,CAAC,CAAA;YAC3B,CAAC;QACH,CAAC;QACD,IAAI,OAAO,IAAI,KAAK,QAAQ,EAAE,CAAC;YAC7B,IAAI,GAAG,IAAI,CAAC,MAAM,CAAC,IAAI,CAAC,CAAA;QAC1B,CAAC;aAAM,IAAI,CAAC,CAAC,IAAI,YAAY,UAAU,CAAC,EAAE,CAAC;YACzC,MAAM,IAAI,KAAK,CAAC,2BAA2B,CAAC,CAAA;QAC9C,CAAC;QAED,IAAI,CAAC,CAAC,IAAI,YAAY,UAAU,CAAC,EAAE,CAAC;YAClC,MAAM,IAAI,KAAK,CAAC,2BAA2B,CAAC,CAAA;QAC9C,CAAC;QAED,OAAO,IAAI,CAAA;IACb,CAAC,CAAA;IAED,MAAM,UAAU,GAAG,CAAC,IAAa,EAAQ,EAAE;QACzC,IAAI,IAAI,IAAI,IAAI,IAAI,CAAC,CAAC,IAAI,IAAI,OAAO,CAAC,EAAE,CAAC;YACvC,MAAM,IAAI,KAAK,CAAC,yBAAyB,CAAC,CAAA;QAC5C,CAAC;IACH,CAAC,CAAA;IAED,OAAO,EAAE,SAAS,EAAE,UAAU,EAAE,WAAW,EAAE,UAAU,EAAE,CAAA;AAC3D,CAAC,CAAA;AAED,MAAM,CAAC,MAAM,QAAQ,GAAG,cAAc,EAAE,CAAA"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@owlmeans/basic-keys",
3
- "version": "0.1.18-rc.47",
3
+ "version": "0.1.18-rc.48",
4
4
  "license": "MIT",
5
5
  "type": "module",
6
6
  "scripts": {
@@ -40,7 +40,7 @@
40
40
  },
41
41
  "devDependencies": {
42
42
  "@owlmeans/dep-config": "workspace:*",
43
- "@owlmeans/test-auth": "^0.1.18-rc.45",
43
+ "@owlmeans/test-auth": "^0.1.18-rc.46",
44
44
  "@types/bun": "^1.4.2",
45
45
  "nodemon": "^3.1.14",
46
46
  "typescript": "^7.0.2"
@@ -49,7 +49,7 @@
49
49
  "@noble/ciphers": "^2.4.0",
50
50
  "@noble/curves": "^2.4.0",
51
51
  "@noble/hashes": "^2.4.0",
52
- "@owlmeans/auth": "^0.1.18-rc.39",
52
+ "@owlmeans/auth": "^0.1.18-rc.40",
53
53
  "@scure/base": "^2.4.0",
54
54
  "canonicalize": "^5.1.0"
55
55
  },
@@ -0,0 +1,17 @@
1
+ import type { AuthCredentials } from '@owlmeans/auth'
2
+ import type { KeyPairModel, PayloadSigner, PayloadVerifier, UnpackedAuthCredentials, UnsignedAuthCredentials } from '../types.js'
3
+
4
+ /** Signed authentication credentials: the signature packed into `credential` with any extras. */
5
+ export interface AuthCredentialsHelper {
6
+ /** Sign `auth` (plus canonicalized `extra`) and pack the signature into `credential`. */
7
+ packAuthCredentials: <T extends {} | undefined>(
8
+ auth: UnsignedAuthCredentials,
9
+ extra: T,
10
+ signer: KeyPairModel | PayloadSigner
11
+ ) => Promise<AuthCredentials>
12
+ /** Take packed credentials apart and, given a verifier, check the signature. */
13
+ unpackAuthCredentials: <T extends {} | undefined>(
14
+ auth: AuthCredentials,
15
+ verifier?: KeyPairModel | PayloadVerifier
16
+ ) => Promise<UnpackedAuthCredentials<T>>
17
+ }
package/src/auth.ts CHANGED
@@ -1,60 +1,67 @@
1
1
  import type { AuthCredentials } from '@owlmeans/auth'
2
2
  import type { KeyPairModel, PayloadSigner, PayloadVerifier, UnpackedAuthCredentials, UnsignedAuthCredentials } from './types.js'
3
3
  import canonicalize from 'canonicalize'
4
+ import type { AuthCredentialsHelper } from './auth/types.js'
4
5
 
5
- export const packAuthCredentials = async <T extends {} | undefined>(
6
- auth: UnsignedAuthCredentials,
7
- extra: T,
8
- signer: KeyPairModel | PayloadSigner
9
- ): Promise<AuthCredentials> => {
10
- const unsigned: UnsignedAuthCredentials = {
11
- ...auth,
12
- ...(extra == null ? {} : { credential: canonicalize(extra) as string })
13
- }
6
+ export const createAuthCredentialsHelper = (): AuthCredentialsHelper => {
7
+ const packAuthCredentials = async <T extends {} | undefined>(
8
+ auth: UnsignedAuthCredentials,
9
+ extra: T,
10
+ signer: KeyPairModel | PayloadSigner
11
+ ): Promise<AuthCredentials> => {
12
+ const unsigned: UnsignedAuthCredentials = {
13
+ ...auth,
14
+ ...(extra == null ? {} : { credential: canonicalize(extra) as string })
15
+ }
14
16
 
15
- if (typeof signer !== 'function') {
16
- const _signer = signer
17
- signer = async payload => await _signer.sign(payload)
18
- }
17
+ if (typeof signer !== 'function') {
18
+ const _signer = signer
19
+ signer = async payload => await _signer.sign(payload)
20
+ }
19
21
 
20
- const signature = await signer(unsigned)
22
+ const signature = await signer(unsigned)
21
23
 
22
- return {
23
- ...auth,
24
- credential: extra == null ? signature : canonicalize({ ...extra, signature }) as string,
24
+ return {
25
+ ...auth,
26
+ credential: extra == null ? signature : canonicalize({ ...extra, signature }) as string,
27
+ }
25
28
  }
26
- }
27
29
 
28
- export const unpackAuthCredentials = async <T extends {} | undefined>(
29
- auth: AuthCredentials,
30
- verifier?: KeyPairModel | PayloadVerifier
31
- ): Promise<UnpackedAuthCredentials<T>> => {
32
- let signature: string = ''
33
- let extras: T
34
- try {
35
- extras = JSON.parse(auth.credential)
36
- if (typeof extras === 'object' && extras != null && "signature" in extras) {
37
- signature = extras.signature as string
38
- delete extras.signature
30
+ const unpackAuthCredentials = async <T extends {} | undefined>(
31
+ auth: AuthCredentials,
32
+ verifier?: KeyPairModel | PayloadVerifier
33
+ ): Promise<UnpackedAuthCredentials<T>> => {
34
+ let signature: string = ''
35
+ let extras: T
36
+ try {
37
+ extras = JSON.parse(auth.credential)
38
+ if (typeof extras === 'object' && extras != null && "signature" in extras) {
39
+ signature = extras.signature as string
40
+ delete extras.signature
41
+ }
42
+ } catch {
43
+ signature = auth.credential
44
+ extras = undefined as T
39
45
  }
40
- } catch {
41
- signature = auth.credential
42
- extras = undefined as T
43
- }
44
- const unsigned: UnsignedAuthCredentials = {
45
- ...auth,
46
- ...(extras == null ? {} : { credential: canonicalize(extras) as string })
47
- }
48
- let isValid: boolean | undefined = undefined
46
+ const unsigned: UnsignedAuthCredentials = {
47
+ ...auth,
48
+ ...(extras == null ? {} : { credential: canonicalize(extras) as string })
49
+ }
50
+ let isValid: boolean | undefined = undefined
49
51
 
50
- if (verifier != null) {
51
- if (typeof verifier !== 'function') {
52
- const _verifier = verifier
53
- verifier = async (payload, signature) => await _verifier.verify(payload, signature)
52
+ if (verifier != null) {
53
+ if (typeof verifier !== 'function') {
54
+ const _verifier = verifier
55
+ verifier = async (payload, signature) => await _verifier.verify(payload, signature)
56
+ }
57
+
58
+ isValid = await verifier(unsigned, signature)
54
59
  }
55
60
 
56
- isValid = await verifier(unsigned, signature)
61
+ return { unsigned, signature, isValid, extras }
57
62
  }
58
63
 
59
- return { unsigned, signature, isValid, extras }
64
+ return { packAuthCredentials, unpackAuthCredentials }
60
65
  }
66
+
67
+ export const authCredentialsHelper = createAuthCredentialsHelper()
@@ -0,0 +1,11 @@
1
+ import type { KeyPairModel } from '../types.js'
2
+
3
+ /** Key models built from what a peer publishes: its public key and its address. */
4
+ export interface KeyHelper {
5
+ /**
6
+ * A verify-only key model of a public key, `<type>:<key>` or a bare key (ed25519 by default).
7
+ */
8
+ fromPubKey: (pubKey: string, type?: string) => KeyPairModel
9
+ /** Whether an address is the one the public key derives to. */
10
+ matchAddress: (address: string, pubKey: string) => boolean
11
+ }
package/src/helper.ts CHANGED
@@ -2,26 +2,36 @@ import { KeyType } from './consts.js'
2
2
  import { makeKeyPairModel } from './model.js'
3
3
  import { plugins } from './plugins/index.js'
4
4
  import type { KeyPair, KeyPairModel } from './types.js'
5
- import { prepareKey } from './utils.js'
5
+ import { keyUtils } from './utils.js'
6
+ import type { KeyHelper } from './helper/types.js'
6
7
 
7
- export const fromPubKey = (pubKey: string, type?: string): KeyPairModel => {
8
- if (type == null) {
9
- [type, pubKey] = pubKey.includes(':') ? pubKey.split(':', 2) : [KeyType.ED25519, pubKey]
10
- if (pubKey == null) {
11
- pubKey = type
12
- type = KeyType.ED25519
8
+ export const createKeyHelper = (): KeyHelper => {
9
+ const fromPubKey = (pubKey: string, type?: string): KeyPairModel => {
10
+ if (type == null) {
11
+ [type, pubKey] = pubKey.includes(':') ? pubKey.split(':', 2) : [KeyType.ED25519, pubKey]
12
+ if (pubKey == null) {
13
+ pubKey = type
14
+ type = KeyType.ED25519
15
+ }
16
+ }
17
+
18
+ const keyPair: KeyPair = {
19
+ privateKey: '',
20
+ publicKey: pubKey,
21
+ type,
22
+ address: plugins[type].toAdress(keyUtils.prepareKey(pubKey))
13
23
  }
14
- }
15
24
 
16
- const keyPair: KeyPair = {
17
- privateKey: '',
18
- publicKey: pubKey,
19
- type,
20
- address: plugins[type].toAdress(prepareKey(pubKey))
25
+ return makeKeyPairModel(keyPair)
21
26
  }
22
27
 
23
- return makeKeyPairModel(keyPair)
28
+ const matchAddress = (address: string, pubKey: string): boolean =>
29
+ address === fromPubKey(pubKey).exportAddress()
30
+
31
+ return { fromPubKey, matchAddress }
24
32
  }
25
33
 
26
- export const matchAddress = (address: string, pubKey: string): boolean =>
27
- address === fromPubKey(pubKey).exportAddress()
34
+ export const keyHelper = createKeyHelper()
35
+
36
+ /** @deprecated compat:factory-refactor — use `keyHelper.fromPubKey(…)` */
37
+ export const fromPubKey = (pubKey: string, type?: string): KeyPairModel => keyHelper.fromPubKey(pubKey, type)
package/src/index.ts CHANGED
@@ -1,4 +1,6 @@
1
1
  export type * from './types.js'
2
+ export type * from './helper/types.js'
3
+ export type * from './auth/types.js'
2
4
 
3
5
  export * from './consts.js'
4
6
  export * from './helper.js'
package/src/keypair.ts CHANGED
@@ -2,7 +2,7 @@ import type { KeyPair } from './types.js'
2
2
  import { base64 } from '@scure/base'
3
3
  import { KeyType } from './consts.js'
4
4
  import { plugins } from './plugins/index.js'
5
- import { assertType } from './utils.js'
5
+ import { keyUtils } from './utils.js'
6
6
 
7
7
  export const inputToKeyPair = (input?: KeyPair | string): KeyPair => {
8
8
  let type: string = KeyType.ED25519
@@ -27,7 +27,7 @@ export const inputToKeyPair = (input?: KeyPair | string): KeyPair => {
27
27
  }
28
28
  }
29
29
  if (keyPair == null) {
30
- assertType(type)
30
+ keyUtils.assertType(type)
31
31
  if (primaryKey == null) {
32
32
  primaryKey = plugins[type].random()
33
33
  }
package/src/model.ts CHANGED
@@ -1,18 +1,18 @@
1
- import type { KeyPairModel, KeyPairModelMaker } from './types.js'
1
+ import type { KeyPair, KeyPairModel } from './types.js'
2
2
  import { plugins } from './plugins/index.js'
3
3
  import { base64urlnopad, utf8 } from '@scure/base'
4
- import { assertType, prepareData, prepareKey } from './utils.js'
4
+ import { keyUtils } from './utils.js'
5
5
  import { inputToKeyPair } from './keypair.js'
6
6
 
7
- export const makeKeyPairModel: KeyPairModelMaker = input => {
7
+ export const makeKeyPairModel = (input?: KeyPair | string): KeyPairModel => {
8
8
  const keyPair = inputToKeyPair(input)
9
9
 
10
10
  const _model: KeyPairModel = {
11
11
  keyPair,
12
12
 
13
13
  sign: async (data) => {
14
- data = prepareData(data)
15
- assertType(_model.keyPair?.type)
14
+ data = keyUtils.prepareData(data)
15
+ keyUtils.assertType(_model.keyPair?.type)
16
16
 
17
17
  if (_model.keyPair == null) {
18
18
  throw new Error('basic.keys:missing-keypair')
@@ -25,14 +25,14 @@ export const makeKeyPairModel: KeyPairModelMaker = input => {
25
25
  return base64urlnopad.encode(
26
26
  plugins[_model.keyPair.type].sign(
27
27
  data as Uint8Array,
28
- prepareKey(_model.keyPair.privateKey)
28
+ keyUtils.prepareKey(_model.keyPair.privateKey)
29
29
  )
30
30
  )
31
31
  },
32
32
 
33
33
  verify: async (data, signature) => {
34
- data = prepareData(data)
35
- assertType(_model.keyPair?.type)
34
+ data = keyUtils.prepareData(data)
35
+ keyUtils.assertType(_model.keyPair?.type)
36
36
  const sig = base64urlnopad.decode(signature)
37
37
 
38
38
  if (_model.keyPair == null) {
@@ -42,12 +42,12 @@ export const makeKeyPairModel: KeyPairModelMaker = input => {
42
42
  return plugins[_model.keyPair.type].verify(
43
43
  data as Uint8Array,
44
44
  sig,
45
- prepareKey(_model.keyPair.publicKey)
45
+ keyUtils.prepareKey(_model.keyPair.publicKey)
46
46
  )
47
47
  },
48
48
 
49
49
  export: () => {
50
- assertType(_model.keyPair?.type)
50
+ keyUtils.assertType(_model.keyPair?.type)
51
51
 
52
52
  if (_model.keyPair == null) {
53
53
  throw new Error('basic.keys:missing-keypair')
@@ -57,7 +57,7 @@ export const makeKeyPairModel: KeyPairModelMaker = input => {
57
57
  },
58
58
 
59
59
  exportPublic: () => {
60
- assertType(_model.keyPair?.type)
60
+ keyUtils.assertType(_model.keyPair?.type)
61
61
 
62
62
  if (_model.keyPair == null) {
63
63
  throw new Error('basic.keys:missing-keypair')
@@ -67,7 +67,7 @@ export const makeKeyPairModel: KeyPairModelMaker = input => {
67
67
  },
68
68
 
69
69
  exportAddress: () => {
70
- assertType(_model.keyPair?.type)
70
+ keyUtils.assertType(_model.keyPair?.type)
71
71
 
72
72
  if (_model.keyPair == null) {
73
73
  throw new Error('basic.keys:missing-keypair')
@@ -77,8 +77,8 @@ export const makeKeyPairModel: KeyPairModelMaker = input => {
77
77
  },
78
78
 
79
79
  encrypt: async data => {
80
- data = prepareData(data)
81
- assertType(_model.keyPair?.type)
80
+ data = keyUtils.prepareData(data)
81
+ keyUtils.assertType(_model.keyPair?.type)
82
82
 
83
83
  if (_model.keyPair == null) {
84
84
  throw new Error('basic.keys:missing-keypair')
@@ -87,7 +87,7 @@ export const makeKeyPairModel: KeyPairModelMaker = input => {
87
87
  return base64urlnopad.encode(
88
88
  plugins[_model.keyPair.type].encrypt(
89
89
  data as Uint8Array,
90
- prepareKey(_model.keyPair.publicKey)
90
+ keyUtils.prepareKey(_model.keyPair.publicKey)
91
91
  )
92
92
  )
93
93
  },
@@ -105,7 +105,7 @@ export const makeKeyPairModel: KeyPairModelMaker = input => {
105
105
 
106
106
  dcrpt: async data => {
107
107
  data = data instanceof Uint8Array ? data : base64urlnopad.decode(data as string)
108
- assertType(_model.keyPair?.type)
108
+ keyUtils.assertType(_model.keyPair?.type)
109
109
 
110
110
  if (_model.keyPair == null) {
111
111
  throw new Error('basic.keys:missing-keypair')
@@ -113,7 +113,7 @@ export const makeKeyPairModel: KeyPairModelMaker = input => {
113
113
 
114
114
  return plugins[_model.keyPair.type].decrypt(
115
115
  data as Uint8Array,
116
- prepareKey(_model.keyPair.privateKey)
116
+ keyUtils.prepareKey(_model.keyPair.privateKey)
117
117
  )
118
118
  }
119
119
  }
@@ -0,0 +1,15 @@
1
+ /** The low-level key and payload primitives the key models and plugins share. */
2
+ export interface KeyUtils {
3
+ /** The 20-byte address of a public key: the tail of its keccak-256 digest. */
4
+ toAddress: (publicKey: Uint8Array) => Uint8Array
5
+ /** A base64-encoded key as bytes. */
6
+ prepareKey: (key: string) => Uint8Array
7
+ /**
8
+ * Bytes to sign, verify or encrypt: an object is canonicalized, a string UTF-8 encoded.
9
+ *
10
+ * @throws {Error} `basic.keys:sign-data-type` for anything else
11
+ */
12
+ prepareData: (data: unknown) => Uint8Array
13
+ /** @throws {Error} `basic.keys:unknown-type` for a key type no plugin is registered for */
14
+ assertType: (type?: string) => void
15
+ }
package/src/utils.ts CHANGED
@@ -2,34 +2,41 @@ import { keccak_256 } from '@noble/hashes/sha3.js'
2
2
  import { base64, utf8 } from '@scure/base'
3
3
  import { plugins } from './plugins/index.js'
4
4
  import canonicalize from 'canonicalize'
5
+ import type { KeyUtils } from './utils/types.js'
5
6
 
6
- export const toAddress = (publicKey: Uint8Array): Uint8Array =>
7
- keccak_256(publicKey.slice(4)).slice(-20)
7
+ export const createKeyUtils = (): KeyUtils => {
8
+ const toAddress = (publicKey: Uint8Array): Uint8Array =>
9
+ keccak_256(publicKey.slice(4)).slice(-20)
8
10
 
9
- export const prepareKey = (key: string): Uint8Array =>
10
- base64.decode(key)
11
+ const prepareKey = (key: string): Uint8Array =>
12
+ base64.decode(key)
13
+
14
+ const prepareData = (data: unknown): Uint8Array => {
15
+ if (typeof data === 'object') {
16
+ if (!(data instanceof Uint8Array)) {
17
+ data = canonicalize(data)
18
+ }
19
+ }
20
+ if (typeof data === 'string') {
21
+ data = utf8.decode(data)
22
+ } else if (!(data instanceof Uint8Array)) {
23
+ throw new Error('basic.keys:sign-data-type')
24
+ }
11
25
 
12
- export const prepareData = (data: unknown): Uint8Array => {
13
- if (typeof data === 'object') {
14
26
  if (!(data instanceof Uint8Array)) {
15
- data = canonicalize(data)
27
+ throw new Error('basic.keys:sign-data-type')
16
28
  }
17
- }
18
- if (typeof data === 'string') {
19
- data = utf8.decode(data)
20
- } else if (!(data instanceof Uint8Array)) {
21
- throw new Error('basic.keys:sign-data-type')
29
+
30
+ return data
22
31
  }
23
32
 
24
- if (!(data instanceof Uint8Array)) {
25
- throw new Error('basic.keys:sign-data-type')
33
+ const assertType = (type?: string): void => {
34
+ if (type == null || !(type in plugins)) {
35
+ throw new Error('basic.keys:unknown-type')
36
+ }
26
37
  }
27
38
 
28
- return data
39
+ return { toAddress, prepareKey, prepareData, assertType }
29
40
  }
30
41
 
31
- export const assertType = (type?: string): void => {
32
- if (type == null || !(type in plugins)) {
33
- throw new Error('basic.keys:unknown-type')
34
- }
35
- }
42
+ export const keyUtils = createKeyUtils()
@@ -1,5 +1,5 @@
1
1
  import { describe, expect, test } from 'bun:test'
2
- import { packAuthCredentials, unpackAuthCredentials } from '@owlmeans/basic-keys'
2
+ import { authCredentialsHelper } from '@owlmeans/basic-keys'
3
3
  import { AuthRole, AuthenticationType } from '@owlmeans/auth'
4
4
  import type { AuthCredentials } from '@owlmeans/auth'
5
5
  import { fixtureKey } from './context.js'
@@ -15,8 +15,8 @@ const baseCreds = (): Omit<AuthCredentials, 'credential'> => ({
15
15
  describe('@owlmeans/basic-keys — packAuthCredentials round-trip', () => {
16
16
  test('signs and verifies credentials with extras attached', async () => {
17
17
  const key = fixtureKey()
18
- const signed = await packAuthCredentials(baseCreds(), { nonce: 'n-1' }, key)
19
- const result = await unpackAuthCredentials<{ nonce: string }>(signed, key)
18
+ const signed = await authCredentialsHelper.packAuthCredentials(baseCreds(), { nonce: 'n-1' }, key)
19
+ const result = await authCredentialsHelper.unpackAuthCredentials<{ nonce: string }>(signed, key)
20
20
  expect(result.isValid).toBe(true)
21
21
  expect(result.extras?.nonce).toBe('n-1')
22
22
  })
@@ -24,8 +24,8 @@ describe('@owlmeans/basic-keys — packAuthCredentials round-trip', () => {
24
24
  test('verification fails for a different keypair', async () => {
25
25
  const signing = fixtureKey('alice')
26
26
  const other = fixtureKey('bob')
27
- const signed = await packAuthCredentials(baseCreds(), { nonce: 'n-1' }, signing)
28
- const result = await unpackAuthCredentials(signed, other)
27
+ const signed = await authCredentialsHelper.packAuthCredentials(baseCreds(), { nonce: 'n-1' }, signing)
28
+ const result = await authCredentialsHelper.unpackAuthCredentials(signed, other)
29
29
  expect(result.isValid).toBe(false)
30
30
  })
31
31
  })
@@ -1,5 +1,5 @@
1
1
  import { describe, expect, test } from 'bun:test'
2
- import { fromPubKey, makeKeyPairModel, matchAddress } from '@owlmeans/basic-keys'
2
+ import { keyHelper, makeKeyPairModel } from '@owlmeans/basic-keys'
3
3
  import { fixtureKey } from './context.js'
4
4
 
5
5
  describe('@owlmeans/basic-keys — KeyPairModel sign/verify', () => {
@@ -26,13 +26,13 @@ describe('@owlmeans/basic-keys — public-key-only verification (fromPubKey)', (
26
26
  const signing = fixtureKey('peer-keys-spec')
27
27
  const sig = await signing.sign({ event: 'check' })
28
28
 
29
- const verifying = fromPubKey(signing.exportPublic())
29
+ const verifying = keyHelper.fromPubKey(signing.exportPublic())
30
30
  expect(await verifying.verify({ event: 'check' }, sig)).toBe(true)
31
31
  })
32
32
 
33
33
  test('matchAddress agrees with exportAddress() on the same keypair', () => {
34
34
  const key = fixtureKey('addr-spec')
35
- expect(matchAddress(key.exportAddress(), key.exportPublic())).toBe(true)
35
+ expect(keyHelper.matchAddress(key.exportAddress(), key.exportPublic())).toBe(true)
36
36
  })
37
37
 
38
38
  test('export() round-trips into a new model that produces the same public key', async () => {