From ebfd0efcfbd00b14d95fa13c2afb4bcc84dd1b5f Mon Sep 17 00:00:00 2001 From: jejolare Date: Fri, 26 Jul 2024 16:22:41 +0700 Subject: [PATCH] update readme --- README.md | 276 ++++++++++++++++++++++++++++++++++++------------------ 1 file changed, 184 insertions(+), 92 deletions(-) diff --git a/README.md b/README.md index b3440ce..0c9bd86 100644 --- a/README.md +++ b/README.md @@ -25,12 +25,14 @@ or yarn add zano_web3 ``` +# WEB API (extension): + ## Usage ### Importing the Library ```typescript -import ZanoWallet from 'zano_web3'; +import ZanoWallet from 'zano_web3/web'; ``` ### Creating a ZanoWallet Instance @@ -164,99 +166,189 @@ export interface Wallet { } ``` - -## Server-Side Validator - -The server-side validator function, `validateWallet`, is used to validate wallet authentication data using the Zano RPC API. It supports authentication using either an alias or a public key. - -### Usage - -The function `validateWallet` accepts a `rpcUrl` for the Zano node and an `AuthData` object containing the authentication details. - -```typescript -import validateWallet from './validateWallet'; - -const authData = { - address: 'wallet_address', - signature: 'signed_message', - message: 'original_message', - alias: 'user_alias' // or pkey: 'public_key' -}; - -const rpcUrl = 'https://zano-node.example.com'; - -validateWallet(rpcUrl, authData) - .then(valid => { - if (valid) { - console.log('Wallet is valid'); - } else { - console.log('Invalid wallet data'); - } - }); -``` - -### AuthData Types - -The `AuthData` type is a union of `AliasAuth` and `PkeyAuth` interfaces: - -```typescript -interface BaseAuthData { - address: string; - signature: string; - message: string; -} - -interface AliasAuth extends BaseAuthData { - alias: string; -} - -interface PkeyAuth extends BaseAuthData { - pkey: string; -} - -type AuthData = AliasAuth | PkeyAuth; -``` - -### Internal Validation Logic - -The `validateWallet` function internally uses the Zano RPC method `validate_signature` to verify the authenticity of the provided signature against the message. If an alias is provided, it also checks that the alias resolves to the correct wallet address. - -### Functions - -#### `validateWallet(rpcUrl: string, authData: AuthData)` - -- `rpcUrl`: The URL of the Zano RPC node. -- `authData`: The authentication data, which includes the address, signature, message, and optionally alias or public key. - -Returns `true` if the wallet data is valid, otherwise returns `false`. - -#### Example - -```typescript -const authData = { - address: 'wallet_address', - signature: 'signed_message', - message: 'original_message', - alias: 'user_alias' // or pkey: 'public_key' -}; - -const rpcUrl = 'https://zano-node.example.com'; - -validateWallet(rpcUrl, authData) - .then(valid => { - if (valid) { - console.log('Wallet is valid'); - } else { - console.log('Invalid wallet data'); - } - }); -``` - - ## Requirements - ZanoWallet browser extension must be installed. -## Contributing -If you find any issues or want to contribute, please create a pull request or submit an issue. +# Server api (Wallet RPC, Daemon): + +#### Methods + +- `updateWalletRpcUrl(rpcUrl: string)`: Updates the wallet RPC URL. +- `updateDaemonRpcUrl(rpcUrl: string)`: Updates the daemon RPC URL. +- `getAssetsList()`: Retrieves the list of assets. +- `getAssetDetails(assetId: string)`: Retrieves details of a specific asset. +- `getAssetInfo(assetId: string)`: Retrieves info of a specific asset. +- `sendTransfer(assetId: string, address: string, amount: string)`: Sends a transfer to an address. +- `getBalances()`: Retrieves the balances. +- `validateWallet(rpcUrl: string, authData: AuthData)`: Validates the wallet. + + +#### 1. **Updating Wallet RPC URL** + +```javascript +import { ServerWallet } from "zano_web3/server"; + +(async () => { + const zanoServerAPI = new ServerWallet({ + walletUrl: "http://127.0.0.1:11211/json_rpc", + daemonUrl: "http://127.0.0.1:11211/json_rpc" + }); + + // Update the wallet RPC URL + await zanoServerAPI.updateWalletRpcUrl("http://new_wallet_url:11211/json_rpc"); + + console.log("Wallet RPC URL updated."); +})(); +``` + +#### 2. **Updating Daemon RPC URL** + +```javascript +import { ServerWallet } from "zano_web3/server"; + +(async () => { + const zanoServerAPI = new ServerWallet({ + walletUrl: "http://127.0.0.1:11211/json_rpc", + daemonUrl: "http://127.0.0.1:11211/json_rpc" + }); + + // Update the daemon RPC URL + await zanoServerAPI.updateDaemonRpcUrl("http://new_daemon_url:11211/json_rpc"); + + console.log("Daemon RPC URL updated."); +})(); +``` + +#### 3. **Getting the List of Assets** + +```javascript +import { ServerWallet } from "zano_web3/server"; + +(async () => { + const zanoServerAPI = new ServerWallet({ + walletUrl: "http://127.0.0.1:11211/json_rpc", + daemonUrl: "http://127.0.0.1:11211/json_rpc" + }); + + // Get the list of assets + const assets = await zanoServerAPI.getAssetsList(); + + console.log("Assets List:", assets); +})(); +``` + +#### 4. **Getting Asset Details** + +```javascript +import { ServerWallet } from "zano_web3/server"; + +(async () => { + const zanoServerAPI = new ServerWallet({ + walletUrl: "http://127.0.0.1:11211/json_rpc", + daemonUrl: "http://127.0.0.1:11211/json_rpc" + }); + + // Get details of a specific asset by ID + const assetId = "example-asset-id"; + const assetDetails = await zanoServerAPI.getAssetDetails(assetId); + + console.log(`Details for Asset ID ${assetId}:`, assetDetails); +})(); +``` + +#### 5. **Getting Asset Info** + +```javascript +import { ServerWallet } from "zano_web3/server"; + +(async () => { + const zanoServerAPI = new ServerWallet({ + walletUrl: "http://127.0.0.1:11211/json_rpc", + daemonUrl: "http://127.0.0.1:11211/json_rpc" + }); + + // Get info for a specific asset by ID + const assetId = "example-asset-id"; + const assetInfo = await zanoServerAPI.getAssetInfo(assetId); + + console.log(`Info for Asset ID ${assetId}:`, assetInfo); +})(); +``` + +#### 6. **Sending a Transfer** + +```javascript +import { ServerWallet } from "zano_web3/server"; + +(async () => { + const zanoServerAPI = new ServerWallet({ + walletUrl: "http://127.0.0.1:11211/json_rpc", + daemonUrl: "http://127.0.0.1:11211/json_rpc" + }); + + // Send a transfer + const assetId = "example-asset-id"; + const address = "recipient-address"; + const amount = "10.5"; // in asset units + + try { + const transferResult = await zanoServerAPI.sendTransfer(assetId, address, amount); + console.log("Transfer successful:", transferResult); + } catch (error) { + console.error("Transfer failed:", error.message); + } +})(); +``` + +#### 7. **Getting Balances** + +```javascript +import { ServerWallet } from "zano_web3/server"; + +(async () => { + const zanoServerAPI = new ServerWallet({ + walletUrl: "http://127.0.0.1:11211/json_rpc", + daemonUrl: "http://127.0.0.1:11211/json_rpc" + }); + + // Get the balances + const balances = await zanoServerAPI.getBalances(); + + console.log("Balances:", balances); +})(); +``` + +#### 8. **Validating a Wallet** + +```javascript +import { ServerWallet } from "zano_web3/server"; +import { AuthData } from "./types"; + +(async () => { + const zanoServerAPI = new ServerWallet({ + walletUrl: "http://127.0.0.1:11211/json_rpc", + daemonUrl: "http://127.0.0.1:11211/json_rpc" + }); + + // Validate wallet using AuthData + const authData: AuthData = { + message: "message to sign", + address: "wallet-address", + signature: "signature", + alias: "wallet-alias" + }; + + try { + const isValid = await zanoServerAPI.validateWallet("http://127.0.0.1:11211/json_rpc", authData); + console.log("Wallet validation:", isValid ? "Valid" : "Invalid"); + } catch (error) { + console.error("Validation failed:", error.message); + } +})(); +``` + +## Requirements + +- Correct RPC URLs for the wallet and daemon. \ No newline at end of file