Skip to content

Commit

Permalink
Removed UUID dependency from json-wallets (#966).
Browse files Browse the repository at this point in the history
  • Loading branch information
ricmoo committed Jul 20, 2020
1 parent 8b907d5 commit e3f7426
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 13 deletions.
3 changes: 1 addition & 2 deletions packages/json-wallets/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,7 @@
"@ethersproject/strings": "^5.0.0",
"@ethersproject/transactions": "^5.0.0",
"aes-js": "3.0.0",
"scrypt-js": "3.0.1",
"uuid": "2.0.1"
"scrypt-js": "3.0.1"
},
"description": "Wallet management utilities for KeyStore and Crowdsale JSON wallets.",
"ethereum": "donations.ethers.eth",
Expand Down
5 changes: 2 additions & 3 deletions packages/json-wallets/src.ts/keystore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

import aes from "aes-js";
import * as scrypt from "scrypt-js";
import uuid from "uuid";

import { ExternallyOwnedAccount } from "@ethersproject/abstract-signer";
import { getAddress } from "@ethersproject/address";
Expand All @@ -14,7 +13,7 @@ import { randomBytes } from "@ethersproject/random";
import { Description } from "@ethersproject/properties";
import { computeAddress } from "@ethersproject/transactions";

import { getPassword, looseArrayify, searchPath, zpad } from "./utils";
import { getPassword, looseArrayify, searchPath, uuidV4, zpad } from "./utils";

import { Logger } from "@ethersproject/logger";
import { version } from "./_version";
Expand Down Expand Up @@ -328,7 +327,7 @@ export function encrypt(account: ExternallyOwnedAccount, password: Bytes | strin
// See: https://github.com/ethereum/wiki/wiki/Web3-Secret-Storage-Definition
const data: { [key: string]: any } = {
address: account.address.substring(2).toLowerCase(),
id: uuid.v4({ random: uuidRandom }),
id: uuidV4(uuidRandom),
version: 3,
Crypto: {
cipher: "aes-128-ctr",
Expand Down
27 changes: 26 additions & 1 deletion packages/json-wallets/src.ts/utils.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
"use strict";

import { arrayify, Bytes } from "@ethersproject/bytes";
import { arrayify, Bytes, BytesLike, hexlify } from "@ethersproject/bytes";
import { toUtf8Bytes, UnicodeNormalizationForm } from '@ethersproject/strings';

export function looseArrayify(hexString: string): Uint8Array {
Expand Down Expand Up @@ -49,3 +49,28 @@ export function searchPath(object: any, path: string): string {

return currentChild;
}

// See: https://www.ietf.org/rfc/rfc4122.txt (Section 4.4)
export function uuidV4(randomBytes: BytesLike): string {
const bytes = arrayify(randomBytes);

// Section: 4.1.3:
// - time_hi_and_version[12:16] = 0b0100
bytes[6] = (bytes[6] & 0x0f) | 0x40;

// Section 4.4
// - clock_seq_hi_and_reserved[6] = 0b0
// - clock_seq_hi_and_reserved[7] = 0b1
bytes[8] = (bytes[8] & 0x3f) | 0x80;

const value = hexlify(bytes);

return [
value.substring(2, 10),
value.substring(10, 14),
value.substring(14, 18),
value.substring(18, 22),
value.substring(22, 34),
].join("-");
}

7 changes: 0 additions & 7 deletions packages/json-wallets/thirdparty.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,3 @@ declare module "aes-js" {
}
}
}

declare module "uuid" {
export type Options = {
random: Uint8Array;
};
export function v4(options?: Options): string;
}

0 comments on commit e3f7426

Please sign in to comment.