2017-06-29 23:03:11 +00:00
|
|
|
// @flow
|
2017-09-12 00:26:16 +00:00
|
|
|
import type { IWallet } from './IWallet';
|
2017-07-04 03:21:19 +00:00
|
|
|
import {
|
|
|
|
privateToPublic,
|
|
|
|
publicToAddress,
|
|
|
|
toChecksumAddress
|
|
|
|
} from 'ethereumjs-util';
|
2017-07-16 21:02:13 +00:00
|
|
|
import { randomBytes } from 'crypto';
|
|
|
|
import { pkeyToKeystore } from 'libs/keystore';
|
2017-08-08 03:25:23 +00:00
|
|
|
import { signRawTxWithPrivKey, signMessageWithPrivKey } from 'libs/signing';
|
2017-08-20 20:28:47 +00:00
|
|
|
import { isValidPrivKey } from 'libs/validators';
|
2017-08-11 21:54:10 +00:00
|
|
|
import type { RawTransaction } from 'libs/transaction';
|
2017-08-24 08:34:08 +00:00
|
|
|
import type { UtcKeystore } from 'libs/keystore';
|
2017-09-15 19:29:38 +00:00
|
|
|
import { stripHexPrefixAndLower } from 'libs/values';
|
2017-06-29 23:03:11 +00:00
|
|
|
|
2017-09-12 00:26:16 +00:00
|
|
|
export default class PrivKeyWallet implements IWallet {
|
2017-07-04 03:21:19 +00:00
|
|
|
privKey: Buffer;
|
|
|
|
pubKey: Buffer;
|
|
|
|
address: Buffer;
|
2017-07-16 21:02:13 +00:00
|
|
|
constructor(privkey: Buffer) {
|
2017-08-20 20:28:47 +00:00
|
|
|
if (!isValidPrivKey(privkey)) {
|
|
|
|
throw new Error('Invalid private key');
|
|
|
|
}
|
2017-07-16 21:02:13 +00:00
|
|
|
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-08-11 21:54:10 +00:00
|
|
|
getAddress(): Promise<string> {
|
2017-08-09 12:01:34 +00:00
|
|
|
return Promise.resolve(
|
|
|
|
toChecksumAddress(`0x${this.address.toString('hex')}`)
|
|
|
|
);
|
2017-07-04 03:21:19 +00:00
|
|
|
}
|
2017-07-16 21:02:13 +00:00
|
|
|
|
|
|
|
getPrivateKey() {
|
|
|
|
return this.privKey.toString('hex');
|
|
|
|
}
|
|
|
|
|
|
|
|
static generate() {
|
|
|
|
return new PrivKeyWallet(randomBytes(32));
|
|
|
|
}
|
|
|
|
|
2017-09-12 00:26:16 +00:00
|
|
|
getNakedAddress(): Promise<string> {
|
|
|
|
return new Promise(resolve => {
|
|
|
|
this.getAddress().then(address => {
|
2017-09-15 19:29:38 +00:00
|
|
|
resolve(stripHexPrefixAndLower(address));
|
2017-09-12 00:26:16 +00:00
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2017-08-24 08:34:08 +00:00
|
|
|
toKeystore(password: string): Promise<UtcKeystore> {
|
2017-08-08 03:25:23 +00:00
|
|
|
return new Promise(resolve => {
|
|
|
|
this.getNakedAddress().then(address => {
|
|
|
|
resolve(pkeyToKeystore(this.privKey, address, password));
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
unlock(): Promise<any> {
|
|
|
|
return Promise.resolve();
|
|
|
|
}
|
|
|
|
|
2017-08-11 21:54:10 +00:00
|
|
|
signRawTransaction(rawTx: RawTransaction): Promise<string> {
|
2017-08-08 03:25:23 +00:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
});
|
2017-07-16 21:02:13 +00:00
|
|
|
}
|
2017-06-29 23:03:11 +00:00
|
|
|
}
|