ethers.js/packages/json-wallets/lib.esm/utils.js

58 lines
1.7 KiB
JavaScript
Raw Normal View History

2019-05-15 01:48:48 +03:00
"use strict";
import { arrayify } from "@ethersproject/bytes";
2019-05-15 01:48:48 +03:00
//import { Description } from "@ethersproject/properties";
import { toUtf8Bytes, UnicodeNormalizationForm } from '@ethersproject/strings';
2019-05-15 01:48:48 +03:00
/*
export class Account extends Description implements ExternallyOwnedAccount {
readonly address: string;
readonly privateKey: string;
readonly mnemonic?: string;
readonly path?: string;
// static isAccount(value: any): value is Account {
// return Description._isType(value);
// }
}
//defineReadOnly(Account, "name", "Account");
*/
export function looseArrayify(hexString) {
2019-05-15 01:48:48 +03:00
if (typeof (hexString) === 'string' && hexString.substring(0, 2) !== '0x') {
hexString = '0x' + hexString;
}
return arrayify(hexString);
2019-05-15 01:48:48 +03:00
}
export function zpad(value, length) {
2019-05-15 01:48:48 +03:00
value = String(value);
while (value.length < length) {
value = '0' + value;
}
return value;
}
export function getPassword(password) {
2019-05-15 01:48:48 +03:00
if (typeof (password) === 'string') {
return toUtf8Bytes(password, UnicodeNormalizationForm.NFKC);
2019-05-15 01:48:48 +03:00
}
return arrayify(password);
2019-05-15 01:48:48 +03:00
}
export function searchPath(object, path) {
let currentChild = object;
let comps = path.toLowerCase().split('/');
for (let i = 0; i < comps.length; i++) {
2019-05-15 01:48:48 +03:00
// Search for a child object with a case-insensitive matching key
let matchingChild = null;
for (let key in currentChild) {
2019-05-15 01:48:48 +03:00
if (key.toLowerCase() === comps[i]) {
matchingChild = currentChild[key];
break;
}
}
// Didn't find one. :'(
if (matchingChild === null) {
return null;
}
// Now check this child...
currentChild = matchingChild;
}
return currentChild;
}