Skip to content

Commit

Permalink
Add initial code for chain id support (#510)
Browse files Browse the repository at this point in the history
* Add initial code for chain id support

* Add chain id for roll op too

* Prettier pass

* Fix some unit tests

* format code

* Factorize buffer to sign code

* Update mock data with chain id

* Update wallet example

* Remove duplication

* Refactor for code duplication

* Add chainId to network

* Remove chainId from ClientFactory ts-docs

* Update web3-utils to 1.4.5

* Update packages/massa-web3/src/web3/accounts/Web3Account.ts

Co-authored-by: Nathan Seva <thykof@protonmail.ch>

* Update packages/massa-web3/src/web3/accounts/Web3Account.ts

Co-authored-by: Nathan Seva <thykof@protonmail.ch>

* Update packages/massa-web3/src/web3/accounts/Web3Account.ts

Co-authored-by: Nathan Seva <thykof@protonmail.ch>

---------

Co-authored-by: sydhds <sydhds@gmail.com>
Co-authored-by: Nathan Seva <thykof@protonmail.ch>
Co-authored-by: Senji888 <44082144+Ben-Rey@users.noreply.github.com>
  • Loading branch information
4 people authored Dec 28, 2023
1 parent a59e35e commit 6af7263
Show file tree
Hide file tree
Showing 19 changed files with 251 additions and 258 deletions.
5 changes: 3 additions & 2 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -69,8 +69,9 @@ bundle.*
# docs files
docs

#.env files
.env
# IDE
.idea

# Misc
.DS_Store

2 changes: 1 addition & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions packages/massa-web3/examples/.env.example
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,7 @@ RECEIVER_PRIVATE_KEY="S1ykLaxXyMnJoaWLYds8UntqKTamZ4vcxrZ1fdToR8WpWEpk3FC"
JSON_RPC_URL_PUBLIC=https://buildnet.massa.net/api/v2
JSON_RPC_URL_PRIVATE=https://buildnet.massa.net/api/v2

CHAIN_ID=77658366

#JSON_RPC_URL_PUBLIC=http://127.0.0.1:33035
#JSON_RPC_URL_PRIVATE=http://127.0.0.1:33034
24 changes: 7 additions & 17 deletions packages/massa-web3/examples/smartContracts/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ import {
strToBytes,
toMAS,
} from '../../src';
import { getEnvVariable } from '../utils';

const path = require('path');
const chalk = require('chalk');
Expand Down Expand Up @@ -84,23 +85,11 @@ dotenv.config({
path: path.resolve(__dirname, '..', '.env'),
});

const publicApi = process.env.JSON_RPC_URL_PUBLIC;
if (!publicApi) {
throw new Error('Missing JSON_RPC_URL_PUBLIC in .env file');
}
const privateApi = process.env.JSON_RPC_URL_PRIVATE;
if (!privateApi) {
throw new Error('Missing JSON_RPC_URL_PRIVATE in .env file');
}
const deployerPrivateKey = process.env.DEPLOYER_PRIVATE_KEY;
if (!deployerPrivateKey) {
throw new Error('Missing DEPLOYER_PRIVATE_KEY in .env file');
}
const receiverPrivateKey = process.env.RECEIVER_PRIVATE_KEY;
if (!receiverPrivateKey) {
throw new Error('Missing RECEIVER_PRIVATE_KEY in .env file');
}

const publicApi = getEnvVariable('JSON_RPC_URL_PUBLIC');
const privateApi = getEnvVariable('JSON_RPC_URL_PRIVATE');
const chainId_ = getEnvVariable('CHAIN_ID');
const chainId = BigInt(chainId_);
const deployerPrivateKey = getEnvVariable('DEPLOYER_PRIVATE_KEY');
const MASSA_EXEC_ERROR = 'massa_execution_error';

interface IEventPollerResult {
Expand Down Expand Up @@ -184,6 +173,7 @@ const pollAsyncEvents = async (
{ url: publicApi, type: ProviderType.PUBLIC } as IProvider,
{ url: privateApi, type: ProviderType.PRIVATE } as IProvider,
],
chainId,
true,
deployerAccount,
);
Expand Down
7 changes: 7 additions & 0 deletions packages/massa-web3/examples/utils.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
export function getEnvVariable(key: string): string {
const value = process.env[key];
if (!value) {
throw new Error(`Missing ${key} in .env file`);
}
return value;
}
26 changes: 9 additions & 17 deletions packages/massa-web3/examples/wallet/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,29 +12,20 @@ import * as dotenv from 'dotenv';
import { Client } from '../../src/web3/Client';
import { IProvider, ProviderType } from '../../src/interfaces/IProvider';
import { fromMAS } from '../../src';
import { getEnvVariable } from '../utils';
const path = require('path');
const chalk = require('chalk');

dotenv.config({
path: path.resolve(__dirname, '..', '.env'),
});

const publicApi = process.env.JSON_RPC_URL_PUBLIC;
if (!publicApi) {
throw new Error('Missing JSON_RPC_URL_PUBLIC in .env file');
}
const privateApi = process.env.JSON_RPC_URL_PRIVATE;
if (!privateApi) {
throw new Error('Missing JSON_RPC_URL_PRIVATE in .env file');
}
const deployerPrivateKey = process.env.DEPLOYER_PRIVATE_KEY;
if (!deployerPrivateKey) {
throw new Error('Missing DEPLOYER_PRIVATE_KEY in .env file');
}
const receiverPrivateKey = process.env.RECEIVER_PRIVATE_KEY;
if (!receiverPrivateKey) {
throw new Error('Missing RECEIVER_PRIVATE_KEY in .env file');
}
const publicApi = getEnvVariable('JSON_RPC_URL_PUBLIC');
const privateApi = getEnvVariable('JSON_RPC_URL_PRIVATE');
const chainId_ = getEnvVariable('CHAIN_ID');
const chainId = BigInt(chainId_);
const deployerPrivateKey = getEnvVariable('DEPLOYER_PRIVATE_KEY');
const receiverPrivateKey = getEnvVariable('RECEIVER_PRIVATE_KEY');

(async () => {
const header = '='.repeat(process.stdout.columns - 1);
Expand All @@ -57,6 +48,7 @@ if (!receiverPrivateKey) {
{ url: publicApi, type: ProviderType.PUBLIC } as IProvider,
{ url: privateApi, type: ProviderType.PRIVATE } as IProvider,
],
chainId,
true,
deployerAccount,
);
Expand Down Expand Up @@ -113,7 +105,7 @@ if (!receiverPrivateKey) {
// sign a random wallet message using account2
const signedMessage = await web3Client
.wallet()
.signMessage(message, receiverAccount.address);
.signMessage(message, chainId, receiverAccount.address);
console.log('Wallet sender signing a message... ', signedMessage);

if (!deployerAccount?.publicKey || !signedMessage) {
Expand Down
1 change: 1 addition & 0 deletions packages/massa-web3/src/interfaces/INodeStatus.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,4 +70,5 @@ export interface INodeStatus {
node_ip: string | null;
pool_stats: { endorsement_count: number; operation_count: number };
version: string;
chain_id: bigint;
}
1 change: 1 addition & 0 deletions packages/massa-web3/src/interfaces/IWalletClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,7 @@ export interface IWalletClient extends BaseClient {
*/
signMessage(
data: string | Buffer,
chainId: bigint,
accountSignerAddress: string,
): Promise<ISignature>;

Expand Down
8 changes: 6 additions & 2 deletions packages/massa-web3/src/web3/ClientFactory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,13 +32,15 @@ export class ClientFactory {
* Creates a default client using a default provider (MAINNET, TESTNET, LABNET, LOCALNET and BUILDNET).
*
* @param provider - Default provider to be used by the client.
* @param chainId - Chain id matching the network used by the provider
* @param retryStrategyOn - Whether to retry failed requests.
* @param baseAccount - Base account to use with the client (optional).
*
* @returns A promise that resolves to a Client object.
*/
public static async createDefaultClient(
provider: DefaultProviderUrls,
chainId: bigint,
retryStrategyOn = true,
baseAccount?: IAccount,
): Promise<Client> {
Expand Down Expand Up @@ -75,7 +77,7 @@ export class ClientFactory {
let publicApi = new PublicApiClient(clientConfig);
let account: Web3Account = null;
if (baseAccount) {
account = new Web3Account(baseAccount, publicApi);
account = new Web3Account(baseAccount, publicApi, chainId);
}
const client: Client = new Client(
{
Expand All @@ -95,13 +97,15 @@ export class ClientFactory {
* Suitable for local node interactions.
*
* @param providers - Array of providers to be used by the client.
* @param chainId - Chain id matching the network used by the provider
* @param retryStrategyOn - Whether to retry failed requests.
* @param baseAccount - Base account to be used by the client (optional).
*
* @returns A promise that resolves to a Client object.
*/
public static async createCustomClient(
providers: Array<IProvider>,
chainId: bigint,
retryStrategyOn = true,
baseAccount?: IAccount,
): Promise<Client> {
Expand All @@ -112,7 +116,7 @@ export class ClientFactory {
let publicApi = new PublicApiClient(clientConfig);
let account: Web3Account = null;
if (baseAccount) {
account = new Web3Account(baseAccount, publicApi);
account = new Web3Account(baseAccount, publicApi, chainId);
}
const client: Client = new Client(clientConfig, account, publicApi);
return client;
Expand Down
3 changes: 2 additions & 1 deletion packages/massa-web3/src/web3/WalletClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -350,6 +350,7 @@ export class WalletClient extends BaseClient implements IWalletClient {
*/
public async signMessage(
data: string | Buffer,
chainId: bigint,
accountSignerAddress: string,
): Promise<ISignature> {
let signerAccount = this.getWalletAccountByAddress(accountSignerAddress);
Expand All @@ -363,7 +364,7 @@ export class WalletClient extends BaseClient implements IWalletClient {
);
}
} else {
account = new Web3Account(signerAccount, this.publicApiClient);
account = new Web3Account(signerAccount, this.publicApiClient, chainId);
}
if (typeof data === 'string') {
data = Buffer.from(data);
Expand Down
Loading

0 comments on commit 6af7263

Please sign in to comment.