Skip to content

Commit d65188b

Browse files
committed
Code Improvements & Bug Fixes
1 parent 9555d07 commit d65188b

2 files changed

Lines changed: 6 additions & 4 deletions

File tree

lib/session.js

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
// helpers like getAddr() / getSigningClient() resolve to the per-request
66
// wallet without explicit plumbing.
77

8-
import { createCipheriv, createDecipheriv, randomBytes, createHash } from 'crypto';
8+
import { createCipheriv, createDecipheriv, randomBytes, createHash, createHmac } from 'crypto';
99
import { AsyncLocalStorage } from 'async_hooks';
1010
import { existsSync, readFileSync, writeFileSync, mkdirSync, chmodSync } from 'fs';
1111
import { join } from 'path';
@@ -178,6 +178,7 @@ export async function sessionFromMnemonic(mnemonic) {
178178
const provAddr = toBech32('sentprov', data);
179179

180180
session = {
181+
kind: 'mnemonic',
181182
addr,
182183
provAddr,
183184
wallet,
@@ -357,7 +358,7 @@ export function buildClearCookie({ secure }) {
357358
export function buildKeplrToken(addr, pubkeyB64) {
358359
if (!_key) throw new Error('Session key not initialized');
359360
const payload = `${addr}|${pubkeyB64 || ''}`;
360-
const mac = createHash('sha256').update(_key).update('|').update(payload).digest('base64url');
361+
const mac = createHmac('sha256', _key).update(payload).digest('base64url');
361362
return `${Buffer.from(payload, 'utf8').toString('base64url')}.${mac}`;
362363
}
363364

@@ -370,7 +371,7 @@ export function parseKeplrToken(token) {
370371
const mac = token.slice(dot + 1);
371372
let payload;
372373
try { payload = Buffer.from(payloadB64, 'base64url').toString('utf8'); } catch { return null; }
373-
const expect = createHash('sha256').update(_key).update('|').update(payload).digest('base64url');
374+
const expect = createHmac('sha256', _key).update(payload).digest('base64url');
374375
if (expect !== mac) return null;
375376
const [addr, pubkeyB64] = payload.split('|');
376377
if (!addr || !addr.startsWith('sent1')) return null;

server.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
// Cache (cached/cacheInvalidate/cacheClear) imported from blue-js-sdk.
55

66
import 'dotenv/config';
7+
import { randomBytes as _cryptoRandomBytes } from 'crypto';
78
import express from 'express';
89
import QRCode from 'qrcode';
910
import { fileURLToPath } from 'url';
@@ -1341,7 +1342,7 @@ function gcKeplrChallenges() {
13411342

13421343
app.post('/api/wallet/keplr-challenge', rateLimit('kchal', 30, 60_000), (req, res) => {
13431344
gcKeplrChallenges();
1344-
const nonce = `spm-login-${Date.now()}-${Math.random().toString(36).slice(2, 12)}`;
1345+
const nonce = `spm-login-${Date.now()}-${_cryptoRandomBytes(16).toString('hex')}`;
13451346
keplrChallenges.set(nonce, Date.now() + KEPLR_CHALLENGE_TTL_MS);
13461347
res.json({ nonce });
13471348
});

0 commit comments

Comments
 (0)