MyCrypto/common/libs/wallet/privkey.js
skubakdj f42837de68 Keystore & Private Key Wallet Decrypts (#116)
* wire up keystore decrypt & build UI

* add support for encrypted private keys

* add check for key length

* rename keystore wallet file

* rename encrypted priv key wallet file

* add support for presale, v1, & v2 JSON keystores

* clean up TODO messages, add class files

* add v3 references

* add flow type

* fix event bug

* update privkey validators to accept whole privkey

* refactor pkey/pass validation to function

* move pass req detection to function, remove unnecessary state

* add tests for decrypt & keystore libs
2017-08-20 22:28:47 +02:00

75 lines
1.8 KiB
JavaScript

// @flow
import BaseWallet from './base';
import {
privateToPublic,
publicToAddress,
toChecksumAddress
} from 'ethereumjs-util';
import { randomBytes } from 'crypto';
import { pkeyToKeystore } from 'libs/keystore';
import { signRawTxWithPrivKey, signMessageWithPrivKey } from 'libs/signing';
import { isValidPrivKey } from 'libs/validators';
import type { RawTransaction } from 'libs/transaction';
export default class PrivKeyWallet extends BaseWallet {
privKey: Buffer;
pubKey: Buffer;
address: Buffer;
constructor(privkey: Buffer) {
if (!isValidPrivKey(privkey)) {
throw new Error('Invalid private key');
}
super();
this.privKey = privkey;
this.pubKey = privateToPublic(this.privKey);
this.address = publicToAddress(this.pubKey);
}
getAddress(): Promise<string> {
return Promise.resolve(
toChecksumAddress(`0x${this.address.toString('hex')}`)
);
}
getPrivateKey() {
return this.privKey.toString('hex');
}
static generate() {
return new PrivKeyWallet(randomBytes(32));
}
toKeystore(password: string): Promise<any> {
return new Promise(resolve => {
this.getNakedAddress().then(address => {
resolve(pkeyToKeystore(this.privKey, address, password));
});
});
}
unlock(): Promise<any> {
return Promise.resolve();
}
signRawTransaction(rawTx: RawTransaction): Promise<string> {
return new Promise((resolve, reject) => {
try {
resolve(signRawTxWithPrivKey(this.privKey, rawTx));
} catch (err) {
reject(err);
}
});
}
signMessage(msg: string, address: string, date: string): Promise<any> {
return new Promise((resolve, reject) => {
try {
resolve(signMessageWithPrivKey(this.privKey, msg, address, date));
} catch (err) {
reject(err);
}
});
}
}