mirror of
https://github.com/Xahau/xahau.js.git
synced 2025-12-01 01:25:48 +00:00
refactor: remove import * where able (#2550)
refactor: remove `import *` where able to BREAKING CHANGE: Moved all methods that were on `Utils` in `@xrplf/secret-numbers` are now individually exported. This affects: - Utils.randomEntropy, - Utils.randomSecret - Utils.entropyToSecret - Utils.secretToEntropy - Utils.calculateChecksum - Utils.checkChecksum - Utils.parseSecretString
This commit is contained in:
@@ -2,6 +2,11 @@
|
||||
|
||||
Subscribe to [the **xrpl-announce** mailing list](https://groups.google.com/g/xrpl-announce) for release announcements. We recommend that `@xrplf/secret-numbers` users stay up-to-date with the latest stable release.
|
||||
|
||||
## Unreleased
|
||||
|
||||
### BREAKING CHANGES:
|
||||
- Moved all methods that were on `Utils` are now individually exported.
|
||||
|
||||
## 1.0.0 Beta 1 (2023-10-19)
|
||||
|
||||
* Add `xrpl-secret-numbers` by @WietseWind to the mono repo.
|
||||
|
||||
@@ -1,8 +1,2 @@
|
||||
/* Methods ==================================================================== */
|
||||
import Account from './schema/Account'
|
||||
import * as Utils from './utils'
|
||||
|
||||
/* Types ==================================================================== */
|
||||
|
||||
/* Export ==================================================================== */
|
||||
export { Account, Utils }
|
||||
export * from './schema/Account'
|
||||
export * from './utils'
|
||||
|
||||
@@ -1,10 +1,14 @@
|
||||
import * as keypairs from 'ripple-keypairs'
|
||||
import { deriveAddress, deriveKeypair, generateSeed } from 'ripple-keypairs'
|
||||
|
||||
import * as utils from '../utils'
|
||||
import {
|
||||
entropyToSecret,
|
||||
parseSecretString,
|
||||
randomSecret,
|
||||
secretToEntropy,
|
||||
} from '../utils'
|
||||
|
||||
/* Types ==================================================================== */
|
||||
|
||||
// eslint-disable-next-line import/no-unused-modules -- it is returned by Account.getKeypair
|
||||
export interface Keypair {
|
||||
publicKey: string
|
||||
privateKey: string
|
||||
@@ -18,7 +22,7 @@ interface AccountData {
|
||||
|
||||
/* Class ==================================================================== */
|
||||
|
||||
export default class Account {
|
||||
export class Account {
|
||||
private readonly _secret: string[]
|
||||
private readonly _account: AccountData = {
|
||||
familySeed: '',
|
||||
@@ -31,13 +35,13 @@ export default class Account {
|
||||
|
||||
constructor(secretNumbers?: string[] | string | Buffer) {
|
||||
if (typeof secretNumbers === 'string') {
|
||||
this._secret = utils.parseSecretString(secretNumbers)
|
||||
this._secret = parseSecretString(secretNumbers)
|
||||
} else if (Array.isArray(secretNumbers)) {
|
||||
this._secret = secretNumbers
|
||||
} else if (Buffer.isBuffer(secretNumbers)) {
|
||||
this._secret = utils.entropyToSecret(secretNumbers)
|
||||
this._secret = entropyToSecret(secretNumbers)
|
||||
} else {
|
||||
this._secret = utils.randomSecret()
|
||||
this._secret = randomSecret()
|
||||
}
|
||||
|
||||
validateLengths(this._secret)
|
||||
@@ -70,12 +74,10 @@ export default class Account {
|
||||
|
||||
private derive(): void {
|
||||
try {
|
||||
const entropy = utils.secretToEntropy(this._secret)
|
||||
this._account.familySeed = keypairs.generateSeed({ entropy })
|
||||
this._account.keypair = keypairs.deriveKeypair(this._account.familySeed)
|
||||
this._account.address = keypairs.deriveAddress(
|
||||
this._account.keypair.publicKey,
|
||||
)
|
||||
const entropy = secretToEntropy(this._secret)
|
||||
this._account.familySeed = generateSeed({ entropy })
|
||||
this._account.keypair = deriveKeypair(this._account.familySeed)
|
||||
this._account.address = deriveAddress(this._account.keypair.publicKey)
|
||||
} catch (error) {
|
||||
let message = 'Unknown Error'
|
||||
if (error instanceof Error) {
|
||||
|
||||
@@ -1,16 +1,16 @@
|
||||
import * as keypairs from 'ripple-keypairs'
|
||||
import { deriveAddress, deriveKeypair, generateSeed } from 'ripple-keypairs'
|
||||
|
||||
import { Account, Utils } from '../src'
|
||||
import { Account, secretToEntropy } from '../src'
|
||||
|
||||
describe('API: XRPL Secret Numbers', () => {
|
||||
describe('Generate new account', () => {
|
||||
const account = new Account()
|
||||
it('Output sanity checks', () => {
|
||||
expect(account.getAddress()).toMatch(/^r[a-zA-Z0-9]{19,}$/u)
|
||||
const entropy = Utils.secretToEntropy(`${account.toString()}`.split(' '))
|
||||
const familySeed = keypairs.generateSeed({ entropy })
|
||||
const keypair = keypairs.deriveKeypair(familySeed)
|
||||
const address = keypairs.deriveAddress(keypair.publicKey)
|
||||
const entropy = secretToEntropy(`${account.toString()}`.split(' '))
|
||||
const familySeed = generateSeed({ entropy })
|
||||
const keypair = deriveKeypair(familySeed)
|
||||
const address = deriveAddress(keypair.publicKey)
|
||||
expect(address).toEqual(account.getAddress())
|
||||
expect(familySeed).toEqual(account.getFamilySeed())
|
||||
})
|
||||
|
||||
@@ -1,8 +1,16 @@
|
||||
import * as utils from '../src/utils'
|
||||
import {
|
||||
calculateChecksum,
|
||||
checkChecksum,
|
||||
entropyToSecret,
|
||||
parseSecretString,
|
||||
randomEntropy,
|
||||
randomSecret,
|
||||
secretToEntropy,
|
||||
} from '../src/utils'
|
||||
|
||||
describe('Utils', () => {
|
||||
it('randomEntropy: valid output', () => {
|
||||
const data = utils.randomEntropy()
|
||||
const data = randomEntropy()
|
||||
expect(typeof data).toEqual('object')
|
||||
expect(data instanceof Buffer).toBeTruthy()
|
||||
expect(data.length).toEqual(16)
|
||||
@@ -11,32 +19,32 @@ describe('Utils', () => {
|
||||
})
|
||||
|
||||
it('calculateChecksum: 1st position', () => {
|
||||
expect(utils.calculateChecksum(0, 55988)).toEqual(8)
|
||||
expect(calculateChecksum(0, 55988)).toEqual(8)
|
||||
})
|
||||
|
||||
it('calculateChecksum: 8th position', () => {
|
||||
expect(utils.calculateChecksum(7, 49962)).toEqual(0)
|
||||
expect(calculateChecksum(7, 49962)).toEqual(0)
|
||||
})
|
||||
|
||||
it('checkChecksum: 2nd position, split numbers', () => {
|
||||
expect(utils.checkChecksum(1, 55450, 3)).toBeTruthy()
|
||||
expect(checkChecksum(1, 55450, 3)).toBeTruthy()
|
||||
})
|
||||
|
||||
it('checkChecksum: 7th position, split numbers', () => {
|
||||
expect(utils.checkChecksum(6, 18373, 7)).toBeTruthy()
|
||||
expect(checkChecksum(6, 18373, 7)).toBeTruthy()
|
||||
})
|
||||
|
||||
it('checkChecksum: 4th position, as string', () => {
|
||||
expect(utils.checkChecksum(3, '391566')).toBeTruthy()
|
||||
expect(checkChecksum(3, '391566')).toBeTruthy()
|
||||
})
|
||||
|
||||
it('randomSecret: valid checksums', () => {
|
||||
utils.randomSecret()
|
||||
randomSecret()
|
||||
expect(0).toEqual(0)
|
||||
})
|
||||
|
||||
it('randomSecret: valid output', () => {
|
||||
const data = utils.randomSecret()
|
||||
const data = randomSecret()
|
||||
expect(Array.isArray(data)).toBeTruthy()
|
||||
expect(data.length).toEqual(8)
|
||||
expect(typeof data[0]).toEqual('string')
|
||||
@@ -56,7 +64,7 @@ describe('Utils', () => {
|
||||
'076618',
|
||||
'024286',
|
||||
]
|
||||
expect(utils.entropyToSecret(entropy)).toEqual(secret)
|
||||
expect(entropyToSecret(entropy)).toEqual(secret)
|
||||
})
|
||||
|
||||
it('secretToEntropy', () => {
|
||||
@@ -71,7 +79,7 @@ describe('Utils', () => {
|
||||
'024286',
|
||||
]
|
||||
const entropy = Buffer.from('76ebb2d06879b45b7568fb9c1ded097c', 'hex')
|
||||
expect(utils.secretToEntropy(secret)).toEqual(entropy)
|
||||
expect(secretToEntropy(secret)).toEqual(entropy)
|
||||
})
|
||||
|
||||
it('parseSecretString with spaces valid', () => {
|
||||
@@ -86,17 +94,15 @@ describe('Utils', () => {
|
||||
'024286',
|
||||
]
|
||||
expect(
|
||||
utils.parseSecretString(
|
||||
parseSecretString(
|
||||
'304435 457766 267453 461717 300560 644127 076618 024286',
|
||||
),
|
||||
).toEqual(secret)
|
||||
expect(
|
||||
utils.parseSecretString(
|
||||
'304435457766267453461717300560644127076618024286',
|
||||
),
|
||||
parseSecretString('304435457766267453461717300560644127076618024286'),
|
||||
).toEqual(secret)
|
||||
expect(
|
||||
utils.parseSecretString(`
|
||||
parseSecretString(`
|
||||
304435 457766
|
||||
267453 461717
|
||||
300560 644127
|
||||
|
||||
Reference in New Issue
Block a user