@kokuin/browser 0.1.0 → 0.1.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/lib/identity.js +16 -1
- package/package.json +2 -1
package/lib/identity.js
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import { CODECS, getDID } from '@kokuin/token';
|
|
2
|
+
import { p256 } from '@noble/curves/nist.js';
|
|
2
3
|
import { b64uFromJSON, fromUTF, toB64U } from '@sozai/codec';
|
|
3
4
|
import { getLogger } from '@sozai/log';
|
|
4
5
|
import { AttributeKeys, createTracer, SpanNames, withSpan } from '@sozai/otel';
|
|
@@ -9,6 +10,18 @@ const logger = getLogger([
|
|
|
9
10
|
'kokuin',
|
|
10
11
|
'browser'
|
|
11
12
|
]);
|
|
13
|
+
// P-256 curve order, used to reflect a high-S signature to its low-S form.
|
|
14
|
+
const P256_N = p256.Point.Fn.ORDER;
|
|
15
|
+
// Web Crypto's subtle.sign does not enforce low-S, so ~half of ECDSA signatures
|
|
16
|
+
// have s > n/2 (high-S). The @kokuin/token verifier runs with `lowS: true` and
|
|
17
|
+
// rejects those for malleability safety, so we normalize here for cross-stack
|
|
18
|
+
// compatibility. `hasHighS()` is the same predicate noble's verifier applies.
|
|
19
|
+
function normalizeSignatureToLowS(sig) {
|
|
20
|
+
// sig is IEEE P1363 compact format: r (32 bytes) || s (32 bytes).
|
|
21
|
+
const parsed = p256.Signature.fromBytes(sig, 'compact');
|
|
22
|
+
if (!parsed.hasHighS()) return sig;
|
|
23
|
+
return new p256.Signature(parsed.r, P256_N - parsed.s).toBytes('compact');
|
|
24
|
+
}
|
|
12
25
|
async function createBrowserSigningIdentity(keyPair) {
|
|
13
26
|
const publicKey = await getPublicKey(keyPair);
|
|
14
27
|
const id = getDID(CODECS.ES256, publicKey);
|
|
@@ -31,10 +44,12 @@ async function createBrowserSigningIdentity(keyPair) {
|
|
|
31
44
|
name: 'ECDSA',
|
|
32
45
|
hash: 'SHA-256'
|
|
33
46
|
}, keyPair.privateKey, messageBytes.buffer);
|
|
47
|
+
// Normalize to low-S: @kokuin/token verifier rejects high-S signatures
|
|
48
|
+
const normalizedSig = normalizeSignatureToLowS(new Uint8Array(signatureBuffer));
|
|
34
49
|
return {
|
|
35
50
|
header: fullHeader,
|
|
36
51
|
payload: fullPayload,
|
|
37
|
-
signature: toB64U(
|
|
52
|
+
signature: toB64U(normalizedSig),
|
|
38
53
|
data
|
|
39
54
|
};
|
|
40
55
|
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@kokuin/browser",
|
|
3
|
-
"version": "0.1.
|
|
3
|
+
"version": "0.1.1",
|
|
4
4
|
"license": "MIT",
|
|
5
5
|
"description": "Key store for browser",
|
|
6
6
|
"keywords": [
|
|
@@ -25,6 +25,7 @@
|
|
|
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",
|