Skip to content

Commit 3378128

Browse files
committed
fix: add runtime validation for algorithm and conditionally pass to deriveKeypair
- Validate opts.algorithm in constructor: throw if not a known ECDSA value - Only pass algorithm to deriveKeypair when explicitly provided (don't force DEFAULT_ALGORITHM) - Preserves fallback to seed-encoded algorithm when algorithm isn't specified - Addresses CodeRabbit review feedback on potential issue (line 122-126) and optimization (line 324-331)
1 parent 3251f5c commit 3378128

2 files changed

Lines changed: 10351 additions & 10 deletions

File tree

packages/xrpl/src/Wallet/index.ts

Lines changed: 24 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -119,11 +119,21 @@ export class Wallet {
119119
? ensureClassicAddress(opts.masterAddress)
120120
: deriveAddress(publicKey)
121121
this.seed = opts.seed
122-
this.algorithm =
123-
opts.algorithm ??
124-
(publicKey.toUpperCase().startsWith('ED')
125-
? ECDSA.ed25519
126-
: ECDSA.secp256k1)
122+
123+
// Validate algorithm if provided
124+
if (
125+
opts.algorithm != null &&
126+
!Object.values(ECDSA).includes(opts.algorithm)
127+
) {
128+
throw new ValidationError('Invalid cryptographic signing algorithm')
129+
}
130+
131+
// Infer from public key if not provided
132+
const inferredAlgorithm = publicKey.toUpperCase().startsWith('ED')
133+
? ECDSA.ed25519
134+
: ECDSA.secp256k1
135+
136+
this.algorithm = opts.algorithm ?? inferredAlgorithm
127137
}
128138

129139
/**
@@ -321,14 +331,18 @@ export class Wallet {
321331
seed: string,
322332
opts: { masterAddress?: string; algorithm?: ECDSA } = {},
323333
): Wallet {
324-
const algorithm = opts.algorithm ?? DEFAULT_ALGORITHM
325-
const { publicKey, privateKey } = deriveKeypair(seed, {
326-
algorithm,
327-
})
334+
// Only pass algorithm to deriveKeypair if explicitly provided
335+
const deriveKeypairOpts: { algorithm?: ECDSA } = {}
336+
if (opts.algorithm !== undefined) {
337+
deriveKeypairOpts.algorithm = opts.algorithm
338+
}
339+
340+
const { publicKey, privateKey } = deriveKeypair(seed, deriveKeypairOpts)
341+
328342
return new Wallet(publicKey, privateKey, {
329343
seed,
330344
masterAddress: opts.masterAddress,
331-
algorithm,
345+
algorithm: opts.algorithm,
332346
})
333347
}
334348

0 commit comments

Comments
 (0)