0.0.7 • Published 1 year ago
naka-connect-js v0.0.7
naka-connect-js
Naka Connect
Naka connect is a simple javascript library that connects apps to Naka Wallet to retrieve user wallet addresses and sign transactions.
Installation
With Yarn:
yarn add https://github.com/TrustlessComputer/naka-connect-js
const CONNECT_URL = 'https://api.nakachain.xyz';
const WALLET_URL = 'https://nakachain.xyz/signer';Connect wallet
1. Example code
import * as nakaConnect from "naka-connect-js";
const connector = new nakaConnect.DappConnect(CONNECT_URL, WALLET_URL);
try {
const accounts = await connector.requestAccount({
target: "popup",
});
console.log("accounts", accounts);
} catch (e) {
// todo handle error
// Reject | Create transaction error
}2. Request
target: window.open type"_blank" | "_parent" | "_self" | "_top" | "popup"
3. Response
accounts: Wallet's list of available accounts.address: The account name.
Sign Message
1. Example code
import * as nakaConnect from "naka-connect-js";
const connector = new nakaConnect.DappConnect(CONNECT_URL, WALLET_URL);
try {
const resp = await connection.requestSignMessage({
target: "popup",
signMessage: "Hello world.",
})
console.log("sign response", resp);
} catch (e) {
// todo handle error
// Reject | Create transaction error
}2. Request
target: window.open type"_blank" | "_parent" | "_self" | "_top" | "popup"
3. Response
accounts: Wallet's list of available accounts.address: The account name.
signature: The signature is signed by firstaddress.
Sign Transaction
Contract Integration
1. Define ABI
const ABI = [
{
"inputs": [
{
"internalType": "contract WrappedToken",
"name": "token",
"type": "address"
},
{
"internalType": "uint256",
"name": "amount",
"type": "uint256"
},
{
"internalType": "string",
"name": "externalAddr",
"type": "string"
}
],
"name": "burn",
"outputs": [],
"stateMutability": "nonpayable",
"type": "function"
},
];2: Create calldata
import { ethers } from "ethers";
const ContractInterface = new ethers.utils.Interface(ABI);
const encodeAbi = ContractInterface.encodeFunctionData("burn", [
tokenAddress,
burnAmount,
receiver
]);3. Request sign transaction
import * as nakaConnect from "naka-connect-js";
const connector = new nakaConnect.DappConnect(CONNECT_URL, WALLET_URL);
const toAddress = "0xd07a71011d5eDF06743EBd6AD848FE0C13D2DB2b";
const response = await connector.requestSign({
target: "popup",
calldata: encodeAbi,
to: toAddress,
value: "",
functionType: 'Burn',
functionName: 'burn(address,uint256,string)',
chainType: 'NAKA',
});
console.log('Sign transaction response: ', response);4. Request Payload
target: window.open type"_blank" | "_parent" | "_self" | "_top" | "popup"calldata: calldata for sign and send transaction.to: destination address.value:optionalvalue NativeToken sending transaction. // 1e18 = 1 BVMchainType: NAKA | RUNE | EAI | string. // or chainIDaccountAbstraction:ObjectpaymasterAddress: string;tokenAddress: string;tokenFeeAddress: string;
5. Response Payload
hash: Transaction hash.to:optionaldestination address.from: sender address.