Skip to content

Commit

Permalink
addr to address
Browse files Browse the repository at this point in the history
  • Loading branch information
lovelycs committed Sep 19, 2019
1 parent 25ed637 commit d911f87
Show file tree
Hide file tree
Showing 36 changed files with 160 additions and 147 deletions.
8 changes: 4 additions & 4 deletions src/HTTP/type.ts
Original file line number Diff line number Diff line change
Expand Up @@ -253,10 +253,10 @@ export declare type changeTransferOwnerBlock = {
}

export declare type AddrObj = {
addr: string;
pubKey: Hex;
privKey: Hex;
hexAddr: Address;
realAddress: string;
publicKey: Hex;
privateKey: Hex;
address: Address;
}

export enum BlockType {
Expand Down
8 changes: 4 additions & 4 deletions src/IPC/type.ts
Original file line number Diff line number Diff line change
Expand Up @@ -253,10 +253,10 @@ export declare type changeTransferOwnerBlock = {
}

export declare type AddrObj = {
addr: string;
pubKey: Hex;
privKey: Hex;
hexAddr: Address;
realAddress: string;
publicKey: Hex;
privateKey: Hex;
address: Address;
}

export enum BlockType {
Expand Down
8 changes: 4 additions & 4 deletions src/WS/type.ts
Original file line number Diff line number Diff line change
Expand Up @@ -253,10 +253,10 @@ export declare type changeTransferOwnerBlock = {
}

export declare type AddrObj = {
addr: string;
pubKey: Hex;
privKey: Hex;
hexAddr: Address;
realAddress: string;
publicKey: Hex;
privateKey: Hex;
address: Address;
}

export enum BlockType {
Expand Down
8 changes: 4 additions & 4 deletions src/abi/type.ts
Original file line number Diff line number Diff line change
Expand Up @@ -253,10 +253,10 @@ export declare type changeTransferOwnerBlock = {
}

export declare type AddrObj = {
addr: string;
pubKey: Hex;
privKey: Hex;
hexAddr: Address;
realAddress: string;
publicKey: Hex;
privateKey: Hex;
address: Address;
}

export enum BlockType {
Expand Down
18 changes: 9 additions & 9 deletions src/account/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,15 +35,15 @@ class AccountClass extends addrAccount {
this.privateKey = null;
this.publicKey = null;
} else {
const { pubKey, privKey, hexAddr } = privToAddr.createAddressByPrivateKey(privateKey);
const addrObj = privToAddr.createAddressByPrivateKey(privateKey);

if (privateKey && address && hexAddr !== address) {
if (privateKey && address && addrObj.address !== address) {
throw new Error(`Private key does not match address ${ address }`);
}

super({ address: hexAddr, client });
this.privateKey = privKey;
this.publicKey = pubKey;
super({ address: addrObj.address, client });
this.privateKey = addrObj.privateKey;
this.publicKey = addrObj.publicKey;
}

this._lock = true;
Expand All @@ -66,13 +66,13 @@ class AccountClass extends addrAccount {
return;
}

const { pubKey, privKey, hexAddr } = privToAddr.createAddressByPrivateKey(privateKey);
if (hexAddr !== this.address) {
const addrObj = privToAddr.createAddressByPrivateKey(privateKey);
if (addrObj.address !== this.address) {
throw new Error(`Private key does not match address ${ this.address }`);
}

this.privateKey = privKey;
this.publicKey = pubKey;
this.privateKey = addrObj.privateKey;
this.publicKey = addrObj.publicKey;
}

getPublicKey() {
Expand Down
8 changes: 4 additions & 4 deletions src/account/type.ts
Original file line number Diff line number Diff line change
Expand Up @@ -253,10 +253,10 @@ export declare type changeTransferOwnerBlock = {
}

export declare type AddrObj = {
addr: string;
pubKey: Hex;
privKey: Hex;
hexAddr: Address;
realAddress: string;
publicKey: Hex;
privateKey: Hex;
address: Address;
}

export enum BlockType {
Expand Down
8 changes: 4 additions & 4 deletions src/accountBlock/type.ts
Original file line number Diff line number Diff line change
Expand Up @@ -253,10 +253,10 @@ export declare type changeTransferOwnerBlock = {
}

export declare type AddrObj = {
addr: string;
pubKey: Hex;
privKey: Hex;
hexAddr: Address;
realAddress: string;
publicKey: Hex;
privateKey: Hex;
address: Address;
}

export enum BlockType {
Expand Down
8 changes: 4 additions & 4 deletions src/addrAccount/type.ts
Original file line number Diff line number Diff line change
Expand Up @@ -253,10 +253,10 @@ export declare type changeTransferOwnerBlock = {
}

export declare type AddrObj = {
addr: string;
pubKey: Hex;
privKey: Hex;
hexAddr: Address;
realAddress: string;
publicKey: Hex;
privateKey: Hex;
address: Address;
}

export enum BlockType {
Expand Down
Empty file added src/address/mnemonic.ts
Empty file.
8 changes: 4 additions & 4 deletions src/address/type.ts
Original file line number Diff line number Diff line change
Expand Up @@ -253,10 +253,10 @@ export declare type changeTransferOwnerBlock = {
}

export declare type AddrObj = {
addr: string;
pubKey: Hex;
privKey: Hex;
hexAddr: Address;
realAddress: string;
publicKey: Hex;
privateKey: Hex;
address: Address;
}

export enum BlockType {
Expand Down
8 changes: 4 additions & 4 deletions src/client/type.ts
Original file line number Diff line number Diff line change
Expand Up @@ -253,10 +253,10 @@ export declare type changeTransferOwnerBlock = {
}

export declare type AddrObj = {
addr: string;
pubKey: Hex;
privKey: Hex;
hexAddr: Address;
realAddress: string;
publicKey: Hex;
privateKey: Hex;
address: Address;
}

export enum BlockType {
Expand Down
8 changes: 4 additions & 4 deletions src/communication/type.ts
Original file line number Diff line number Diff line change
Expand Up @@ -253,10 +253,10 @@ export declare type changeTransferOwnerBlock = {
}

export declare type AddrObj = {
addr: string;
pubKey: Hex;
privKey: Hex;
hexAddr: Address;
realAddress: string;
publicKey: Hex;
privateKey: Hex;
address: Address;
}

export enum BlockType {
Expand Down
8 changes: 4 additions & 4 deletions src/constant/type.ts
Original file line number Diff line number Diff line change
Expand Up @@ -253,10 +253,10 @@ export declare type changeTransferOwnerBlock = {
}

export declare type AddrObj = {
addr: string;
pubKey: Hex;
privKey: Hex;
hexAddr: Address;
realAddress: string;
publicKey: Hex;
privateKey: Hex;
address: Address;
}

export enum BlockType {
Expand Down
8 changes: 4 additions & 4 deletions src/error/type.ts
Original file line number Diff line number Diff line change
Expand Up @@ -253,10 +253,10 @@ export declare type changeTransferOwnerBlock = {
}

export declare type AddrObj = {
addr: string;
pubKey: Hex;
privKey: Hex;
hexAddr: Address;
realAddress: string;
publicKey: Hex;
privateKey: Hex;
address: Address;
}

export enum BlockType {
Expand Down
6 changes: 3 additions & 3 deletions src/hdAccount/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ class HdAccountClass {
let i;
for (i = 0; i < this.activeAccountList.length; i++) {
const account = this.activeAccountList[i];
if (account.address === addrObj.hexAddr) {
if (account.address === addrObj.address) {
break;
}
}
Expand All @@ -148,7 +148,7 @@ class HdAccountClass {
}

return new Account({
privateKey: addrObj.privKey,
privateKey: addrObj.privateKey,
client: this._client
}, { autoPow, usePledgeQuota });
}
Expand Down Expand Up @@ -186,7 +186,7 @@ class HdAccountClass {

let i;
for (i = 0; i < this.addrList.length; i++) {
if (this.addrList[i].hexAddr === address) {
if (this.addrList[i].address === address) {
break;
}
}
Expand Down
8 changes: 4 additions & 4 deletions src/hdAccount/type.ts
Original file line number Diff line number Diff line change
Expand Up @@ -253,10 +253,10 @@ export declare type changeTransferOwnerBlock = {
}

export declare type AddrObj = {
addr: string;
pubKey: Hex;
privKey: Hex;
hexAddr: Address;
realAddress: string;
publicKey: Hex;
privateKey: Hex;
address: Address;
}

export enum BlockType {
Expand Down
35 changes: 24 additions & 11 deletions src/hdAddr/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ const bip39 = require('bip39');
const hd = require('@sisi/ed25519-blake2b-hd-key');
import { createAddressByPrivateKey, isAddress as _isAddress, getRealAddressFromAddress as _getRealAddressFromAddress } from '~@vite/vitejs-privtoaddr';
import { paramsFormat } from '~@vite/vitejs-error';
import { checkParams, bytesToHex, blake2b } from '~@vite/vitejs-utils';
import { checkParams, blake2b } from '~@vite/vitejs-utils';

import { AddrObj, Hex } from './type';

Expand Down Expand Up @@ -36,9 +36,7 @@ export function getMnemonicFromEntropy(entropy: string, wordlist: Array<String>)
return bip39.entropyToMnemonic(entropy, wordlist);
}

export function createAddress(bits: number = 256, wordlist: Array<String>, pwd: string = '', isContract?: boolean): {
addr: AddrObj; entropy: string; mnemonic: string; id: Hex;
} {
export function createMnemonic(bits: number = 256, wordlist: Array<String>, pwd: string = '') {
const err = checkParams({ bits }, ['bits']);
if (err) {
throw new Error(err.message);
Expand All @@ -48,6 +46,22 @@ export function createAddress(bits: number = 256, wordlist: Array<String>, pwd:
const mnemonic = bip39.generateMnemonic(bits, null, wordlist);
const entropy = bip39.mnemonicToEntropy(mnemonic, wordlist);
const seed = bip39.mnemonicToSeedSync(mnemonic, pwd).toString('hex');

return { mnemonic, entropy, seed };
}

export function deriveKeyPair(seed, index: number) {
const path = getPath(index);
const { key } = hd.derivePath(path, seed);
return hd.getPublicKey(key);
}

export function createAddress(bits: number = 256, wordlist: Array<String>, pwd: string = '', isContract?: boolean): {
addr: AddrObj; entropy: string; mnemonic: string; id: Hex;
} {
wordlist = wordlist || bip39.wordlists.EN;
const { mnemonic, entropy, seed } = createMnemonic(bits, wordlist, pwd);

const path = getPath(0);
const addr = getAddrFromPath(path, seed, isContract);
const id = getId(mnemonic, wordlist);
Expand Down Expand Up @@ -113,15 +127,14 @@ export const getRealAddressFromAddress = _getRealAddressFromAddress;
export const isAddress = _isAddress;


function getPath(index: number): string {
return `${ ROOT_PATH }/${ index }\'`;
}

function getAddrFromPath(path: string, seed: string, isContract?: boolean): AddrObj {
const { key } = hd.derivePath(path, seed);
const { privateKey } = hd.getPublicKey(key);
const priv = bytesToHex(privateKey);
return createAddressByPrivateKey(priv, isContract);
}

function getPath(index: number): string {
return `${ ROOT_PATH }/${ index }\'`;
return createAddressByPrivateKey(privateKey, isContract);
}

function _getId(mnemonic: string, wordlist: Array<String>, pwd: string = '', isContract?: boolean): Hex {
Expand All @@ -136,7 +149,7 @@ function _getId(mnemonic: string, wordlist: Array<String>, pwd: string = '', isC
}

const addrObj = getAddrFromMnemonic(mnemonic, 0, wordlist, pwd, isContract);
const keyBuffer = Buffer.from(addrObj.hexAddr);
const keyBuffer = Buffer.from(addrObj.address);
const idByte = blake2b(keyBuffer, null, 32);
return Buffer.from(idByte).toString('hex');
}
8 changes: 4 additions & 4 deletions src/hdAddr/type.ts
Original file line number Diff line number Diff line change
Expand Up @@ -253,10 +253,10 @@ export declare type changeTransferOwnerBlock = {
}

export declare type AddrObj = {
addr: string;
pubKey: Hex;
privKey: Hex;
hexAddr: Address;
realAddress: string;
publicKey: Hex;
privateKey: Hex;
address: Address;
}

export enum BlockType {
Expand Down
6 changes: 3 additions & 3 deletions src/keystore/encrypt.ts
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ export function encryptOldKeystore(privKey, pwd, selfScryptsy) {
throw new Error(err.message);
}

const key = createAddressByPrivateKey(privKey);
const addrObj = createAddressByPrivateKey(privKey);

const scryptParams = {
n,
Expand All @@ -92,7 +92,7 @@ export function encryptOldKeystore(privKey, pwd, selfScryptsy) {
const getResult = (_encryptPwd, res) => {
const nonce = random(12);
const text = cipheriv({
rawText: key.privKey,
rawText: addrObj.privateKey,
pwd: _encryptPwd,
nonce,
algorithm
Expand All @@ -107,7 +107,7 @@ export function encryptOldKeystore(privKey, pwd, selfScryptsy) {
};

const encryptedKeyJSON = {
hexAddress: key.hexAddr,
hexAddress: addrObj.address,
crypto: cryptoJSON,
id: new UUID(1).format(),
keystoreVersion: 1,
Expand Down
8 changes: 4 additions & 4 deletions src/keystore/type.ts
Original file line number Diff line number Diff line change
Expand Up @@ -253,10 +253,10 @@ export declare type changeTransferOwnerBlock = {
}

export declare type AddrObj = {
addr: string;
pubKey: Hex;
privKey: Hex;
hexAddr: Address;
realAddress: string;
publicKey: Hex;
privateKey: Hex;
address: Address;
}

export enum BlockType {
Expand Down

0 comments on commit d911f87

Please sign in to comment.