mirror of
https://github.com/status-im/MyCrypto.git
synced 2025-03-02 19:50:38 +00:00
* add route and nav tab for new module * add new module to tabs index * add signMessage to wallet interface * add signed message verification, normalize pkey sign * init Sign & Verify Message tab * reorder imports * mock out Trezor * cast to bool instead of length check * normalize ledger sign message * fix broken this context * add commented message signing to trezor wallet * correct var to start on sign tab * remove unused state var * clean up SignMessage classes * clean up VerifyMessage classes, remove unnecessary log * correct event variable types * remove unnecessary exports * remove empty classname * use implicit return * shorten signMessage method * remove unnecessary disable * tweak variable name * make better use of destructuring, remove console log * use destructured var * flatten if statement * add signMessage method to wallet reducer test
75 lines
2.0 KiB
TypeScript
75 lines
2.0 KiB
TypeScript
import { randomBytes } from 'crypto';
|
|
import {
|
|
privateToPublic,
|
|
publicToAddress,
|
|
toChecksumAddress
|
|
} from 'ethereumjs-util';
|
|
import { pkeyToKeystore, UtcKeystore } from 'libs/keystore';
|
|
import { signMessageWithPrivKeyV2, signRawTxWithPrivKey } from 'libs/signing';
|
|
import { RawTransaction } from 'libs/transaction';
|
|
import { isValidPrivKey } from 'libs/validators';
|
|
import { stripHexPrefixAndLower } from 'libs/values';
|
|
import { IWallet } from './IWallet';
|
|
|
|
export default class PrivKeyWallet implements IWallet {
|
|
public static generate() {
|
|
return new PrivKeyWallet(randomBytes(32));
|
|
}
|
|
|
|
private privKey: Buffer;
|
|
private pubKey: Buffer;
|
|
private address: Buffer;
|
|
|
|
constructor(privkey: Buffer) {
|
|
if (!isValidPrivKey(privkey)) {
|
|
throw new Error('Invalid private key');
|
|
}
|
|
this.privKey = privkey;
|
|
this.pubKey = privateToPublic(this.privKey);
|
|
this.address = publicToAddress(this.pubKey);
|
|
}
|
|
|
|
public getAddress(): Promise<string> {
|
|
return Promise.resolve(
|
|
toChecksumAddress(`0x${this.address.toString('hex')}`)
|
|
);
|
|
}
|
|
|
|
public getPrivateKey() {
|
|
return this.privKey.toString('hex');
|
|
}
|
|
|
|
public getNakedAddress(): Promise<string> {
|
|
return new Promise(resolve => {
|
|
this.getAddress().then(address => {
|
|
resolve(stripHexPrefixAndLower(address));
|
|
});
|
|
});
|
|
}
|
|
|
|
public toKeystore(password: string): Promise<UtcKeystore> {
|
|
return new Promise(resolve => {
|
|
this.getNakedAddress().then(address => {
|
|
resolve(pkeyToKeystore(this.privKey, address, password));
|
|
});
|
|
});
|
|
}
|
|
|
|
public unlock(): Promise<any> {
|
|
return Promise.resolve();
|
|
}
|
|
|
|
public signRawTransaction(rawTx: RawTransaction): Promise<string> {
|
|
return new Promise((resolve, reject) => {
|
|
try {
|
|
resolve(signRawTxWithPrivKey(this.privKey, rawTx));
|
|
} catch (err) {
|
|
reject(err);
|
|
}
|
|
});
|
|
}
|
|
|
|
public signMessage = async (msg: string) =>
|
|
signMessageWithPrivKeyV2(this.privKey, msg);
|
|
}
|