Skip to content
This repository has been archived by the owner on Feb 23, 2023. It is now read-only.

Commit

Permalink
Merge pull request #51 from nervosnetwork/develop
Browse files Browse the repository at this point in the history
release v0.0.1-rc12
  • Loading branch information
RetricSu authored Oct 19, 2021
2 parents 51ce8d5 + 0fcc089 commit 40a83c2
Show file tree
Hide file tree
Showing 35 changed files with 1,791 additions and 73 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ provider are designed to got these 3 things done for you and your dapp, mainly b

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.
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](docs/get-started.md#L91) if needed.

## Known Caveats Of polyjuice-provider

Expand Down
41 changes: 31 additions & 10 deletions docs/api.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ import { SOME_NAME } from "@polyjuice-provider/base";
type PolyjuiceConfig = {
rollupTypeHash?: string;
ethAccountLockCodeHash?: string;
creatorId?: HexNumber;
defaultFromAddress?: HexString;
abiItems?: AbiItems;
web3Url?: string;
};
Expand All @@ -29,6 +31,10 @@ type GodwokerOption = {
rollup_type_hash?: Hash;
eth_account_lock?: Omit<Script, "args">;
};
polyjuice?: {
creator_id?: HexNumber;
default_from_address?: HexString;
};
queryEthAddressByShortAddress?: (short_address: string) => string;
saveEthAddressShortAddressMapping?: (eth_address: string, short_address: string) => void;
request_option?: object;
Expand Down Expand Up @@ -76,12 +82,14 @@ function decodeArgs(_args: HexString): {
function encodeArgs(_tx: EthTransaction): string;

class Godwoker {
private eth_account_lock;
private rollup_type_hash;
private client;
private godwokenUtils;
private queryEthAddressByShortAddress;
private saveEthAddressShortAddressMapping;
eth_account_lock: Omit<Script, "args"> | undefined;
rollup_type_hash: string | undefined;
creator_id: HexNumber | undefined;
default_from_address: HexString | undefined;
client: any;
godwokenUtils: GodwokenUtils;
queryEthAddressByShortAddress: ((short_address: string) => string) | undefined;
saveEthAddressShortAddressMapping: ((eth_address: string, short_address: string) => void) | undefined;
constructor(host: string, option?: GodwokerOption);
init(): Promise<void>;
initSync(): Promise<void>;
Expand All @@ -96,12 +104,13 @@ class Godwoker {
computeShortAddressByEoaEthAddress(_address: string): HexString;
getShortAddressByAllTypeEthAddress(_address: string): Promise<ShortAddress>;
getEthAddressByAllTypeShortAddress(_short_address: HexString): Promise<HexString>;
isShortAddressOnChain(short_address: HexString): Promise<boolean>;
isShortAddressOnChain(short_address: HexString, scriptHashCallback?: (script_hash: HexString) => void): Promise<boolean>;
checkEthAddressIsEoa(eth_address: string, _target_short_address: string): boolean;
defaultQueryEthAddressByShortAddress(_short_address: string): Promise<HexString>;
getNonce(account_id: number): Promise<HexNumber>;
assembleRawL2Transaction(eth_tx: EthTransaction): Promise<RawL2Transaction>;
generateTransactionMessageToSign(tx: RawL2Transaction, sender_script_hash: string, receiver_script_hash: string, is_add_prefix_in_signing_message?: boolean): string;
generateMessageFromRawL2Transaction(rawL2Tx: RawL2Transaction, msg_type?: SigningMessageType): Promise<string>;
generateMessageFromEthTransaction(tx: EthTransaction, msg_type?: SigningMessageType): Promise<string>;
serializeL2Transaction(tx: L2Transaction): HexString;
serializeRawL2Transaction(tx: RawL2Transaction): HexString;
Expand Down Expand Up @@ -137,9 +146,9 @@ function filterInterestedInput(data: HexString, abiItem: AbiItem): DecodedMethod
function getAddressesFromInputDataByAbi(data: HexString, abiItem: AbiItem): string[];

class Abi {
private abi_items;
private interested_methods;
private interested_method_ids;
abi_items: AbiItem[];
interested_methods: AbiItem[];
interested_method_ids: MethodIDs;
constructor(_abi_items: AbiItem[]);
get_method_ids(_abi_items: AbiItem[]): MethodIDs;
filter_interested_methods(_abi_items: AbiItem[]): AbiItem[];
Expand All @@ -163,6 +172,8 @@ class PolyjuiceJsonRpcProvider extends providers.JsonRpcProvider {
godwoker: Godwoker;
constructor(polyjuice_config: PolyjuiceConfig, url?: ConnectionInfo | string, network?: Networkish);
setAbi(abiItems: AbiItems): void;
setMultiAbi(abiItemsArray: AbiItems[]): void;
addAbi(_abiItems: AbiItems): void;
sendTransaction(signedTransaction: string | Promise<string>): Promise<TransactionResponse>;
send(method: string, params: Array<any>): Promise<any>;
prepareRequest(method: string, params: any): [string, Array<any>];
Expand All @@ -177,6 +188,8 @@ class PolyjuiceWebsocketProvider extends providers.WebSocketProvider {
godwoker: Godwoker;
constructor(polyjuiceConfig: PolyjuiceConfig, url: string, network?: Networkish);
setAbi(abiItems: AbiItems): void;
setMultiAbi(abiItemsArray: AbiItems[]): void;
addAbi(_abiItems: AbiItems): void;
sendTransaction(signedTransaction: string | Promise<string>): Promise<TransactionResponse>;
prepareRequest(method: string, params: any): [string, Array<any>];
send(method: string, params?: Array<any>): Promise<any>;
Expand All @@ -187,6 +200,8 @@ class PolyjuiceWallet extends Wallet {
abi: Abi;
constructor(privateKey: BytesLike | ExternallyOwnedAccount | SigningKey, polyjuiceConfig: PolyjuiceConfig, provider?: providers.JsonRpcProvider);
setAbi(abiItems: AbiItems): void;
setMultiAbi(abiItemsArray: AbiItems[]): void;
addAbi(_abiItems: AbiItems): void;
signTransaction(transaction: TransactionRequest): Promise<string>;
}
```
Expand All @@ -200,6 +215,8 @@ class PolyjuiceHttpProvider {
abi: Abi;
constructor(host: string, polyjuice_config: PolyjuiceConfig, options?: HttpProviderOptions);
setAbi(abiItems: AbiItems): void;
setMultiAbi(abiItemsArray: AbiItems[]): void;
addAbi(_abiItems: AbiItems): void;
send(payload: any, callback?: (error: Error | null, result: JsonRpcResponse | undefined) => void):Promise<void>;
}

Expand All @@ -216,6 +233,8 @@ class PolyjuiceWebsocketProvider extends Web3WsProvider {
responseQueue: Map<number | string, RequestItem>;
constructor(host: string, polyjuiceConfig: PolyjuiceConfig, option?: WebsocketProviderOptions);
setAbi(abiItems: AbiItems): void;
setMultiAbi(abiItemsArray: AbiItems[]): void;
addAbi(_abiItems: AbiItems): void;
simulateWebsocketResponse(result: JsonRpcResponse, id: string | number): void;
}

Expand All @@ -224,6 +243,8 @@ class PolyjuiceAccounts extends Accounts {
abi: Abi;
constructor(polyjuiceConfig: PolyjuiceConfig, provider?: provider);
setAbi(abiItems: AbiItems): void;
setMultiAbi(abiItemsArray: AbiItems[]): void;
addAbi(_abiItems: AbiItems): void;
signTransaction(_tx: TransactionConfig, privateKey: string, callback?: (error: Error, signedTransaction?: SignedTransaction) => void): Promise<SignedTransaction>;
}
```
Expand Down
49 changes: 49 additions & 0 deletions docs/get-started.md
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,8 @@ import { PolyjuiceWallet, PolyjuiceJsonRpcProvider } from "@polyjuice-provider/e
const polyjuiceConfig: PolyjuiceConfig = {
rollupTypeHash: 'godwoken rollup type hash', // this is optional
ethAccountLockCodeHash: 'godwoken eth account lock code hash', // this is optional
creatorId: 'polyjuice creator account id', // this is optional
defaultFromAddress: 'a default eth address, which will be used as a default from in ethereum transaction', // this is optional
abiItems: ['your abi items array'],
web3Url: 'godwoken web3 rpc url',
};
Expand Down Expand Up @@ -86,6 +88,27 @@ const rpc = new PolyjuiceJsonRpcProvider(polyjuiceConfig, polyjuiceConfig.web3Ur
const deployer = new PolyjuiceWallet('<your deployer private key>', polyjuiceConfig, rpc);
```

if you have multiple smart-contracts on the frontend, you can pass multiple abis to provider via `setMultiAbi()` / `addAbi()`:

```ts
const polyjuiceConfig: PolyjuiceConfig = {
web3Url: 'godwoken web3 rpc url',
};
const provider = new PolyjuiceJsonRpcProvider(polyjuiceConfig, polyjuiceConfig.web3Url);

const abi_items_1 = [...];
const abi_items_2 = [...];
const abi_items_3 = [...];

provider.setMultiAbi([abi_items_1, abi_items_2, abi_items_3]);

// or

provider.addAbi(abi_items1);
provider.addAbi(abi_items2);
provider.addAbi(abi_items3);
```

when init websocket provider, one thing to pay attention is that you still need to feed PolyjuiceConfig with web3-http-rpc-url:

```ts
Expand Down Expand Up @@ -252,6 +275,8 @@ import { PolyjuiceHttpProvider, PolyjuiceAccounts } from "@polyjuice-provider/we
const polyjuiceConfig: PolyjuiceConfig = {
rollupTypeHash: 'godwoken rollup type hash', // this is optional
ethAccountLockCodeHash: 'godwoken eth account lock code hash', // this is optional
creatorId: 'polyjuice creator account id', // this is optional
defaultFromAddress: 'a default eth address, which will be used as a default from in ethereum transaction', // this is optional
abiItems: ['your abi items array'],
web3Url: 'godwoken web3 rpc url',
};
Expand Down Expand Up @@ -294,6 +319,30 @@ provider = new PolyjuiceHttpProvider(
polyjuiceAccounts = new PolyjuiceAccounts(polyjuiceConfig);
```

if you have multiple smart-contracts on the frontend, you can pass multiple abis to provider via `setMultiAbi()` / `addAbi()`:

```ts
const polyjuiceConfig: PolyjuiceConfig = {
web3Url: 'godwoken web3 rpc url',
};
const provider = new PolyjuiceHttpProvider(
polyjuiceConfig.web3Url,
polyjuiceConfig,
);

const abi_items_1 = [...];
const abi_items_2 = [...];
const abi_items_3 = [...];

provider.setMultiAbi([abi_items_1, abi_items_2, abi_items_3]);

// or

provider.addAbi(abi_items1);
provider.addAbi(abi_items2);
provider.addAbi(abi_items3);
```

when init websocket provider, one thing to pay attention is that you still need to feed PolyjuiceConfig with web3-http-rpc-url:

```ts
Expand Down
4 changes: 2 additions & 2 deletions packages/base/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@polyjuice-provider/base",
"version": "0.0.1-rc12-beta.1",
"version": "0.0.1-rc12",
"main": "lib/index.js",
"license": "MIT",
"author": "RetricSu <inneverland2013@gmail.com>",
Expand Down Expand Up @@ -58,7 +58,7 @@
},
"dependencies": {
"@ckb-lumos/base": "0.18.0-rc1",
"@polyjuice-provider/godwoken": "0.0.1-rc12-beta.1",
"@polyjuice-provider/godwoken": "0.0.1-rc12",
"buffer": "^6.0.3",
"encoding": "^0.1.13",
"eth-sig-util": "^3.0.1",
Expand Down
6 changes: 3 additions & 3 deletions packages/base/src/abi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -289,9 +289,9 @@ export function getAddressesFromInputDataByAbi(
}

export class Abi {
private abi_items: AbiItem[] = [];
private interested_methods: AbiItem[] = [];
private interested_method_ids: MethodIDs = {};
public abi_items: AbiItem[] = [];
public interested_methods: AbiItem[] = [];
public interested_method_ids: MethodIDs = {};

constructor(_abi_items: AbiItem[]) {
this.abi_items = _abi_items;
Expand Down
19 changes: 15 additions & 4 deletions packages/base/src/process.ts
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ export async function buildProcess(
);
}

tx.from = tx.from || (await godwoker.getPolyjuiceDefaultFromAddress());
tx.from = tx.from || godwoker.default_from_address!;

let addressMappingItemVec: AddressMappingItem[] = [];
function setAddressMappingItemVec(
Expand Down Expand Up @@ -138,10 +138,21 @@ export async function buildProcess(
case ProcessTransactionType.send: {
const signingMessageType =
process.signingMessageType || SigningMessageType.withPrefix;
const message = await godwoker.generateMessageFromEthTransaction(
t,
signingMessageType

// generate message to sign
const senderScriptHash = godwoker.computeScriptHashByEoaEthAddress(
t.from
);
const receiverScriptHash = await godwoker.getScriptHashByAccountId(
parseInt(rawL2Tx.to_id, 16)
);
const message = godwoker.generateTransactionMessageToSign(
rawL2Tx,
senderScriptHash,
receiverScriptHash,
signingMessageType === SigningMessageType.withPrefix
);

const _signature = await process.signingMethod!(message);
const signature = godwoker.packSignature(_signature);
const l2Tx = { raw: rawL2Tx, signature: signature };
Expand Down
4 changes: 3 additions & 1 deletion packages/base/src/types.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
import { HexString } from "@ckb-lumos/base";
import { HexNumber, HexString } from "@ckb-lumos/base";
import { AbiItem } from "web3-utils";

export type AbiItems = AbiItem[];

export type PolyjuiceConfig = {
rollupTypeHash?: string;
ethAccountLockCodeHash?: string;
creatorId?: HexNumber;
defaultFromAddress?: HexString;
abiItems?: AbiItems;
web3Url?: string;
};
Expand Down
Loading

0 comments on commit 40a83c2

Please sign in to comment.