mirror of
https://github.com/Xahau/xahau.js.git
synced 2025-11-26 23:25:49 +00:00
feat: use @noble and @scure libraries for cryptography (#2273)
Switch to using `@noble/hashes`, `@noble/curves`, `@scure/base`, `@scure/bip32`, and `@scure/bip39`. This replaces `crypto` polyfills (such as `crypto-browserify`), `create-hash`, `elliptic`, `hash.js`, `bn.js` (both versions), and their many dependencies. This also means there are 33 less dependencies downloaded when running a fresh `npm install` and will make the project much easier to maintain. This reduces the bundle size by 44% (82kb minified and gzipped) over the current 3.0 branch as well as reducing the amount of configuration required to bundle. Closes #1814, #1817, #2272, and #2306 Co-authored-by: Caleb Kniffen <ckniffen@ripple.com>
This commit is contained in:
committed by
Caleb Kniffen
parent
5607320ce2
commit
217b111ef2
81
packages/ripple-keypairs/test/getAlgorithmFromKey.test.ts
Normal file
81
packages/ripple-keypairs/test/getAlgorithmFromKey.test.ts
Normal file
@@ -0,0 +1,81 @@
|
||||
import { getAlgorithmFromKey } from '../src/utils/getAlgorithmFromKey'
|
||||
|
||||
function hexData(count: number) {
|
||||
// for our purposes any hex will do
|
||||
return 'a'.repeat(count)
|
||||
}
|
||||
|
||||
describe('getAlgorithmFromKey', () => {
|
||||
it('should return ed25519 for valid ed25519 private key', () => {
|
||||
const privateKey = `ed${hexData(64)}`
|
||||
expect(getAlgorithmFromKey(privateKey, 'private')).toEqual('ed25519')
|
||||
})
|
||||
|
||||
it('should return ed25519 for valid ed25519 public key', () => {
|
||||
const publicKey = `ed${hexData(64)}`
|
||||
expect(getAlgorithmFromKey(publicKey, 'public')).toEqual('ed25519')
|
||||
})
|
||||
|
||||
it('should return ecdsa-secp256k1 for valid secp256k1 private key without prefix', () => {
|
||||
// 32 bytes, no prefix
|
||||
const privateKey = hexData(64)
|
||||
expect(getAlgorithmFromKey(privateKey, 'private')).toEqual(
|
||||
'ecdsa-secp256k1',
|
||||
)
|
||||
})
|
||||
|
||||
it('should return ecdsa-secp256k1 for valid secp256k1 private key with 0x00 prefix', () => {
|
||||
// 33 bytes, 0x00 prefix
|
||||
const privateKey = `00${hexData(64)}`
|
||||
expect(getAlgorithmFromKey(privateKey, 'private')).toEqual(
|
||||
'ecdsa-secp256k1',
|
||||
)
|
||||
})
|
||||
|
||||
it('should return ecdsa-secp256k1 for valid secp256k1 public key with 0x02 prefix', () => {
|
||||
// 33 bytes, 0x02 prefix
|
||||
const publicKey = `02${hexData(64)}`
|
||||
expect(getAlgorithmFromKey(publicKey, 'public')).toEqual('ecdsa-secp256k1')
|
||||
})
|
||||
|
||||
it('should throw error for invalid private key format', () => {
|
||||
// Invalid tag and length
|
||||
const privateKey = `ff${hexData(60)}`
|
||||
expect(() => getAlgorithmFromKey(privateKey, 'private'))
|
||||
.toThrowErrorMatchingInlineSnapshot(`
|
||||
"invalid_key:
|
||||
|
||||
Type: private
|
||||
Key: ffaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
|
||||
Prefix: 0xff
|
||||
Length: 31 bytes
|
||||
|
||||
Acceptable private formats are:
|
||||
ecdsa-secp256k1 - Prefix: None Length: 32 bytes
|
||||
ecdsa-secp256k1 - Prefix: 0x00 Length: 33 bytes
|
||||
ed25519 - Prefix: 0xed Length: 33 bytes
|
||||
"
|
||||
`)
|
||||
})
|
||||
|
||||
it('should throw error for invalid public key format', () => {
|
||||
// Invalid tag and length
|
||||
const publicKey = `ff${hexData(60)}`
|
||||
expect(() => getAlgorithmFromKey(publicKey, 'public'))
|
||||
.toThrowErrorMatchingInlineSnapshot(`
|
||||
"invalid_key:
|
||||
|
||||
Type: public
|
||||
Key: ffaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
|
||||
Prefix: 0xff
|
||||
Length: 31 bytes
|
||||
|
||||
Acceptable public formats are:
|
||||
ed25519 - Prefix: 0xed Length: 33 bytes
|
||||
ecdsa-secp256k1 - Prefix: 0x02 Length: 33 bytes
|
||||
ecdsa-secp256k1 - Prefix: 0x03 Length: 33 bytes
|
||||
ecdsa-secp256k1 - Prefix: 0x04 Length: 65 bytes
|
||||
"
|
||||
`)
|
||||
})
|
||||
})
|
||||
Reference in New Issue
Block a user