Add guard check for signing algorithm (#2278)

Previously unsupported algorithm would not throw exceptions.

Co-authored-by: Caleb Kniffen <ckniffen@ripple.com>
This commit is contained in:
Wo Jake
2023-05-17 04:28:23 +08:00
committed by GitHub
parent 4cca7c9088
commit 6b1ac0bd4f
3 changed files with 15 additions and 0 deletions

View File

@@ -4,6 +4,7 @@ Subscribe to [the **xrpl-announce** mailing list](https://groups.google.com/g/xr
## Unreleased
### Added
* Guard check for signing algorithm used in `Wallet.generate()`
* Null and undefined values in transactions are now treated as though the field was not passed in.
### Fixed

View File

@@ -129,8 +129,13 @@ class Wallet {
*
* @param algorithm - The digital signature algorithm to generate an address for.
* @returns A new Wallet derived from a generated seed.
*
* @throws ValidationError when signing algorithm isn't valid
*/
public static generate(algorithm: ECDSA = DEFAULT_ALGORITHM): Wallet {
if (!Object.values(ECDSA).includes(algorithm)) {
throw new ValidationError('Invalid cryptographic signing algorithm')
}
const seed = generateSeed({ algorithm })
return Wallet.fromSeed(seed)
}

View File

@@ -57,6 +57,15 @@ describe('Wallet', function () {
assert.isTrue(wallet.classicAddress.startsWith(classicAddressPrefix))
})
it('generates a new wallet using an invalid/unknown algorithm', function () {
const algorithm = 'test'
assert.throws(() => {
// @ts-expect-error -- We know it is an invalid algorithm
Wallet.generate(algorithm)
}, /Invalid cryptographic signing algorithm/u)
})
it('generates a new wallet using algorithm ecdsa-secp256k1', function () {
const algorithm = ECDSA.secp256k1
const wallet = Wallet.generate(algorithm)