1.1.7 • Published 11 months ago

@zbyteio/zbyte-wallet-sdk-core v1.1.7

Weekly downloads
-
License
ISC
Repository
github
Last release
11 months ago

zbyte Wallet core SDK

SDK contains all the core functionality related to zbyte wallet, and can be supported on browser(client side code).

Installation

  • Authenticate to npm github registry
    npm login --registry=https://npm.pkg.github.com --scope=@zbyteio
  • It will prompt for the below details
    • Username
      • Use the github username
    • Password
      • Use github PAT (Personal Access token)
    • EmailId
      • Use github email Id
  • Then install the lib use npm below npm command
    npm install @zbyteio/zbyte-wallet-sdk-core

Documentation

  • zbyte core wallet manages the user account creation and worked with DPLAT token.
  • User can see the current DPLAT balance and transfer it to another account.
  • Supported blockchain's are polygon, avalanche c-chain hedera and base-chain.
  • All the key-pairs are managed by the other third-party libraries which we termed as external key provider(EKP).
  • Currently supported external key provider(EKP) are Web3auth and Metamask.
  • Since DPLAT Token is not a L1 token, zbyte wallet defined some mandatory interfaces which are mentioned below:

    interface IWalletProvider {
    	/**
    	 * @description Use to connect the wallet provider.
    	 */
    	connect(): Promise<any>;
    	/**
    	 * @description Use to checked whether wallet provider connected or not.
    	 */
    	isConnected(): boolean;
    	/**
    	 * @description fetch the wallet provider which internally set the provider
    	 * @param networkConfig Blockchain Network parameters
    	 */
    	getKeyProvider(networkConfig: NetworkConfig): Promise<IKeyProvider>;
    }
    
    interface IKeyProvider {
    	/**
    	 * @description Name of the key provider
    	 */
    	readonly serviceProviderName: string;
    	/**
    	 * @description Add Network to the given key provider
    	 * @param networkConfig Network Information of the blockchain
    	 */
    	addChain(networkConfig: NetworkConfig): Promise<void>;
    	/**
    	 * @description Switch default network to the given key provider,
    	 *              switchChain only supported on the added chain.
    	 * @param chainId chainId in hex string format e.g 0x89(137)
    	 */
    	switchChain(chainId: string): Promise<void>;
    	getProvider(): any;
    }
  • Then below are some important apis to interact with wallet:

    • IsConnected()
      • Check whether wallet is connected to underneath key provider like web3auth or metamask.
    • Connect()
      • Connect to underneath key provider.
    • SignTypedData(txnMessage: string, chainId: number)
      • Sign the EIP-712 transaction.
      • Inputs:
        • txnMessage: Serialized EIP-712 message
        • chainId: blockchain id which need the signature
    • BatchSignTypedData(txnBatch: UnsignedBatchTx): Promise<OperationSign[]>;
      • Its used when we need multiple transaction to be signed.
    • GetAddress()
      • Return the blockchain address for the given user.
    • GetTokenBalance(address: string)
      • Provide the zbyte token present in the given user's address.
      • Input:
        • address: User's blockchain address

Usage

  • Please refer the below code for consuming the wallet sdk:

        const web3Auth = new Web3AuthProvider();
        const metamask = new MetaMaskProvider();
    	const walletProvider: IWalletProvider = web3Auth || metamask;
    
        const wallet = new WalletCore(walletProvider, getBlockchainNetwork(CHAIN_ID_MATIC_TESTNET));
    
        // For using web3auth which is the default and preferred provider
        // requires extra authentication function
        wallet.injectAuthVerifier({
            clientId: string;
            domain: string;
            typeOfLogin?: LOGIN_TYPE;
            verifier: string;
            accessToken: string;
            tokenExpiry?: number;
            typeOfToken?: string;
        });
    
        if (!wallet.isConnected()) {
            console.log(await wallet.connect()
            .then((result) => {
                console.log("initialized")
                return result;
            })
            .catch(e => console.error(e)));
        }
    
        const userAddress = await wallet.getAddress();
        const dplatBalance = await wallet.getTokenBalance(userAddress);