@pier-wallet/mpc-lib
v0.10.9
Published
The Pier Multi-Party Computation (MPC) Wallet SDK allows you to create and manage wallets that leverage multi-party computation protocols for enhanced security and privacy. This documentation provides an overview of the API and its components to help you
Downloads
245
Readme
Pier Multi-Party Computation (MPC) Wallet
The Pier Multi-Party Computation (MPC) Wallet SDK allows you to create and manage wallets that leverage multi-party computation protocols for enhanced security and privacy. This documentation provides an overview of the API and its components to help you integrate and utilize the SDK effectively.
Installation
To use the MPC Wallet SDK, you need to install it as a dependency in your project. You can use npm or yarn to install the required packages.
npm install @pier-wallet/mpc-lib
Usage
Initializing the SDK
import { createPierMpcSdkWasm } from "@pier-wallet/mpc-lib/wasm";
const pierMpcSdk = createPierMpcSdkWasm();
Creating a wallet
This section shows how to create a 2/2 MPC Ethereum wallet. Bitcoin wallet generation is exactly the same but uses a different class to instantiate the wallet.
To create an MPC wallet, you need to establish a connection with other parties. On the first computer, create a session for key generation using the establishConnection
method. You can use the SessionKind.KEYGEN
constant to specify the session type.
import { SessionKind } from "@pier-wallet/mpc-lib";
// PARTY 1
const party1Connection = await pierMpcSdk.establishConnection(
SessionKind.KEYGEN,
{
type: "create",
partiesParameters: {
requiredPartiesToSign: 2,
totalParties: 2,
},
},
);
// share this ID with the second party
console.log(party1Connection.sessionId);
On the second computer, join the session using establishConnection
method, passing the session ID from the first party.
// PARTY 2
const sessionId = getSessionIdFromTheFirstParty();
const party2Connection = await pierMpcSdk.establishConnection(
SessionKind.KEYGEN,
{
type: "join",
sessionId,
},
);
Once the connection is established, you can generate a key share for the wallet using the generateKeyShare
method. On both parties, you can use the same code to generate a key share.
// PARTY 1
const party1KeyShare = await pierMpcSdk.generateKeyShare(party1Connection);
console.log(
"Wallet Address:",
ethers.utils.computeAddress(party1KeyShare.publicKey),
);
// PARTY 2
const party2KeyShare = await pierMpcSdk.generateKeyShare(party2Connection);
console.log(
"Wallet Address:",
ethers.utils.computeAddress(party2KeyShare.publicKey),
);
Addresses of party1KeyShare
and party2KeyShare
will be the same.
Store key share
When key share is generated, you can store it in a secure storage. Use .raw()
method to get key share in JSON serializable format.
saveKeyShareToSecureStorage(JSON.stringify(party1KeyShare.raw()));
Instantiate an Ethereum wallet from a key share
Use PierMpcEthereumWallet
to instantiate an Ethereum wallet from a key share. PierMpcEthereumWallet
implements ethers.js Signer
interface.
You need to create and join a session in the same way as you did for key share generation except you pass SessionKind.SIGN
as a parameter.
import { KeyShare } from "@pier-wallet/mpc-lib";
import { PierMpcEthereumWallet } from "@pier-wallet/mpc-lib/ethers-v5";
// PARTY 1
const party1KeyShare = new KeyShare(
JSON.parse(loadKeyShareFromSecureStorage()),
);
const party1Connection = await pierMpcSdk.establishConnection(
SessionKind.SIGN,
{
type: "create",
partiesParameters: party1KeyShare.partiesParameters,
},
);
const wallet = new PierMpcEthereumWallet(
party1KeyShare,
party1Connection,
pierMpcSdk,
);
console.log("wallet address", wallet.address);
// PARTY 2
const party2KeyShare = new KeyShare(
JSON.parse(loadKeyShareFromSecureStorage()),
);
const party2Connection = await pierMpcSdk.establishConnection(
SessionKind.SIGN,
{
type: "join",
sessionId: getSessionIdFromTheFirstParty(),
},
);
const wallet = new PierMpcEthereumWallet(
party2KeyShare,
party2Connection,
pierMpcSdk,
);
console.log("wallet address", wallet.address);
Getting wallet address
You can retrieve the Ethereum wallet address using the getAddress
method or address
property.
const walletAddress = await wallet.getAddress();
const walletAddress = wallet.address;
Signing a message
To sign a digest using Pier MPC wallet, you can use the signMessage
method. You need to run signMessage
on at least requiredPartiesToSign
parties (2 in our case) for it to generate a signature.
// PARTY 1 and PARTY 2
const message = "hello world";
const signature = await wallet.signMessage(message);
// these two addresses will be the same
console.log(
await wallet.getAddress(),
ethers.utils.verifyMessage(message, signature),
);