Skip to content

Commit

Permalink
feat(MPC-2011): refactor webjs to viem
Browse files Browse the repository at this point in the history
  • Loading branch information
MnrGreg committed Jan 21, 2025
1 parent dd701dd commit 917ade7
Show file tree
Hide file tree
Showing 34 changed files with 4,907 additions and 5,671 deletions.
2 changes: 1 addition & 1 deletion .env.example
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
BLOCKDAEMON_RPC_URL="https://svc.blockdaemon.com/native/v1/ethereum/holesky?apiKey=zpka_..."
BUILDERVAULT_PLAYER_COUNT=3 # Count of BuilderVault MPC Node Players
BUILDERVAULT_PLAYER0_URL="http://localhost:8500"
BUILDERVAULT_PLAYER0_MPCPUBLICKEY="MFkwEwYHKoZIzj0....gO+224X8T0J9eMg=" # Base64 encoding of MPC Player public key. Used in Dynamic communications with broker setups
Expand All @@ -21,3 +20,4 @@ BUILDERVAULT_PLAYER1_APIKEY="..."
## Optional mTLS-based server certificate pinning
# BUILDERVAULT_PLAYER0_MTLSPUBLICKEY="-----BEGIN CERTIFICATE-----\nMIICMTCCAdegAwIBAg...iABMV+KTXJxA==\n-----END CERTIFICATE-----"
# BUILDERVAULT_PLAYER1_MTLSPUBLICKEY="-----BEGIN CERTIFICATE-----\nMIICMjCCAdegAwIBAg...srtGsDhLOe8O8=\n-----END CERTIFICATE-----"

85 changes: 44 additions & 41 deletions .github/workflows/build.yaml
Original file line number Diff line number Diff line change
@@ -1,41 +1,44 @@
name: Build and Publish NPM Package

on:
push:
branches: [main]
tags: ["v*.*.*"]

jobs:
build:
runs-on: ubuntu-latest
permissions:
contents: read
packages: write

steps:
- name: Checkout code
uses: actions/checkout@v2

- name: Setup Node.js
uses: actions/setup-node@v2
with:
node-version: '22'
registry-url: https://npm.pkg.github.com/

- name: Install dependencies
run: npm install

- name: Add @sepior/tsmsdkv2 Typescript Definitions
run: cp ./types/* ./node_modules/@sepior/tsmsdkv2/

- name: Run Tests
run: npm test
continue-on-error: true # ToDo: remove once integration with hosted BV sandbox is in place.

- name: Build TypeScript application
run: npm run build

- name: Publish to GitHub npm package registry
env:
NODE_AUTH_TOKEN: ${{secrets.GITHUB_TOKEN}}
run: npm publish
# name: Build and Publish NPM Package

# on:
# push:
# branches: [main]
# tags: ["v*.*.*"]

# jobs:
# build:
# runs-on: ubuntu-latest
# permissions:
# contents: read
# packages: write

# steps:
# - name: Checkout code
# uses: actions/checkout@v4

# - name: Setup Node.js
# uses: actions/setup-node@v2
# with:
# node-version: '23'
# registry-url: https://npm.pkg.github.com/

# - name: Install dependencies
# run: npm install

# - name: Run Tests
# run: npm test

# - name: Build TypeScript application
# run: npm run build

# - name: Publish to GitHub npm package registry
# env:
# NODE_AUTH_TOKEN: ${{secrets.GITHUB_TOKEN}}
# run: |
# NEW_VERSION=$(npm version patch --no-git-tag-version)
# git config user.name "github-actions[bot]"
# git config user.email "github-actions[bot]@users.noreply.github.com"
# git add package.json
# git commit -m "chore(release SDK): $NEW_VERSION"
# git push origin HEAD:main
# npm publish
48 changes: 42 additions & 6 deletions .github/workflows/tests.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -11,21 +11,57 @@ on:
jobs:
test:
runs-on: ubuntu-latest
env:
BUILDERVAULT_PLAYER_COUNT: 2
BUILDERVAULT_PLAYER0_URL: "http://localhost:8500"
BUILDERVAULT_PLAYER0_APIKEY: "apikey0"
BUILDERVAULT_PLAYER1_URL: "http://localhost:8501"
BUILDERVAULT_PLAYER1_APIKEY: "apikey1"
BUILDERVAULT_ACCOUNT_ID: 0
BUILDERVAULT_ADDRESS_INDEX: 0

steps:
- name: Checkout code
uses: actions/checkout@v2
uses: actions/checkout@v4

- name: Setup Node.js
uses: actions/setup-node@v2
with:
node-version: '22'

- name: Install dependencies
run: npm install
- name: Download BuilderVault TSM Demo repo
env:
GITLAB_TOKEN: ${{ secrets.GITLAB_TOKEN }}
run: git clone "https://:${{ secrets.GITLAB_TOKEN }}@gitlab.com/Blockdaemon/tsm-demo.git" tsm-demo

- name: Log into Nexus registry
env:
NEXUS_USERNAME: ${{ secrets.NEXUS_USERNAME }}
NEXUS_TOKEN: ${{ secrets.NEXUS_TOKEN }}
run: echo "${{ secrets.NEXUS_TOKEN }}" | docker login nexus.sepior.net:19001 -u "${{ secrets.NEXUS_USERNAME }}" --password-stdin

- name: Run BuilderVault TSM Demo
working-directory: ./tsm-demo/tsm
run: docker compose up --quiet-pull -d

- name: Generate BuilderVault MasterKey
working-directory: ./tsm-demo/sdk/nodejs/
run: |
npm config set @sepior:registry=https://gitlab.com/api/v4/projects/56306653/packages/npm/
npm install @sepior/tsmsdkv2 ethers asn1.js node-fs
npx ts-node crypto_ethereum.js
echo BUILDERVAULT_MASTERKEY_ID=$(cat key.txt) >> "$GITHUB_ENV"
- name: Add @sepior/tsmsdkv2 Typescript Definitions
run: cp ./types/* ./node_modules/@sepior/tsmsdkv2/
- name: Install Foundry Tools
uses: foundry-rs/foundry-toolchain@v1

- name: Run Foundry Anvil
run: |
pwd
anvil &
- name: Install Buildervault Web3 Provider dependencies
run: npm install

- name: Run tests
- name: Run Buildervault Web3 Provider tests
run: npm test
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
*.key
.vscode
init
SCRATCH.md
**/key.txt
**/.env
**/node_modules
Expand Down
133 changes: 63 additions & 70 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,23 @@ npm config set //npm.pkg.github.com/:_authToken=YOUR_GITHUB_PAT
npm install @blockdaemon/buildervault-web3-provider
```

## Usage with viem.sh
```sh
npm install viem
```
```js
import { createWalletClient, custom } from 'viem'

const walletClient = createWalletClient({
transport: custom({
async request({ method, params }) {
const response = await eip1193Provider.request({method, params})
return response
}
})
})
```

## Usage with ethers.js
```sh
npm install ethers@6
Expand All @@ -32,23 +49,6 @@ import Web3 from "web3";
const web3 = new Web3(eip1193Provider);
```

## Usage with viem.sh
```sh
npm install viem
```
```js
import { createWalletClient, custom } from 'viem'

const walletClient = createWalletClient({
transport: custom({
async request({ method, params }) {
const response = await eip1193Provider.request({method, params})
return response
}
})
})
```


## API Documentation

Expand All @@ -57,10 +57,6 @@ const walletClient = createWalletClient({
```ts
export type BuildervaultProviderConfig = {
// ------------- Mandatory fields -------------
/**
* Set the RPC API URL endpoint for JSON-RPC over HTTP access to the blockchain data
*/
rpcUrl?: string,
/**
* Set the numnber of the BuilderVault players
*/
Expand Down Expand Up @@ -116,64 +112,61 @@ export type BuildervaultProviderConfig = {
player0mTLSpublicKey?: string,
player1mTLSpublicKey?: string,
player2mTLSpublicKey?: string,
/**
* Default: false
* By setting to true, every request and response processed by the provider will be logged to the console
* Same as setting env var `DEBUG=buildervault-web3-provider:req_res`
/**
* Set the list of Ethereum chains to be used with this provider (type AddEthereumChainParameter[] compatible with EIP-3085)
*/
logRequestsAndResponses?: boolean,
chains: [
{
chainId: "0x1",
rpcUrls: ["https://svc.blockdaemon.com/native/v1/ethereum/mainnet?apiKey=zpka_853...b25"] as const,
chainName: "Ethereum Mainnet",
nativeCurrency: {
name: "Ether",
symbol: "ETH",
decimals: 18,
},
},
]
}
```
### Web3.js full client example
### Viem full client example
```ts
import { BuildervaultWeb3Provider } from "@blockdaemon/buildervault-web3-provider";
import Web3 from "web3";
import { createEIP1193Provider } from "@blockdaemon/buildervault-web3-provider";
import { createWalletClient, custom } from 'viem';
import { holesky } from 'viem/chains';

const eip1193Provider = new BuildervaultWeb3Provider({
rpcUrl: "https://svc.blockdaemon.com/native/v1/ethereum/holesky?apiKey=zpka_...",
player0Url: "http://localhost:8500",
player0ApiKey: "apikey...",
player1Url: "http://localhost:8501",
player1ApiKey: "apikey...",
masterKeyId: "Ap3...",
accountId: 0, // account of BIP44 m/44/60/account/0/address_index
addressIndex: 0, // address_index of BIP44 m/44/60/account/0/address_index
logRequestsAndResponses: false, // Verbose logging
})
const chain = {
chainName: "Ethereum Holesky",
chainId: "0x4268",
rpcUrls: ["https://svc.blockdaemon.com/native/v1/ethereum/holesky?apiKey=zpka_853...b25"],
};

async function main() {

const web3 = new Web3(eip1193Provider);

const chainId = await web3.eth.getChainId();
console.log(`ChainID:`, chainId);

const accounts = await web3.eth.getAccounts();
console.log(`Wallet addresses:`, accounts);

console.log(`Initial balance for address_index 0:`, await web3.eth.getBalance(accounts[0]));

const feeData = await web3.eth.calculateFeeData();

// Construct the transaction
const transaction = {
chainId: chainId,
from: accounts[0],
to: '0x11f4d0A3c12e86B4b5F39B213F7E19D048276DAe',
value: '1000000000000',
maxFeePerGas: feeData.maxFeePerGas,
maxPriorityFeePerGas: feeData.maxPriorityFeePerGas,
}

// Sign the transaction with BuilderVault, broadcast and wait for the receipt
const receipt = await web3.eth.sendTransaction(transaction);

// Log the transaction receipt
console.log('Transaction receipt:', receipt);

// Log final balances after the transaction has been mined
console.log(`Final balance for address_index 0:`, await web3.eth.getBalance(accounts[0]));
const eip1193Provider = await createEIP1193Provider({
chains: [chain],
playerCount: 2,
player0Url: "http://localhost:8500",
player0ApiKey: "apikey0",
player1Url: "http://localhost:8501",
player1ApiKey: "apikey1",
masterKeyId: "Ap7fC2YPwBKbRXwVHEBVUkHYF37G",
accountId: 0, // account of BIP44 m/44/60/account/0/address_index
addressIndex: 0, // address_index of BIP44 m/44/60/account/0/address_index
});

const walletClient = createWalletClient({
chain: holesky,
transport: custom({
async request({ method, params }) {
const response = await eip1193Provider.request({method, params})
return response
}
})
});

walletClient.requestAddresses().then(console.log);

}

Expand Down
Loading

0 comments on commit 917ade7

Please sign in to comment.