@kokuin/browser 0.1.0 → 0.1.2

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 (2) hide show
  1. package/lib/identity.js +24 -8
  2. package/package.json +5 -3
package/lib/identity.js CHANGED
@@ -1,7 +1,9 @@
1
+ import { createTracer, KokuinAttributeKeys, KokuinSpanNames } from '@kokuin/otel';
1
2
  import { CODECS, getDID } from '@kokuin/token';
3
+ import { p256 } from '@noble/curves/nist.js';
2
4
  import { b64uFromJSON, fromUTF, toB64U } from '@sozai/codec';
3
5
  import { getLogger } from '@sozai/log';
4
- import { AttributeKeys, createTracer, SpanNames, withSpan } from '@sozai/otel';
6
+ import { withSpan } from '@sozai/otel';
5
7
  import { BrowserKeyStore } from './store.js';
6
8
  import { getPublicKey } from './utils.js';
7
9
  const tracer = createTracer('keystore.browser');
@@ -9,6 +11,18 @@ const logger = getLogger([
9
11
  'kokuin',
10
12
  'browser'
11
13
  ]);
14
+ // P-256 curve order, used to reflect a high-S signature to its low-S form.
15
+ const P256_N = p256.Point.Fn.ORDER;
16
+ // Web Crypto's subtle.sign does not enforce low-S, so ~half of ECDSA signatures
17
+ // have s > n/2 (high-S). The @kokuin/token verifier runs with `lowS: true` and
18
+ // rejects those for malleability safety, so we normalize here for cross-stack
19
+ // compatibility. `hasHighS()` is the same predicate noble's verifier applies.
20
+ function normalizeSignatureToLowS(sig) {
21
+ // sig is IEEE P1363 compact format: r (32 bytes) || s (32 bytes).
22
+ const parsed = p256.Signature.fromBytes(sig, 'compact');
23
+ if (!parsed.hasHighS()) return sig;
24
+ return new p256.Signature(parsed.r, P256_N - parsed.s).toBytes('compact');
25
+ }
12
26
  async function createBrowserSigningIdentity(keyPair) {
13
27
  const publicKey = await getPublicKey(keyPair);
14
28
  const id = getDID(CODECS.ES256, publicKey);
@@ -31,10 +45,12 @@ async function createBrowserSigningIdentity(keyPair) {
31
45
  name: 'ECDSA',
32
46
  hash: 'SHA-256'
33
47
  }, keyPair.privateKey, messageBytes.buffer);
48
+ // Normalize to low-S: @kokuin/token verifier rejects high-S signatures
49
+ const normalizedSig = normalizeSignatureToLowS(new Uint8Array(signatureBuffer));
34
50
  return {
35
51
  header: fullHeader,
36
52
  payload: fullPayload,
37
- signature: toB64U(new Uint8Array(signatureBuffer)),
53
+ signature: toB64U(normalizedSig),
38
54
  data
39
55
  };
40
56
  }
@@ -45,9 +61,9 @@ async function createBrowserSigningIdentity(keyPair) {
45
61
  };
46
62
  }
47
63
  export async function provideSigningIdentity(keyID, useStore) {
48
- return withSpan(tracer, SpanNames.KEYSTORE_GET_OR_CREATE, {
64
+ return withSpan(tracer, KokuinSpanNames.KEYSTORE_GET_OR_CREATE, {
49
65
  attributes: {
50
- [AttributeKeys.KEYSTORE_STORE_TYPE]: 'browser'
66
+ [KokuinAttributeKeys.KEYSTORE_STORE_TYPE]: 'browser'
51
67
  }
52
68
  }, async (span)=>{
53
69
  const storePromise = useStore == null || typeof useStore === 'string' ? BrowserKeyStore.open(useStore) : Promise.resolve(useStore);
@@ -56,14 +72,14 @@ export async function provideSigningIdentity(keyID, useStore) {
56
72
  const existing = await entry.getAsync();
57
73
  if (existing != null) {
58
74
  const identity = await createBrowserSigningIdentity(existing);
59
- span.setAttribute(AttributeKeys.AUTH_DID, identity.id);
60
- span.setAttribute(AttributeKeys.KEYSTORE_KEY_CREATED, false);
75
+ span.setAttribute(KokuinAttributeKeys.AUTH_DID, identity.id);
76
+ span.setAttribute(KokuinAttributeKeys.KEYSTORE_KEY_CREATED, false);
61
77
  return identity;
62
78
  }
63
79
  const keyPair = await entry.provideAsync();
64
80
  const identity = await createBrowserSigningIdentity(keyPair);
65
- span.setAttribute(AttributeKeys.AUTH_DID, identity.id);
66
- span.setAttribute(AttributeKeys.KEYSTORE_KEY_CREATED, true);
81
+ span.setAttribute(KokuinAttributeKeys.AUTH_DID, identity.id);
82
+ span.setAttribute(KokuinAttributeKeys.KEYSTORE_KEY_CREATED, true);
67
83
  logger.info('New identity generated: {did}', {
68
84
  did: identity.id
69
85
  });
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@kokuin/browser",
3
- "version": "0.1.0",
3
+ "version": "0.1.2",
4
4
  "license": "MIT",
5
5
  "description": "Key store for browser",
6
6
  "keywords": [
@@ -25,11 +25,13 @@
25
25
  ],
26
26
  "sideEffects": false,
27
27
  "dependencies": {
28
+ "@noble/curves": "^2.0.1",
28
29
  "@sozai/async": "^0.1.0",
29
30
  "@sozai/codec": "^0.1.0",
30
31
  "@sozai/log": "^0.1.0",
31
- "@sozai/otel": "^0.1.0",
32
- "@kokuin/token": "^0.1.0"
32
+ "@sozai/otel": "^0.2.0",
33
+ "@kokuin/token": "^0.1.1",
34
+ "@kokuin/otel": "^0.1.0"
33
35
  },
34
36
  "scripts": {
35
37
  "build:clean": "del lib",