0.1.70 • Published 16 days ago

wallet-bridge v0.1.70

Weekly downloads
-
License
-
Repository
-
Last release
16 days ago

wallet-bridge

wallet-bridge is a Web3 wallet connection library that supports multiple chains and multiple login methods. It mainly provides functionalities like connecting wallets, sending transactions, signing messages, etc.

Features

  • Supported Chains: Ethereum, BNB Smart Chain, Polygon, TRON, Dogecoin.
  • Login Methods: Passkey, Torus.

Live Demo

中文文档

Main Scripts

  • dev: Bootstrap the Storybook preview with Hot Reload.
  • build: Builds the static storybook project.
  • build:lib: Builds the component library into the dist folder.
  • lint:fix: Applies linting based on the rules defined in .eslintrc.js.
  • format:prettier: Formats files using the prettier rules defined in .prettierrc.
  • test: Runs testing using watch mode.
  • test:cov: Runs testing displaying a coverage report.

Using the Library

1. Installation:

yarn add wallet-bridge

2. Import Wallet:

import { Wallet } from 'wallet-bridge'

Note: wallet-bridge does not support SSR. If your project uses SSR, make sure to check if you are in a browser environment before using wallet-bridge. Although this library is developed based on react.js, it can also be used in any frontend project.

To import the Wallet on demand, you can use the import() method:

const { Wallet } = await import('wallet-bridge')

3. Initialization

To create a new Wallet object, you can use its constructor and provide the following parameters:

  • isTestNet (optional): Whether to use the test network. Defaults to false.
  • loggedInSelectAddress (optional): Whether to allow users to choose when logging in with Passkey if there are multiple addresses. Defaults to true.
  • customChains (optional): Custom chains sourced from the CustomChain enum. Defaults to an empty array.
  • customWallets (optional): Custom wallets sourced from the CustomWallet enum. Defaults to an empty array.

Example:

const wallet = new Wallet({
  isTestNet: false,
  loggedInSelectAddress: true,
  customChains: [CustomChain.eth],
  customWallets: [CustomWallet.metaMask],
})

4. Wallet Instance Methods

Here are some primary instance methods:

4.1 initWallet({ involution = true }: { involution?: boolean } = {}): Promise<boolean>

Initialize the wallet login status. It can be initialized globally or before every wallet use, and you can decide whether to continue executing the related business code based on the return value. The initWallet method checks whether the wallet is already logged in. If logged in, it won't prompt the user to log in again. If not logged in, the involution parameter can control whether to prompt the user to re-login.

Parameters:

  • involution (optional): Type boolean. Defaults to true. Whether to prompt the user to re-login if not already logged in.

Return Value:

  • Promise<boolean>: Indicates whether the wallet has successfully logged in. Returns true if successful; otherwise, returns false.

Example:

wallet.initWallet().then((success) => {
  console.log('Wallet Initialization:', success ? 'Successful' : 'Failed')
})

4.2 connectWallet(params: { onlyEth?: boolean } = {})

Display the wallet connection popup.

Parameters:

  • onlyEth (optional): Type boolean. Whether to display only the Ethereum chain. Defaults to false.

Example:

wallet.connectWallet({ onlyEth: true })

4.3 loggedInfo()

Display the logged-in popup. If the user is already logged in, the popup will show the login information.

Example:

wallet.loggedInfo()

4.4 sendTransaction(data: ISendTrxParams): Promise<string | undefined>

Used for sending transactions.

Parameters:

  • data: Transaction parameters.
    • to: The transaction's recipient address, type string.
    • value: Transaction amount, must be in the smallest unit of the token, type string.
    • data (optional): Transaction memo, type string.

Return Value:

  • Promise<string | undefined>: After successfully sending the transaction, a transaction hash is returned. If there's any error or issue, it might return undefined or throw an error.

Example:

const transactionData = {
  to: '0x020881E3F5B7832E752d16FE2710eE855A6977Dc',
  value: '10000000000',
  data: 'a message',
}
wallet.sendTransaction(transactionData).then((txHash) => {
  console.log('Transaction Hash:', txHash)
})

4.5 initSignContext(): Promise<InitSignContextRes>

Initialize the signing context and return the signing method. To prevent the browser from blocking the signing popup, ensure that you call initSignContext before any asynchronous operations in the click event.

Return Value:

  • Promise<InitSignContextRes>: Returns an object containing the following methods:
    • signTxList: Function, specific to .bit business, used for signing transaction lists.
    • signData: Function, the return value is the signed data.
    • onFailed: Function, its return value is a DeviceAuthError. In case of any errors, call onFailed to notify the popup to display the error.

Example:

const { signTxList, signData, onFailed } = await wallet.initSignContext()
const res = await signTxList({})
const res = await signData('0x123')
const res = await signData({}, { isEIP712: true })
await onFailed()

4.6 useWalletState(): { walletSnap: Snapshot<WalletState> }

useWalletState is a React hook for retrieving and listening to the wallet's state.

Returns:

  • walletSnap: A current snapshot of the walletState with type WalletState, which contains the following fields:

    • protocol: The protocol type of the wallet, of type WalletProtocol.
    • address: The currently logged-in wallet address, of type string.
    • coinType: The type of token, of type CoinType.
    • hardwareWalletTipsShow: Whether the hardware wallet tips are shown or not, of type boolean.
    • deviceData: Device data during Passkey login, of type IDeviceData.
    • ckbAddresses: List of CKB addresses that the device can manage during Passkey login, of type string[].
    • deviceList: List of backup devices, of type string[].
    • isTestNet: Whether it's on testnet or not, of type boolean.
    • loggedInSelectAddress: Whether to select an address when logging in with Passkey with multiple addresses available, default is true.
    • canAddDevice: Whether a backup device can be added or not, of type boolean.
    • iCloudPasskeySupport: Whether the current environment supports storing the passkey in iCloud, of type boolean.
    • customChains: Custom chains to be displayed, of type CustomChain[].
    • customWallets: Custom wallets to be displayed, of type CustomWallet[].

Example:

function Component() {
  const { walletSnap } = wallet.useWalletState()
  return <div>Address: {walletSnap.address}</div>
}

4.7 getWalletState(): { walletSnap: Snapshot<WalletState> }

Used to immediately get the current snapshot of the wallet's state.

Returns:

Same as the return of useWalletState.

Example:

const { walletSnap } = wallet.getWalletState()
console.log(walletSnap.address)

4.8 _verifyPasskeySignature(params: { message: string, signature: string }): Promise<boolean>

Verify if the passkey signature is correct.

Parameters:

  • params: Transaction parameters.
    • message: Original message, of type string.
    • signature: Result of message signature, of type string.

Return value:

  • Promise<boolean>: If the signature is correct, it returns true. Any errors or issues may result in an exception being thrown.

Example:

wallet._verifyPasskeySignature({ message: '0x123', signature: '0x40b4a569e0cb53163f...' }).then((result) => {
  console.log('result: ', result)
})

License

MIT

1.0.0-beta.11

16 days ago

1.0.0-beta.10

17 days ago

1.0.0-beta.8

18 days ago

1.0.0-beta.9

18 days ago

1.0.0-beta.7

22 days ago

1.0.0-beta.5

27 days ago

1.0.0-beta.3

1 month ago

1.0.0-beta.4

1 month ago

1.0.0-beta.2

1 month ago

1.0.0-beta.1

1 month ago

0.1.70-beta.2

2 months ago

0.1.70-beta.1

2 months ago

0.1.70-beta.4

2 months ago

0.1.70

2 months ago

0.1.69

2 months ago

0.1.67

2 months ago

0.1.68

2 months ago

0.1.66-beta.2

3 months ago

0.1.66-beta.3

3 months ago

0.1.66-beta.1

3 months ago

0.1.66

3 months ago

0.1.65-beta.1

3 months ago

0.1.65-beta.2

3 months ago

0.1.64

3 months ago

0.1.65

3 months ago

0.1.64-beta.1

3 months ago

0.1.64-beta.7

3 months ago

0.1.64-beta.6

3 months ago

0.1.64-beta.3

3 months ago

0.1.64-beta.2

3 months ago

0.1.64-beta.5

3 months ago

0.1.64-beta.4

3 months ago

0.1.63

4 months ago

0.1.62

4 months ago

0.1.62-beta.1

4 months ago

0.1.61

4 months ago

0.1.61-beta.2

4 months ago

0.1.61-beta.3

4 months ago

0.1.61-beta.1

4 months ago

0.1.60

4 months ago

0.1.58

4 months ago

0.1.59

4 months ago

0.1.58-beta.1

4 months ago

0.1.52

6 months ago

0.1.53

6 months ago

0.1.54

6 months ago

0.1.55

6 months ago

0.1.56

5 months ago

0.1.57

5 months ago

0.1.55-beta.1

6 months ago

0.1.55-beta.2

6 months ago

0.1.50

6 months ago

0.1.55-beta.3

6 months ago

0.1.51

6 months ago

0.1.50-beta.52

6 months ago

0.1.50-beta.53

6 months ago

0.1.50-beta.54

6 months ago

0.1.50-beta.50

7 months ago

0.1.50-beta.51

7 months ago

0.1.56-beta.4

6 months ago

0.1.56-beta.3

6 months ago

0.1.56-beta.1

6 months ago

0.1.56-beta.9

6 months ago

0.1.56-beta.8

6 months ago

0.1.56-beta.7

6 months ago

0.1.56-beta.6

6 months ago

0.1.54-beta.2

6 months ago

0.1.56-beta.5

6 months ago

0.1.54-beta.1

6 months ago

0.1.53-beta.2

6 months ago

0.1.53-beta.1

6 months ago

0.1.50-beta.49

7 months ago

0.1.50-beta.47

7 months ago

0.1.50-beta.48

7 months ago

0.1.50-beta.41

7 months ago

0.1.50-beta.42

7 months ago

0.1.50-beta.43

7 months ago

0.1.50-beta.40

7 months ago

0.1.50-beta.36

7 months ago

0.1.50-beta.34

7 months ago

0.1.50-beta.33

7 months ago

0.1.50-beta.32

7 months ago

0.1.50-beta.31

7 months ago

0.1.50-beta.30

7 months ago

0.1.50-beta.27

7 months ago

0.1.50-beta.26

7 months ago

0.1.50-beta.25

7 months ago

0.1.50-beta.24

7 months ago

0.1.50-beta.23

7 months ago

0.1.50-beta.22

7 months ago

0.1.50-beta.21

7 months ago

0.1.50-beta.20

7 months ago

0.1.50-beta.19

7 months ago

0.1.50-beta.18

7 months ago

0.1.50-beta.17

7 months ago

0.1.50-beta.16

7 months ago

0.1.50-beta.15

7 months ago

0.1.50-beta.14

7 months ago

0.1.50-beta.13

7 months ago

0.1.50-beta.12

7 months ago

0.1.50-beta.10

7 months ago

0.1.50-beta.9

7 months ago

0.1.50-beta.8

7 months ago

0.1.50-beta.7

7 months ago

0.1.50-beta.6

7 months ago

0.1.50-beta.5

7 months ago

0.1.50-beta.4

7 months ago

0.1.50-beta.3

8 months ago

0.1.50-beta.2

8 months ago

0.1.50-beta.1

8 months ago

0.1.49

8 months ago

0.1.48-beta.1

8 months ago

0.1.47

8 months ago

0.1.47-beta.3

8 months ago

0.1.47-beta.2

8 months ago

0.1.47-beta.1

8 months ago

0.1.46

8 months ago

0.1.45

8 months ago

0.1.44-beta.13

8 months ago

0.1.44-beta.12

8 months ago

0.1.44-beta.11

8 months ago

0.1.44-beta.10

8 months ago

0.1.44-beta.9

8 months ago

0.1.44-beta.8

8 months ago

0.1.44-beta.6

8 months ago

0.1.44-beta.5

8 months ago

0.1.44-beta.4

8 months ago

0.1.44-beta.3

8 months ago

0.1.44-beta.2

8 months ago

0.1.44-beta.1

8 months ago

0.1.43

8 months ago

0.1.42

8 months ago

0.1.41

8 months ago

0.1.40

8 months ago

0.1.39

8 months ago

0.1.38

8 months ago

0.1.37

8 months ago

0.1.36

9 months ago

0.1.35

9 months ago

0.1.34

9 months ago

0.1.33

9 months ago

0.1.32

9 months ago

0.1.31

9 months ago

0.1.30

9 months ago

0.1.29

9 months ago

0.1.28

9 months ago

0.1.27

9 months ago

0.1.26

9 months ago

0.1.25

9 months ago

0.1.24

9 months ago

0.1.23

9 months ago

0.1.22

9 months ago

0.1.21

9 months ago

0.1.20

9 months ago

0.1.19

9 months ago

0.1.18

9 months ago

0.1.17

9 months ago

0.1.16

9 months ago

0.1.15

9 months ago

0.1.14

9 months ago

0.1.13

9 months ago

0.1.12

9 months ago

0.1.11

9 months ago

0.1.10

9 months ago

0.1.9

9 months ago

0.1.8

9 months ago

0.1.7

9 months ago

0.1.6

9 months ago

0.1.5

9 months ago

0.1.4

9 months ago

0.1.3

9 months ago

0.1.2

9 months ago

0.1.1

9 months ago

0.1.0

10 months ago

0.0.4

10 months ago

0.0.3

10 months ago

0.0.2

10 months ago

0.0.1

10 months ago

1.0.1

2 years ago

1.0.0

2 years ago