0.0.9-alpha.0 • Published 1 year ago

@next-wallet/iframe v0.0.9-alpha.0

Weekly downloads
-
License
MIT
Repository
github
Last release
1 year ago

注意:intro

@next-wallet/iframe 应该放到一个iframe中执行,如nextjs

Installation

Install the library using npm:

npm install @next-wallet/iframe

Usage

Importing the Library

First, import the library in your project:

import * as authLib from '@next-wallet/iframe';

Creating a Wallet

Create a new wallet and store the shares:

const iframeAPI = new authLib.IframeAPI({
  appId: 'your-app-id', // 目前没有使用
  userId: 'your-user-id', // 目前没有使用
  storeSharesApi: async (address, authShare, recoverShare) => {
    // Implement your API call to store shares
  },
  fetchAuthShareApi: async (address) => {
    // Implement your API call to fetch auth share
    return { authShare: 'auth-share-hex' };
  },
  fetchSharesApi: async (address) => {
    // Implement your API call to fetch shares
    return { authShare: 'auth-share-hex', recoveryShare: 'recovery-share-hex' };
  },
  enablePassword: true, // use password or default to kdf
  password: 'your-password',
  defaultChain: 'your-default-chain',
});

await iframeAPI.createWallet('message-id');

⚠️ Example

const message = {
  action: EmbeddedWalletAction.hello,
  payload: 'this is',
};

sendMessageToIframe(message, iframeRef.current).then((response) => {
  console.log('hello', response);
  setHello(response.data);
});

Recovering Device Share

Recover the device share after verifying MFA:

await iframeAPI.recoveryDeviceShare('message-id', { address: 'your-address' });

Updating Device Share

Update the device share after verifying MFA:

await iframeAPI.updateDeviceShare('message-id', { address: 'your-address' });

Adding a New Device Share

Add a new device share after verifying MFA:

await iframeAPI.addDeviceShare('message-id', { address: 'your-address' });

Importing Device Share

Import a device share:

await iframeAPI.importDeviceShare('message-id', { address: 'your-address', deviceShare: 'device-share-hex' });

Importing Secret

Import a secret and generate new shares:

await iframeAPI.importSecret('message-id', { address: 'your-address', secret: 'your-secret-hex' });

Exporting Device Share

Export a device share after verifying MFA:

await iframeAPI.exportDeviceShare('message-id', { address: 'your-address' });

Exporting Secret

Export a secret after verifying MFA:

await iframeAPI.exportSecret('message-id', { address: 'your-address' });

Signing a Message

Sign a message using the private key:

await iframeAPI.signMessage('message-id', { address: 'your-address', message: 'your-message' });

Signing a Transaction

Sign a transaction using the private key:

await iframeAPI.signTransaction('message-id', { address: 'your-address', transaction: 'your-transaction' });

Signing Typed Data

Sign typed data using the private key:

await iframeAPI.signTypedData('message-id', {
  address: 'your-address',
  typedDataDefinition: 'your-typed-data-definition',
});

API Reference

generatePrivateKeyShares(): Promise<Required<KeyData>>

Generates new private key shares from a mnemonic.

  • Returns: A promise that resolves to an object containing the private key, address, and shares.

IframeAPI

Class for managing device authentication and cryptographic operations.

Constructor

constructor(params: IframeAPIParams)
  • Parameters:
    • params: An object containing the following properties:
      • appId: The application ID.
      • userId: The user ID.
      • storeSharesApi: Function to store shares.
      • fetchAuthShareApi: Function to fetch the auth share.
      • fetchSharesApi: Function to fetch all shares.
      • enablePassword: Boolean to enable password protection.
      • password: The password for encryption (if enabled).
      • defaultChain: The default blockchain chain.

Methods

  • createWallet(messageId: string): Creates a new wallet.
  • recoveryDeviceShare(messageId: string, payload: { address: Hex }): Recovers the device share.
  • updateDeviceShare(messageId: string, payload: { address: Hex }): Updates the device share.
  • addDeviceShare(messageId: string, payload: { address: Hex }): Adds a new device share.
  • importDeviceShare(messageId: string, payload: { address: Hex; deviceShare: DeviceShare }): Imports a device share.
  • importSecret(messageId: string, payload: { address: Hex; secret: Hex }): Imports a secret.
  • exportDeviceShare(messageId: string, payload: { address: Hex; password?: string }): Exports a device share.
  • exportSecret(messageId: string, payload: { address: Hex; password?: string }): Exports a secret.
  • signMessage(messageId: string, payload: { messageId: string; address: Hex; message?: SignableMessage }): Signs a message.
  • signTransaction(messageId: string, payload: { address: Hex; transaction: { transaction: transaction; args?: { serializer?: serializer | undefined } | undefined } }): Signs a transaction.
  • signTypedData(messageId: string, payload: { address: Hex; typedDataDefinition: TypedDataDefinition<typedData, primaryType> }): Signs typed data.

License

This project is licensed under the MIT License - see the LICENSE file for details.