Skip to content

Commit

Permalink
Merge pull request #70 from formysister/development
Browse files Browse the repository at this point in the history
feature: BNB Beacon chain added
  • Loading branch information
moneroid authored Apr 8, 2024
2 parents ae64f85 + e60db02 commit 09b4418
Show file tree
Hide file tree
Showing 8 changed files with 196 additions and 15 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "multichain-wallet-sdk",
"version": "1.4.5",
"version": "1.4.6",
"description": "",
"main": "dist/index.js",
"types": "dist/index.d.ts",
Expand Down
21 changes: 12 additions & 9 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,35 +3,30 @@

### 📡Supported Network List
- EVM based networks
- Solana
- Solana
- BNB Beacon Chain

[Documentation](https://cybers-organization-5.gitbook.io/multichain-wallet-sdk-documentation)

### installation

```
npm install multichain-wallet-sdk
```

### import example (es5)

```javascript

const { EthereumWallet } = require('multichain-wallet-sdk');

```

### import example (es6)

```javascript

import { EthereumWallet } from 'multichain-wallet-sdk';

```

### functions(ethereum)
### functions(Ethereum)

- Create wallet
- Recover wallet from phrase words
Expand All @@ -53,7 +48,7 @@ import { EthereumWallet } from 'multichain-wallet-sdk';
- Get latency of JSON RPC endpoint (util function)
- GET latency of websocket endpoint (util function)

### functions(solana)
### functions(Solana)
- Create wallet
- Recover wallet from mnemonic phrase
- Get key pair from private key
Expand All @@ -67,6 +62,14 @@ import { EthereumWallet } from 'multichain-wallet-sdk';
- Get existing token list of network (util function)
- Get token detail from token address (util function)

### functions(BNB Beacon Chain)
- Create wallet
- Recover wallet from mnemonic phrase
- Recover account from private key
- Get BNB balance
- Send BNB
- Transfer tokens

Contribute [here](https://github.com/formysister/multichain-wallet-sdk/fork).
Submit issues [here](https://github.com/formysister/multichain-wallet-sdk/issues).

Expand Down
4 changes: 2 additions & 2 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { EthereumWallet, SolanaWallet } from "./wallet_class"
import { EthereumWallet, SolanaWallet, BinanceWallet } from "./wallet_class"

// export = ({ EthereumWallet })
export { EthereumWallet, SolanaWallet }
export { EthereumWallet, SolanaWallet, BinanceWallet }
141 changes: 141 additions & 0 deletions src/wallet_class/binance/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,141 @@
import {
generateMnemonic,
getPrivateKeyFromMnemonic,
getPublicKeyFromPrivateKey,
getAddressFromPublicKey
} from "@binance-chain/javascript-sdk/lib/crypto";

import { BncClient } from "@binance-chain/javascript-sdk/lib/client";

import { Wallet } from "../../type/type";

class BinanceWallet {

privateKey: string
address: string

constructor() {
const mnemonic = generateMnemonic();
const privateKey = getPrivateKeyFromMnemonic(mnemonic);
const publicKey = getPublicKeyFromPrivateKey(privateKey);
const address = getAddressFromPublicKey(publicKey, 'bnb');

this.privateKey = privateKey
this.address = address
}

/**
*
* @returns {Wallet}
*/
createWallet = (): Wallet => {
const mnemonic = generateMnemonic();
const privateKey = getPrivateKeyFromMnemonic(mnemonic);
const publicKey = getPublicKeyFromPrivateKey(privateKey);
const address = getAddressFromPublicKey(publicKey, 'bnb');

return {
mnemonic,
privateKey,
address
}
}

/**
*
* @param mnemonic
* @returns {Wallet}
*/
recoverWallet = (mnemonic: string): Wallet => {
const privateKey = getPrivateKeyFromMnemonic(mnemonic);
const publicKey = getPublicKeyFromPrivateKey(privateKey);
const address = getAddressFromPublicKey(publicKey, 'bnb');

return {
mnemonic,
privateKey,
address
}
}

/**
*
* @param privateKey
* @returns {Wallet}
*/
importAccount = (privateKey: string): Wallet => {
const publicKey = getPublicKeyFromPrivateKey(privateKey);
const address = getAddressFromPublicKey(publicKey, 'bnb');

return {
privateKey,
address
}
}

/**
*
* @param rpcUrl
* @param network
* @param address
* @returns {Promise<any>}
*/
getBalance = async (rpcUrl: string, network: 'mainnet' | 'testnet', address?: string): Promise<any> => {
try {
const client = new BncClient(rpcUrl)
client.chooseNetwork(network)

const balance = await client.getBalance(address || this.address)

if(balance.length <= 0 || balance == null || balance == undefined || !balance.filter((asset: any) => asset.symbol === 'BNB')) {
return 0
} else {
return balance.filter((asset: any) => {return asset.symbol === 'BNB'})[0].free
}
}
catch(error) {
throw error
}
}

/**
*
* @param rpcUrl
* @param fromAddress
* @param recipientAddress
* @param amount
* @param network
* @param privateKey
* @returns {Promise<{result: any, status: number}>}
*/
sendBNB = async (rpcUrl: string, fromAddress: string, recipientAddress: string, amount: any, network: 'mainnet' | 'testnet', privateKey?: string): Promise<{result: any, status: number}> => {
const client = new BncClient(rpcUrl)
client.chooseNetwork(network)
client.setPrivateKey(privateKey || this.privateKey)

const tx = await client.transfer(fromAddress, recipientAddress, amount, 'BNB')
return tx
}

/**
*
* @param rpcUrl
* @param fromAddress
* @param recipientAddress
* @param amount
* @param network
* @param asset
* @param privateKey
* @returns {Promise<{result: any, status: number}>}
*/
tokenTransfer = async (rpcUrl: string, fromAddress: string, recipientAddress: string, amount: any, network: 'mainnet' | 'testnet', asset: string, privateKey?: string): Promise<{result: any, status: number}> => {
const client = new BncClient(rpcUrl);
client.chooseNetwork(network);
client.setPrivateKey(privateKey || this.privateKey);

const tx = await client.transfer(fromAddress, recipientAddress, amount, asset);
return tx
}
}

export default BinanceWallet
1 change: 0 additions & 1 deletion src/wallet_class/ethereum/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import { mnemonicToSeed } from 'bip39';
import {
ERC721_INTERFACE_ID,
ERC1155_INTERFACE_ID,
ETHEREUM_DEFAULT
} from '../../constant';
// import ABI
import { erc20ABI, ecr721ABI, erc1155ABI } from '../../abi'
Expand Down
3 changes: 2 additions & 1 deletion src/wallet_class/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import EthereumWallet from "./ethereum"
import SolanaWallet from "./solana"
import BinanceWallet from "./binance"

export { EthereumWallet, SolanaWallet }
export { EthereumWallet, SolanaWallet, BinanceWallet }
5 changes: 5 additions & 0 deletions test/sample_data.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,11 @@ const SAMPLE_DATA = {
SAMPLE_ADDRESS: 'bc1qfgf0eukwjslus64vvcqt2l6t7e85hjp3e28lxp',
TESTNET_ADDRESS_P2PKH: 'mnGd3ZgszE3BJNH6sxVcvJhNRz3qnXQgS7',
TESTNET_ADDRESS_BECH32: 'tb1qfgf0eukwjslus64vvcqt2l6t7e85hjp3nvuvaj'
},
BEACON: {
SAMPLE_PRIVATE_KEY: "ca9a92085b6ea19fbe6bc4c369c5652cc4f1a2f3c71280c5b863d372f300d063",
SAMPLE_ADDRESS: "bnb1z79qgmv2e9xm6p24f2el0a2evxzyvnsnps0mvx",
SAMPLE_SERVER_URL: "https://dex.binance.org/"
}
}

Expand Down
34 changes: 33 additions & 1 deletion test/wallet.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// import EthereumWallet from '../src/wallet_class/ethereum'
import { EthereumWallet, SolanaWallet } from "../src"
import { EthereumWallet, SolanaWallet, BinanceWallet } from "../src"
import SAMPLE_DATA from './sample_data'

jest.setTimeout(50000)
Expand Down Expand Up @@ -159,6 +159,38 @@ describe("Wallet Test", () => {
})
})

describe("BNB Beacon wallet test", () => {
let beaconWallet: BinanceWallet

beforeAll(() => {
beaconWallet = new BinanceWallet()
})

it("Create wallet", () => {
const wallet = beaconWallet.createWallet()

expect(typeof wallet).toBe("object")
})

it("Recover wallet", () => {
const wallet = beaconWallet.recoverWallet(SAMPLE_DATA.COMMON.MNEMONIC)

expect(typeof wallet).toBe("object")
})

it("Import account", () => {
const account = beaconWallet.importAccount(SAMPLE_DATA.BEACON.SAMPLE_PRIVATE_KEY)

expect(typeof account).toBe("object")
})

it("Get balance", async () => {
const balance = await beaconWallet.getBalance(SAMPLE_DATA.BEACON.SAMPLE_SERVER_URL, "mainnet", SAMPLE_DATA.BEACON.SAMPLE_ADDRESS)

expect(typeof balance).toBe("string")
})
})

// describe("Bitcoin Wellet Test", () => {
// let bitcoinWallet: BitcoinWallet

Expand Down

0 comments on commit 09b4418

Please sign in to comment.