@oxy.so/protocol 1.0.1 → 1.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.
- package/LICENSE +675 -201
- package/NOTICE +7 -2
- package/dist/cjs/.tsbuildinfo +1 -1
- package/dist/cjs/auth/registrationPow.js +149 -0
- package/dist/cjs/index.js +9 -1
- package/dist/esm/.tsbuildinfo +1 -1
- package/dist/esm/auth/registrationPow.js +143 -0
- package/dist/esm/index.js +4 -0
- package/dist/types/.tsbuildinfo +1 -1
- package/dist/types/auth/registrationPow.d.ts +102 -0
- package/dist/types/index.d.ts +1 -0
- package/package.json +3 -3
- package/src/__tests__/registrationPow.test.ts +73 -0
- package/src/auth/registrationPow.ts +146 -0
- package/src/index.ts +10 -0
|
@@ -0,0 +1,149 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
/**
|
|
3
|
+
* Registration proof-of-work — the message format and difficulty check a
|
|
4
|
+
* signup grinds a nonce against, shared verbatim between the client (which
|
|
5
|
+
* SOLVES it) and the server (which CHECKS it).
|
|
6
|
+
*
|
|
7
|
+
* ## Why this exists
|
|
8
|
+
*
|
|
9
|
+
* `POST /auth/register` mints an account from nothing more than a freshly
|
|
10
|
+
* generated secp256k1 keypair and a signature over
|
|
11
|
+
* `oxy:register:{publicKey}:{timestamp}` — computationally free. A PoW nonce
|
|
12
|
+
* makes each account cost a small, real amount of CPU time to mint, without
|
|
13
|
+
* asking a real signer for anything beyond what they already do locally: no
|
|
14
|
+
* server round trip, no third-party CAPTCHA, no extra user-visible step.
|
|
15
|
+
*
|
|
16
|
+
* ## Single source, on purpose
|
|
17
|
+
*
|
|
18
|
+
* The client must SOLVE the exact message the server CHECKS, so the message
|
|
19
|
+
* format and the difficulty threshold live here once, in a package both
|
|
20
|
+
* `@oxy.so/core` (the client SDK) and the API already depend on for
|
|
21
|
+
* secp256k1 — see `packages/contracts/src/username.ts`'s docblock for why
|
|
22
|
+
* this codebase treats "the same rule declared twice" as a bug class, not a
|
|
23
|
+
* style preference. This file is deliberately CRYPTO-FREE: it is pure
|
|
24
|
+
* string/bit arithmetic over a caller-supplied hex digest, so it needs no
|
|
25
|
+
* platform hashing primitive and is trivially unit-testable with fixed
|
|
26
|
+
* digests. Each side hashes with whatever SHA-256 it already has — Node's
|
|
27
|
+
* `crypto` server-side (`packages/api/src/controllers/session.controller.ts`),
|
|
28
|
+
* `@noble/hashes/sha256` client-side (`packages/core/src/crypto/registrationPow.ts`,
|
|
29
|
+
* already a `@oxy.so/core` dependency — see `crypto/kdf.ts`,
|
|
30
|
+
* `crypto/identityProof.ts`) — and both produce byte-identical digests over
|
|
31
|
+
* the same message, because SHA-256 has exactly one correct output for a
|
|
32
|
+
* given input regardless of implementation.
|
|
33
|
+
*
|
|
34
|
+
* ## What this is NOT
|
|
35
|
+
*
|
|
36
|
+
* Not a defense against a targeted, patient attacker: nothing stops one from
|
|
37
|
+
* choosing their OWN `timestamp` a few minutes in the future and grinding the
|
|
38
|
+
* nonce ahead of time, banking a queue of pre-solved (timestamp, nonce) pairs
|
|
39
|
+
* to spend inside the 5-minute signature freshness window
|
|
40
|
+
* (`SignatureService.isTimestampFresh` / `MAX_SIGNATURE_AGE_MS`). Closing that
|
|
41
|
+
* needs a server-issued, single-use challenge — a real round trip and server
|
|
42
|
+
* state this deliberately does not add, because the goal here is raising the
|
|
43
|
+
* COST of unattended bulk signup, not proving liveness. Combined with a
|
|
44
|
+
* per-IP rate limit on `/auth/register`, it still meaningfully raises the
|
|
45
|
+
* price of automated mass account creation over today's zero-cost baseline.
|
|
46
|
+
*
|
|
47
|
+
* ## Rollout: advisory only, for now
|
|
48
|
+
*
|
|
49
|
+
* `POST /auth/register`'s `powNonce` field is OPTIONAL on the wire and the
|
|
50
|
+
* server does not yet reject a missing or failing one — see the comment at
|
|
51
|
+
* the check site in `packages/api/src/controllers/session.controller.ts` for
|
|
52
|
+
* why (every Oxy app consumes the SDK as a published package, not this
|
|
53
|
+
* monorepo live, so hard-enforcing before every app has picked up a client
|
|
54
|
+
* new enough to send it would break their signup outright) and exactly what
|
|
55
|
+
* to change to flip it to a hard 400.
|
|
56
|
+
*/
|
|
57
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
58
|
+
exports.REGISTRATION_POW_DIFFICULTY_BITS = void 0;
|
|
59
|
+
exports.registrationPowMessage = registrationPowMessage;
|
|
60
|
+
exports.countLeadingZeroBits = countLeadingZeroBits;
|
|
61
|
+
exports.meetsRegistrationPowDifficulty = meetsRegistrationPowDifficulty;
|
|
62
|
+
/**
|
|
63
|
+
* Leading zero BITS contributed by one lowercase hex nibble.
|
|
64
|
+
*
|
|
65
|
+
* A hex digit is 4 bits: `'0'` contributes all 4, `'8'`–`'f'` (binary `1xxx`)
|
|
66
|
+
* contribute none because their own leading bit is already `1`. Spelled out
|
|
67
|
+
* as a table rather than derived with `Math.clz32` so the mapping is checkable
|
|
68
|
+
* by eye against the difficulty this gates.
|
|
69
|
+
*/
|
|
70
|
+
const NIBBLE_LEADING_ZERO_BITS = {
|
|
71
|
+
'0': 4,
|
|
72
|
+
'1': 3,
|
|
73
|
+
'2': 2,
|
|
74
|
+
'3': 2,
|
|
75
|
+
'4': 1,
|
|
76
|
+
'5': 1,
|
|
77
|
+
'6': 1,
|
|
78
|
+
'7': 1,
|
|
79
|
+
'8': 0,
|
|
80
|
+
'9': 0,
|
|
81
|
+
a: 0,
|
|
82
|
+
b: 0,
|
|
83
|
+
c: 0,
|
|
84
|
+
d: 0,
|
|
85
|
+
e: 0,
|
|
86
|
+
f: 0,
|
|
87
|
+
};
|
|
88
|
+
/**
|
|
89
|
+
* The message a registration PoW nonce is grinded against, given the same
|
|
90
|
+
* `publicKey`/`timestamp` the registration signature (`oxy:register:…`)
|
|
91
|
+
* already commits to. A distinct prefix (`register-pow`, not `register`) so a
|
|
92
|
+
* solved PoW nonce can never be replayed as a registration signature input or
|
|
93
|
+
* vice versa — the two are unrelated preimages even for the same
|
|
94
|
+
* `publicKey`/`timestamp` pair.
|
|
95
|
+
*/
|
|
96
|
+
function registrationPowMessage(publicKey, timestamp, nonce) {
|
|
97
|
+
return `oxy:register-pow:${publicKey}:${timestamp}:${nonce}`;
|
|
98
|
+
}
|
|
99
|
+
/**
|
|
100
|
+
* The number of leading zero bits {@link meetsRegistrationPowDifficulty}
|
|
101
|
+
* requires of a solved nonce's digest.
|
|
102
|
+
*
|
|
103
|
+
* 16 bits ⇒ 65,536 SHA-256 attempts on average to solve. The client grinds
|
|
104
|
+
* with `@noble/hashes/sha256` — a pure-JS, SYNCHRONOUS implementation with
|
|
105
|
+
* "identical behaviour on web, Node, and React Native with zero WebCrypto /
|
|
106
|
+
* native-module dependency" (its own doc comment in `crypto/kdf.ts`) — so,
|
|
107
|
+
* unlike a solve loop built on a platform hashing primitive that crosses a
|
|
108
|
+
* JS↔native bridge per call (`expo-crypto`'s `digestStringAsync`, which this
|
|
109
|
+
* deliberately avoids for exactly that reason), there is no per-attempt
|
|
110
|
+
* bridge cost to budget for. The remaining uncertainty is Hermes' lack of a
|
|
111
|
+
* JIT, which can make a tight pure-JS loop meaningfully slower than V8 — 16
|
|
112
|
+
* bits is chosen to stay a sub-second grind even under that penalty, without
|
|
113
|
+
* reaching for a bound high enough to make Hermes specifically the deciding
|
|
114
|
+
* factor. Retune this constant (it is the only place the number is declared)
|
|
115
|
+
* once there is real on-device telemetry across the low end of the supported
|
|
116
|
+
* device range.
|
|
117
|
+
*/
|
|
118
|
+
exports.REGISTRATION_POW_DIFFICULTY_BITS = 16;
|
|
119
|
+
/**
|
|
120
|
+
* Count the leading zero BITS of a lowercase hex digest.
|
|
121
|
+
*
|
|
122
|
+
* Stops at the first non-zero nibble — a solved nonce only ever needs to beat
|
|
123
|
+
* a bound in the tens of bits, so scanning the full 256-bit digest is wasted
|
|
124
|
+
* work. A character outside `[0-9a-f]` (not a real digest — a caller passed
|
|
125
|
+
* something malformed) stops the count where it is rather than throwing: the
|
|
126
|
+
* caller compares the result against a difficulty, and an under-count from bad
|
|
127
|
+
* input correctly fails that comparison instead of crashing a request path.
|
|
128
|
+
*/
|
|
129
|
+
function countLeadingZeroBits(hexDigest) {
|
|
130
|
+
let bits = 0;
|
|
131
|
+
for (const char of hexDigest.toLowerCase()) {
|
|
132
|
+
const nibbleZeroBits = NIBBLE_LEADING_ZERO_BITS[char];
|
|
133
|
+
if (nibbleZeroBits === undefined)
|
|
134
|
+
break;
|
|
135
|
+
bits += nibbleZeroBits;
|
|
136
|
+
if (nibbleZeroBits < 4)
|
|
137
|
+
break;
|
|
138
|
+
}
|
|
139
|
+
return bits;
|
|
140
|
+
}
|
|
141
|
+
/**
|
|
142
|
+
* Whether a SHA-256 hex digest — of {@link registrationPowMessage} — clears
|
|
143
|
+
* `difficultyBits` leading zero bits. The one predicate both the client's
|
|
144
|
+
* solve loop and the server's check call, so neither can drift from what
|
|
145
|
+
* "solved" means.
|
|
146
|
+
*/
|
|
147
|
+
function meetsRegistrationPowDifficulty(hexDigest, difficultyBits) {
|
|
148
|
+
return countLeadingZeroBits(hexDigest) >= difficultyBits;
|
|
149
|
+
}
|
package/dist/cjs/index.js
CHANGED
|
@@ -11,7 +11,7 @@
|
|
|
11
11
|
* subpath so they never enter React Native / web bundles.
|
|
12
12
|
*/
|
|
13
13
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
14
|
-
exports.loadSharedIdentityBridge = exports.getRandomBytesRN = exports.loadAsyncStorage = exports.loadSecureStore = exports.loadExpoCrypto = exports.loadNodeCrypto = exports.isNodeJS = exports.isReactNative = exports.isAuthorizedKey = exports.verifyCheckpointSignature = exports.signCheckpoint = exports.checkpointHash = exports.checkpointSigningInput = exports.verifyInclusionProof = exports.inclusionProof = exports.buildTransparencyTreeFromHeads = exports.buildTransparencyTree = exports.transparencyLeafHash = exports.EMPTY_TRANSPARENCY_ROOT = exports.verifyAndAppend = exports.DEFAULT_CLOCK_SKEW_MS = exports.verifyEnvelope = exports.checkContinuity = exports.UNCHAINED_SEQ = exports.verifyEnvelopeSignature = exports.signEnvelope = exports.verifySignature = exports.signMessage = exports.computeRecordId = exports.sha256 = exports.signedRecordSigningInput = exports.canonicalize = void 0;
|
|
14
|
+
exports.loadSharedIdentityBridge = exports.getRandomBytesRN = exports.loadAsyncStorage = exports.loadSecureStore = exports.loadExpoCrypto = exports.loadNodeCrypto = exports.isNodeJS = exports.isReactNative = exports.isAuthorizedKey = exports.REGISTRATION_POW_DIFFICULTY_BITS = exports.meetsRegistrationPowDifficulty = exports.countLeadingZeroBits = exports.registrationPowMessage = exports.verifyCheckpointSignature = exports.signCheckpoint = exports.checkpointHash = exports.checkpointSigningInput = exports.verifyInclusionProof = exports.inclusionProof = exports.buildTransparencyTreeFromHeads = exports.buildTransparencyTree = exports.transparencyLeafHash = exports.EMPTY_TRANSPARENCY_ROOT = exports.verifyAndAppend = exports.DEFAULT_CLOCK_SKEW_MS = exports.verifyEnvelope = exports.checkContinuity = exports.UNCHAINED_SEQ = exports.verifyEnvelopeSignature = exports.signEnvelope = exports.verifySignature = exports.signMessage = exports.computeRecordId = exports.sha256 = exports.signedRecordSigningInput = exports.canonicalize = void 0;
|
|
15
15
|
// ---------------------------------------------------------------------------
|
|
16
16
|
// Envelope — canonical JSON, signing input, content address, signing/verify
|
|
17
17
|
// ---------------------------------------------------------------------------
|
|
@@ -52,6 +52,14 @@ Object.defineProperty(exports, "checkpointHash", { enumerable: true, get: functi
|
|
|
52
52
|
Object.defineProperty(exports, "signCheckpoint", { enumerable: true, get: function () { return checkpoint_1.signCheckpoint; } });
|
|
53
53
|
Object.defineProperty(exports, "verifyCheckpointSignature", { enumerable: true, get: function () { return checkpoint_1.verifyCheckpointSignature; } });
|
|
54
54
|
// ---------------------------------------------------------------------------
|
|
55
|
+
// Auth — registration proof-of-work (message format + difficulty check)
|
|
56
|
+
// ---------------------------------------------------------------------------
|
|
57
|
+
var registrationPow_1 = require("./auth/registrationPow");
|
|
58
|
+
Object.defineProperty(exports, "registrationPowMessage", { enumerable: true, get: function () { return registrationPow_1.registrationPowMessage; } });
|
|
59
|
+
Object.defineProperty(exports, "countLeadingZeroBits", { enumerable: true, get: function () { return registrationPow_1.countLeadingZeroBits; } });
|
|
60
|
+
Object.defineProperty(exports, "meetsRegistrationPowDifficulty", { enumerable: true, get: function () { return registrationPow_1.meetsRegistrationPowDifficulty; } });
|
|
61
|
+
Object.defineProperty(exports, "REGISTRATION_POW_DIFFICULTY_BITS", { enumerable: true, get: function () { return registrationPow_1.REGISTRATION_POW_DIFFICULTY_BITS; } });
|
|
62
|
+
// ---------------------------------------------------------------------------
|
|
55
63
|
// Identity — injected verification-method resolution + authorization rule
|
|
56
64
|
// ---------------------------------------------------------------------------
|
|
57
65
|
var resolver_1 = require("./identity/resolver");
|