npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

wan-mpc-smart-account

v0.0.1

Published

## Install

Downloads

29

Readme

mpc-smart-account

Install

Install By Npm

npm install mpcsmartaccount

Install By Yarn

yarn add mpcsmartaccount

Install By Pnpm

pnpm add mpcsmartaccount

Create Smart Account

Step1: Master Key Signer

Master Key Signer is the main signer for signing messages and transactions. It has to inherit the Signer in ethers.js.

Init Signer from idToken

If you want to use signers from YeehaWallet, also need to run this command:

npm install mpcsinger
import {  MpcSigner } from "mpcsinger";

/**
 * export type YeehaJwtSignerOptions = {
 *   private_key: string;                 // The App Identity registered in YeehaWallet server.
 *   userTokenFn: functin;              // The function is get user token from the Oauth service registered in the YeehaWallet server.
 * };
 */

let signer = new MpcSigner({
  private_key,
  userTokenFn
});

/**
 * export type signerInitOptions = {
 *   sendSmsCodeFn: functin;              // The function customer may send smscode
 * };
 */
signer = await signer.init({ sendSmsCodeFn });

Step2 : Smart Account

import { SmartAccount } from "mpcsmartaccount";

/**
 * export type SmartAccountOptions = {
 *  appId: string;                                  // appId registered From Backend
 *  masterKeySigner: Signer;                        // Got Master Key Signer From Step1
 *  chainOptions: {                                 // Chain Options
 *    chainId: number;
 *    rpcUrl: string;                               // The Rpc Url matched the chainId
 *    relayerUrl?: string;                          // relayer url, default official relayer
 *  }[];
 *  fetch?: typeof fetch;
 *  unipassServerUrl?: string;
 * };
 */
const smartAccount = new SmartAccount({
  masterKeySigner: signer,
  appId,
  chainOptions,
});

await smartAccount.init({ chainId }); // init with active chain id. Notice that the chainId must be included in the `rpcUrlList`.

Send Transaction

Step1 Generate Transaction

const tx = {
  to, // To Address
  data, // Transaction Data
  value, // The value transferred to `To Address`
};

Step2 Got Fee Options By Simulating Transaction

const {
  isFeeRequired, // whether required fee for the transaction
  feeOptions: FeeOptions, // the fee options
} = await smartAccount.simulateTransaction(tx);

Step3 Validate Whether Fee is sufficient

import { constants } from "ethers";

if (isFeeRequired) {
  const feeOption; // Select Fee Option from fee options Got From `Step1`
  let balance;
  // Validate Native Token
  if (feeOption.token === constants.ZeroAddress) {
    balance = await smartAccount
      .getProvider()
      .getBalance(await smartAccount.getAddress());

    // Validate ERC20 Token
  } else {
    const erc20Interface = [
      "function balanceOf(address _owner) public view returns (uint256 balance)",
    ];
    const erc20Contract = new Contract(
      feeOption.token,
      new ethers.utils.Interface(ERC20Interface),
      smartAccount.getProvider()
    );
    balance = erc20Contract.balanceOf(await smartAccount.getAddress());
  }

  if (balance.le(feeOptions.amount)) {
    console.error("Fee Balance Not Enough");
  }
}

Notice that if there is a transaction involving fee tokens, the validating result may not be accurate.

Step4 Sending Transaction With Specific Fee Option

if (isFeeRequired) {
  const feeOption;
  const response = await smartAccount.sendTransaction(tx, {
    fee: feeOption,
  }); // Send Transaction
  const receipt = await response.wait(); // Got Transaction Receipt Or Wait For at most 60 seconds
  // const receipt = await response.wait(1, 60);
} else {
  // Not Need Fee
  const response = await smartAccount.sendTransaction(tx); // Send Transaction
  const receipt = await response.wait(); // Got Transaction Receipt
}

Sign And Verify Message

import { verifyMessage } from "@unipasswallet/smart-account-validator";

const message; // The Message to Sign
const signature = await smartAccount.signMessage(message); // Sign message
const isValid = await verifyMessage({
  // Verify Message
  message,
  signature,
  provider,
  address: await smartAccount.getAddress(),
});

Sign And Verify Typed Data

import { verifyTypedData } from "@unipasswallet/smart-account-validator";

const typedData; // The Typed Data to Sign
// Sign V4 Typed Data
const signature = await smartAccount.signTypedData(
  typedData.domain,
  typedData.types,
  typedData.message
); // Sign message
// Verify V4 Typed Data
const isValid = await verifyTypedData({
  // Verify Message
  typedData,
  signature,
  provider,
  address: await smartAccount.getAddress(),
});

Destroy Smart Account

await smartAccount.destroy();

Notice that if you are using UniPassJwtSigner and pass the storage, please run destroy method to avoid security issues.

Switch new Smart Account

// Destroy Old Smart Account
await oldSmartAccount.destroy();

// Reconstruct a new Smart Account
const newSmartAccount = new SmartAccount({
  masterKeySigner: signer,
  appId,
  chainOptions,
});

await newSmartAccount.init({ chainId });

Methods of SmartAccount

The instance of SmartAccount returns the following functions:

  • Get Smart Account Info
    • getAddress() : returns the address of your smart account.
    • isDeployed() : returns the result whether your smart account is deployed in current chain.
    • getProvider(): returns current provider that your smart account is using.
    • getChainId(): returns current chain id of your smart account.
  • sendTransaction(): returns the response of transaction
  • signMessage(): returns the signature
  • switchChain(): switch active chain and returns smart account with new chain.

Get Smart Account Info (Finished)

getAddress()

This returns the address of your smart account.

const address = await smartAccount.getAddress();

isDeployed()

This returns the result whether your smart account is deployed in current chain.

const isDeployed = await smartAccount.isDeployed();

getProvider()

This returns current provider that your smart account is using.

const provider = smartAccount.getProvider();

getChainId()

This returns current chain of your smart account.

const chainId = smartAccount.getChainId();

switchChain()

Switch active chain and returns smart account with new chain.

smartAccount = await smartAccount.switchChain(chainId);