Skip to content
This repository has been archived by the owner on May 24, 2022. It is now read-only.

Rewrite post$ to take a password and not use signer #93

Merged
merged 12 commits into from
Jan 22, 2019
5 changes: 5 additions & 0 deletions packages/api/src/rpc/personal/personal.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,11 @@ class Personal {
.then(outAddress);
}

signTransaction(options, password) {
return this._provider
.send('personal_signTransaction', inOptions(options), password);
}

sendTransaction (options, password) {
return this._provider
.send('personal_sendTransaction', inOptions(options), password);
Expand Down
11 changes: 7 additions & 4 deletions packages/light.js/src/rpc/other/makeContract.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,19 +39,20 @@ const getContract = memoizee(
);

/**
* Create a contract, givan an api object.
* Create a contract, given an api object.
* Pure function version of {@link makeContract}.
*
* @ignore
* @param address - The contract address.
* @param abiJson - The contract abi.
* @param passphrase - Passphrase of the account creating the contract
* @param api - The api Object.
* @return - An object whose keys are all the functions of the
* contract, and each function returns an Observable which will fire when the
* function resolves.
*/
const makeContractWithApi = memoizee(
(address: Address, abiJson: any[], api: any) => {
(address: Address, abiJson: any[], passphrase: string, api: any) => {
const abi = new Abi(abiJson); // use types from @parity/abi

// Variable result will hold the final object to return
Expand Down Expand Up @@ -97,7 +98,7 @@ const makeContractWithApi = memoizee(
args
),
...options
});
}, passphrase);
}
};
});
Expand All @@ -111,6 +112,7 @@ const makeContractWithApi = memoizee(
*
* @param address - The contract address.
* @param abiJson - The contract abi.
* @param passphrase - Passphrase of the account creating the contract
* @param options - The options to pass in when creating the contract.
* @return - An object whose keys are all the functions of the
* contract, and each function return an Observable which will fire when the
Expand All @@ -119,10 +121,11 @@ const makeContractWithApi = memoizee(
export const makeContract = (
address: Address,
abiJson: any[],
passphrase: string,
options: { provider?: any } = {}
) => {
const { provider } = options;
const api = provider ? createApiFromProvider(provider) : getApi();

return makeContractWithApi(address, abiJson, api);
return makeContractWithApi(address, abiJson, passphrase, api);
};
29 changes: 10 additions & 19 deletions packages/light.js/src/rpc/other/post.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,14 +37,16 @@ function getTransactionReceipt (transactionHash: string, api: any) {
/**
* Post a transaction to the network.
*
* Calls, in this order, `eth_estimateGas`, `parity_postTransaction`,
* `parity_checkRequest` and `eth_getTransactionReceipt` to get the status of
* Calls, in this order, `eth_estimateGas`, `personal_signTransaction`,
* `eth_sendRawTransaction` and `eth_getTransactionReceipt` to get the status of
* the transaction.
*
* @param tx - Transaction object
* @param passphrase - Passphrase of the account
* @param options? - Options to pass to the {@link RpcObservable}.
* @return - The status of the transaction.
*/
export function post$ (tx: Tx, options: PostOptions = {}) {
export function post$ (tx: Tx, passphrase: string, options: PostOptions = {}) {
const { estimate, provider } = options;
const api = provider ? createApiFromProvider(provider) : getApi();

Expand All @@ -55,22 +57,10 @@ export function post$ (tx: Tx, options: PostOptions = {}) {
const gas = await api.eth.estimateGas(tx);
observer.next({ estimated: gas });
}
const signerRequestId = await api.parity.postTransaction(tx);
observer.next({ requested: signerRequestId });
const transactionHash = await api.pollMethod(
'parity_checkRequest',
signerRequestId
);
if (tx.condition) {
observer.next({ signed: transactionHash, schedule: tx.condition });
} else {
observer.next({ signed: transactionHash });

const receipt = await getTransactionReceipt(transactionHash, api);
observer.next({ confirmed: receipt });
}
const signedTransaction = await api.personal.signTransaction(tx, passphrase);
postRaw$(signedTransaction.raw).subscribe(observer);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since this is a breaking change, what do you think of putting observer.next({ signed: ... }) here

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

sounds good!


observer.complete();
} catch (error) {
observer.next({ failed: error });
observer.error(error);
Expand All @@ -87,16 +77,17 @@ export function post$ (tx: Tx, options: PostOptions = {}) {
* Calls, in this order, `eth_sendRawTransaction` and
* `eth_getTransactionReceipt` to get the status of the transaction.
*
* @param rawTx - Raw transaction
* @param options? - Options to pass to the {@link RpcObservable}.
* @return - The status of the transaction.
*/
export function postRaw$ (tx: string, options: PostOptions = {}) {
export function postRaw$ (rawTx: string, options: PostOptions = {}) {
const { provider } = options;
const api = provider ? createApiFromProvider(provider) : getApi();

const source$ = Observable.create(async (observer: Observer<TxStatus>) => {
try {
const transactionHash = await api.eth.sendRawTransaction(tx);
const transactionHash = await api.eth.sendRawTransaction(rawTx);
observer.next({ signed: transactionHash });
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

... and changing here to observer.next({ sent: ... })


const receipt = await getTransactionReceipt(transactionHash, api);
Expand Down