web3: fixes for web3.eth.accounts types and methods, added missing methods (#29495)

* web3: removed `publicKey` field from Account

* web3: split sign/signTransaction return values to specific interfaces

* web3: `Account.publicKey` removal regression test

* web3: Account.sign & Account.signTransaction regression tests

* web3: fixed typo in web3.eth.accounts.encrypt and added regression test

* web3: added sign(), signTransaction() and encrypt() methods on `Account`

* web3: make linter happy
This commit is contained in:
Dmitry Radkovskiy
2018-10-08 20:07:01 +03:00
committed by Andy
parent 61a1219980
commit d6950287e2
2 changed files with 52 additions and 11 deletions

View File

@@ -3,20 +3,31 @@ import { Tx } from "./types";
export interface Account {
address: string;
privateKey: string;
publicKey: string;
sign(data: string): MessageSignature;
signTransaction(
tx: Tx,
cb?: (err: Error, result: TxSignature) => void
): Promise<TxSignature>;
encrypt(password: string, options?: any): PrivateKey;
}
export interface Signature {
message: string;
hash: string;
messageHash: string;
r: string;
s: string;
v: string;
}
export interface MessageSignature extends Signature {
message: string;
signature: string;
}
export interface TxSignature extends Signature {
rawTransaction: string;
}
export interface PrivateKey {
address: string;
Crypto: {
crypto: {
cipher: string;
ciphertext: string;
cipherparams: {
@@ -43,15 +54,13 @@ export default interface Accounts {
signTransaction(
tx: Tx,
privateKey: string,
returnSignature?: boolean,
cb?: (err: Error, result: string | Signature) => void
): Promise<string> | Signature;
recoverTransaction(signature: string | Signature): string;
cb?: (err: Error, result: TxSignature) => void
): Promise<TxSignature>;
recoverTransaction(signature: string): string;
sign(
data: string,
privateKey: string,
returnSignature?: boolean
): string | Signature;
privateKey: string
): MessageSignature;
recover(
sigOrHash: string | Signature,
sigOrV?: string,

View File

@@ -39,6 +39,38 @@ myContract.options.gas = 5000000;
//
// web3.eth.accounts
// --------------------------------------------------------------------------
const account = web3.eth.accounts.privateKeyToAccount("0x1234");
// check that no `publicKey` field is present on `Account` type
const noPublicKeyInAccount: typeof account & { publicKey?: never } = account;
const testTx = {
to: '0xF0109fC8DF283027b6285cc889F5aA624EaC1F55',
value: '1000000000',
gas: 2000000
};
web3.eth.accounts.signTransaction(testTx, "").then(txSig => {
txSig.messageHash = "0x1234";
txSig.rawTransaction = "0x5678";
const noHashFieldInTxSig: typeof txSig & { hash?: never, message?: never, signature?: never } = txSig;
});
const msgSig = web3.eth.accounts.sign("0x1234", "0x5678");
msgSig.messageHash = "0x1234";
msgSig.message = "0x5678";
msgSig.signature = "0x90ab";
const noHashFieldInMsgSig: typeof msgSig & { hash?: never, rawTransaction?: never } = msgSig;
const encryptedKeystore = web3.eth.accounts.encrypt("0x1234", "5678");
encryptedKeystore.crypto.cipher = "aes-128-ctr";
const msgSignature: string = account.sign("0x1234").signature;
account.signTransaction(testTx).then(txSig => {
const txSignature: string = txSig.rawTransaction;
});
//
// web3.eth.personal