nervosnetwork / polyjuice-provider

Godwoken-Polyjuice compatible providers for ethereum library
https://www.npmjs.com/org/polyjuice-provider
8 stars 14 forks source link

Polyjuice Provider

godwoken-polyjuice compatible providers for ethereum library like ethers and web3js.

Please Read Known-Caveats Carefully Before Using This Library

Important Notes

Install

yarn add @polyjuice-provider/web3
yarn add @polyjuice-provider/ethers
yarn add @polyjuice-provider/truffle

Usage

simple React example

Docs

note: this project is still under development, some APIs might be changed in the future.

Known Caveats(things you should be careful about !!)

polyjuice can run your solidity contract code compatible with EVM, but in order to achieve max interoperability with more than just Ethereum, it use polyjuice address, which is different from ethereum address.

the polyjuice-provider convert the two different address type automatically for your Dapp. we aims to provide 100% compatibility, but there are still some limits as this library developing.

short version:

long version and why:

you can learn more by reading how it works below.

How It Works?

there are three main differences between Godwoken-Polyjuice and Ethereum:

  1. Different Transaction Structure: when you send an ethereum transaction to Godwoken-Polyjuice, the data structure of this very transaction needs to be converting to godwoken transaction type.
  2. Different Signing Message: when you are signing a Godwoken-Polyjuice tx, it will looks like you are requesting to signing a message using ethereum personal-sign method.
  3. Different Address Type: polyjuice address is different from ethereum address. when you pass some address-type parameters to call smart-contract, the address converting must be done in order to feed the right polyjuice address for Godwoken-Polyjuice. vice versa for the return address value.

provider are designed to got these 3 things done for you and your dapp, mainly by:

  1. convert Ethereum transaction to Godwoken L2 transaction.
  2. generate signing message from Godwoken L2 transaction and call Metamask for signing (or use privateKey in non-browser env).
  3. do address type converting according to your contract's Abi. that's why you need to pass AbiItems to provider constructor. you can pass multiple smart-contracts ABIs to provider if needed.

Need another provider?

right now we have 3 compatible providers:

if you need another compatible provider, you can request via opening an issue or even create an pull request by yourself :) we hope community can takes part in and build more useful tools together for godwoken-polyjuice.

How To Develop This Project

yarn
yarn build

Test

start a devnet via Godwoken-Kicker on your local environment.

run

yarn env

to generate .env file across all workspace. the terminal will ask you to enter some polyjuice config.

default web3 rpc url is localhost:8024(and ws rpc is localhost:8024/ws), if you want to use that default value, just press enter directly. the rollup type hash and eth account lock hash are optional parameters too, you can skip those as well.

finally, you can run all tests with

yarn test