Skip to content

Commit

Permalink
chore(lint): apply prettier to secret-numbers (#2549)
Browse files Browse the repository at this point in the history
The config was not applied when it was moved to the monorepo
  • Loading branch information
ckniffen committed Nov 1, 2023
1 parent bf3dc85 commit 8a58d12
Show file tree
Hide file tree
Showing 7 changed files with 244 additions and 247 deletions.
14 changes: 7 additions & 7 deletions packages/secret-numbers/HISTORY.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,12 @@ Subscribe to [the **xrpl-announce** mailing list](https://groups.google.com/g/xr

## 1.0.0 Beta 1 (2023-10-19)

- Add `xrpl-secret-numbers` by @WietseWind to the mono repo.
- `unpkg` and `jsdelivr` support was simplified.
- Unit tests run in a browser and node.
- Remove `brorand` as a dependency and use `@xrplf/isomorphic` instead.
* Add `xrpl-secret-numbers` by @WietseWind to the mono repo.
* `unpkg` and `jsdelivr` support was simplified.
* Unit tests run in a browser and node.
* Remove `brorand` as a dependency and use `@xrplf/isomorphic` instead.

### BREAKING CHANGES:
- `xrpl-secret-numbers` is now `@xrplf/secret-numbers`.
- The bundled file produced changed from `dist/browerified.js` to `build/xrplf-secret-numbers-latest.js`.
- Bundle variable is `xrplf_secret_numbers` instead of using browserify's loader.
* `xrpl-secret-numbers` is now `@xrplf/secret-numbers`.
* The bundled file produced changed from `dist/browerified.js` to `build/xrplf-secret-numbers-latest.js`.
* Bundle variable is `xrplf_secret_numbers` instead of using browserify's loader.
1 change: 1 addition & 0 deletions packages/secret-numbers/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
"@xrplf/isomorphic": "^1.0.0-beta.0",
"ripple-keypairs": "^2.0.0-beta.0"
},
"prettier": "@xrplf/prettier-config",
"repository": {
"type": "git",
"url": "[email protected]:XRPLF/xrpl.js.git"
Expand Down
6 changes: 3 additions & 3 deletions packages/secret-numbers/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
/* Methods ==================================================================== */
import Account from "./schema/Account";
import * as Utils from "./utils";
import Account from './schema/Account'
import * as Utils from './utils'

/* Types ==================================================================== */

/* Export ==================================================================== */
export { Account, Utils };
export { Account, Utils }
74 changes: 37 additions & 37 deletions packages/secret-numbers/src/schema/Account.ts
Original file line number Diff line number Diff line change
@@ -1,99 +1,99 @@
import * as keypairs from "ripple-keypairs";
import * as keypairs from 'ripple-keypairs'

import * as utils from "../utils";
import * as utils from '../utils'

/* Types ==================================================================== */

// eslint-disable-next-line import/no-unused-modules -- it is returned by Account.getKeypair
export interface Keypair {
publicKey: string;
privateKey: string;
publicKey: string
privateKey: string
}

interface AccountData {
familySeed: string;
address: string;
keypair: Keypair;
familySeed: string
address: string
keypair: Keypair
}

/* Class ==================================================================== */

export default class Account {
private readonly _secret: string[];
private readonly _secret: string[]
private readonly _account: AccountData = {
familySeed: "",
address: "",
familySeed: '',
address: '',
keypair: {
publicKey: "",
privateKey: "",
publicKey: '',
privateKey: '',
},
};
}

constructor(secretNumbers?: string[] | string | Buffer) {
if (typeof secretNumbers === "string") {
this._secret = utils.parseSecretString(secretNumbers);
if (typeof secretNumbers === 'string') {
this._secret = utils.parseSecretString(secretNumbers)
} else if (Array.isArray(secretNumbers)) {
this._secret = secretNumbers;
this._secret = secretNumbers
} else if (Buffer.isBuffer(secretNumbers)) {
this._secret = utils.entropyToSecret(secretNumbers);
this._secret = utils.entropyToSecret(secretNumbers)
} else {
this._secret = utils.randomSecret();
this._secret = utils.randomSecret()
}

validateLengths(this._secret);
this.derive();
validateLengths(this._secret)
this.derive()
}

getSecret(): string[] {
return this._secret;
return this._secret
}

getSecretString(): string {
return this._secret.join(" ");
return this._secret.join(' ')
}

getAddress(): string {
return this._account.address;
return this._account.address
}

getFamilySeed(): string {
return this._account.familySeed;
return this._account.familySeed
}

getKeypair(): Keypair {
return this._account.keypair;
return this._account.keypair
}

toString(): string {
return this.getSecretString();
return this.getSecretString()
}

private derive(): void {
try {
const entropy = utils.secretToEntropy(this._secret);
this._account.familySeed = keypairs.generateSeed({ entropy });
this._account.keypair = keypairs.deriveKeypair(this._account.familySeed);
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
);
this._account.keypair.publicKey,
)
} catch (error) {
let message = "Unknown Error";
let message = 'Unknown Error'
if (error instanceof Error) {
message = error.message;
message = error.message
}
// we'll proceed, but let's report it
throw new Error(message);
throw new Error(message)
}
}
}

function validateLengths(secretNumbers: string[]): void {
if (secretNumbers.length !== 8) {
throw new Error("Secret must have 8 numbers");
throw new Error('Secret must have 8 numbers')
}
secretNumbers.forEach((num) => {
if (num.length !== 6) {
throw new Error("Each secret number must be 6 digits");
throw new Error('Each secret number must be 6 digits')
}
});
})
}
76 changes: 38 additions & 38 deletions packages/secret-numbers/src/utils/index.ts
Original file line number Diff line number Diff line change
@@ -1,82 +1,82 @@
import { randomBytes } from "@xrplf/isomorphic/utils";
import { randomBytes } from '@xrplf/isomorphic/utils'

function randomEntropy(): Buffer {
return Buffer.from(randomBytes(16));
return Buffer.from(randomBytes(16))
}

function calculateChecksum(position: number, value: number): number {
return (value * (position * 2 + 1)) % 9;
return (value * (position * 2 + 1)) % 9
}

function checkChecksum(
position: number,
value: number | string,
checksum?: number
checksum?: number,
): boolean {
let normalizedChecksum: number;
let normalizedValue: number;
let normalizedChecksum: number
let normalizedValue: number

if (typeof value === "string") {
if (typeof value === 'string') {
if (value.length !== 6) {
throw new Error("value must have a length of 6");
throw new Error('value must have a length of 6')
}
normalizedChecksum = parseInt(value.slice(5), 10);
normalizedValue = parseInt(value.slice(0, 5), 10);
normalizedChecksum = parseInt(value.slice(5), 10)
normalizedValue = parseInt(value.slice(0, 5), 10)
} else {
if (typeof checksum !== "number") {
throw new Error("checksum must be a number when value is a number");
if (typeof checksum !== 'number') {
throw new Error('checksum must be a number when value is a number')
}
normalizedChecksum = checksum;
normalizedValue = value;
normalizedChecksum = checksum
normalizedValue = value
}
return (normalizedValue * (position * 2 + 1)) % 9 === normalizedChecksum;
return (normalizedValue * (position * 2 + 1)) % 9 === normalizedChecksum
}

function entropyToSecret(entropy: Buffer): string[] {
const len = new Array(Math.ceil(entropy.length / 2));
const len = new Array(Math.ceil(entropy.length / 2))
const chunks = Array.from(len, (_a, chunk) => {
const buffChunk = entropy.slice(chunk * 2, (chunk + 1) * 2);
const no = parseInt(buffChunk.toString("hex"), 16);
const fill = "0".repeat(5 - String(no).length);
return fill + String(no) + String(calculateChecksum(chunk, no));
});
const buffChunk = entropy.slice(chunk * 2, (chunk + 1) * 2)
const no = parseInt(buffChunk.toString('hex'), 16)
const fill = '0'.repeat(5 - String(no).length)
return fill + String(no) + String(calculateChecksum(chunk, no))
})
if (chunks.length !== 8) {
throw new Error("Chucks must have 8 digits");
throw new Error('Chucks must have 8 digits')
}
return chunks;
return chunks
}

function randomSecret(): string[] {
return entropyToSecret(randomEntropy());
return entropyToSecret(randomEntropy())
}

function secretToEntropy(secret: string[]): Buffer {
return Buffer.concat(
secret.map((chunk, i) => {
const no = Number(chunk.slice(0, 5));
const checksum = Number(chunk.slice(5));
const no = Number(chunk.slice(0, 5))
const checksum = Number(chunk.slice(5))
if (chunk.length !== 6) {
throw new Error("Invalid secret: number invalid");
throw new Error('Invalid secret: number invalid')
}
if (!checkChecksum(i, no, checksum)) {
throw new Error("Invalid secret part: checksum invalid");
throw new Error('Invalid secret part: checksum invalid')
}
const hex = `0000${no.toString(16)}`.slice(-4);
return Buffer.from(hex, "hex");
})
);
const hex = `0000${no.toString(16)}`.slice(-4)
return Buffer.from(hex, 'hex')
}),
)
}

function parseSecretString(secret: string): string[] {
const normalizedSecret = secret.replace(/[^0-9]/gu, "");
const normalizedSecret = secret.replace(/[^0-9]/gu, '')
if (normalizedSecret.length !== 48) {
throw new Error(
"Invalid secret string (should contain 8 blocks of 6 digits"
);
'Invalid secret string (should contain 8 blocks of 6 digits',
)
}
return Array.from(new Array(8), (_a, index) => {
return normalizedSecret.slice(index * 6, (index + 1) * 6);
});
return normalizedSecret.slice(index * 6, (index + 1) * 6)
})
}

export {
Expand All @@ -87,4 +87,4 @@ export {
calculateChecksum,
checkChecksum,
parseSecretString,
};
}
Loading

0 comments on commit 8a58d12

Please sign in to comment.