MyCrypto/common/libs/wallet/privkey.js

38 lines
862 B
JavaScript
Raw Normal View History

2017-06-29 23:03:11 +00:00
// @flow
import BaseWallet from './base';
2017-07-04 03:21:19 +00:00
import {
privateToPublic,
publicToAddress,
toChecksumAddress
} from 'ethereumjs-util';
import { randomBytes } from 'crypto';
import { pkeyToKeystore } from 'libs/keystore';
2017-06-29 23:03:11 +00:00
export default class PrivKeyWallet extends BaseWallet {
2017-07-04 03:21:19 +00:00
privKey: Buffer;
pubKey: Buffer;
address: Buffer;
constructor(privkey: Buffer) {
2017-07-04 03:21:19 +00:00
super();
this.privKey = privkey;
2017-07-04 03:21:19 +00:00
this.pubKey = privateToPublic(this.privKey);
this.address = publicToAddress(this.pubKey);
}
2017-06-29 23:03:11 +00:00
2017-07-04 03:21:19 +00:00
getAddress() {
return toChecksumAddress(`0x${this.address.toString('hex')}`);
}
getPrivateKey() {
return this.privKey.toString('hex');
}
static generate() {
return new PrivKeyWallet(randomBytes(32));
}
toKeystore(password: string): Object {
return pkeyToKeystore(this.privKey, this.getNakedAddress(), password);
}
2017-06-29 23:03:11 +00:00
}