Skip to content

Commit 09b4418

Browse files
authored
Merge pull request #70 from formysister/development
feature: BNB Beacon chain added
2 parents ae64f85 + e60db02 commit 09b4418

File tree

8 files changed

+196
-15
lines changed

8 files changed

+196
-15
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "multichain-wallet-sdk",
3-
"version": "1.4.5",
3+
"version": "1.4.6",
44
"description": "",
55
"main": "dist/index.js",
66
"types": "dist/index.d.ts",

readme.md

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,35 +3,30 @@
33

44
### 📡Supported Network List
55
- EVM based networks
6-
- Solana
6+
- Solana
7+
- BNB Beacon Chain
78

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

1011
### installation
1112

1213
```
13-
1414
npm install multichain-wallet-sdk
15-
1615
```
1716

1817
### import example (es5)
1918

2019
```javascript
21-
2220
const { EthereumWallet } = require('multichain-wallet-sdk');
23-
2421
```
2522

2623
### import example (es6)
2724

2825
```javascript
29-
3026
import { EthereumWallet } from 'multichain-wallet-sdk';
31-
3227
```
3328

34-
### functions(ethereum)
29+
### functions(Ethereum)
3530

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

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

65+
### functions(BNB Beacon Chain)
66+
- Create wallet
67+
- Recover wallet from mnemonic phrase
68+
- Recover account from private key
69+
- Get BNB balance
70+
- Send BNB
71+
- Transfer tokens
72+
7073
Contribute [here](https://github.com/formysister/multichain-wallet-sdk/fork).
7174
Submit issues [here](https://github.com/formysister/multichain-wallet-sdk/issues).
7275

src/index.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { EthereumWallet, SolanaWallet } from "./wallet_class"
1+
import { EthereumWallet, SolanaWallet, BinanceWallet } from "./wallet_class"
22

33
// export = ({ EthereumWallet })
4-
export { EthereumWallet, SolanaWallet }
4+
export { EthereumWallet, SolanaWallet, BinanceWallet }

src/wallet_class/binance/index.ts

Lines changed: 141 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,141 @@
1+
import {
2+
generateMnemonic,
3+
getPrivateKeyFromMnemonic,
4+
getPublicKeyFromPrivateKey,
5+
getAddressFromPublicKey
6+
} from "@binance-chain/javascript-sdk/lib/crypto";
7+
8+
import { BncClient } from "@binance-chain/javascript-sdk/lib/client";
9+
10+
import { Wallet } from "../../type/type";
11+
12+
class BinanceWallet {
13+
14+
privateKey: string
15+
address: string
16+
17+
constructor() {
18+
const mnemonic = generateMnemonic();
19+
const privateKey = getPrivateKeyFromMnemonic(mnemonic);
20+
const publicKey = getPublicKeyFromPrivateKey(privateKey);
21+
const address = getAddressFromPublicKey(publicKey, 'bnb');
22+
23+
this.privateKey = privateKey
24+
this.address = address
25+
}
26+
27+
/**
28+
*
29+
* @returns {Wallet}
30+
*/
31+
createWallet = (): Wallet => {
32+
const mnemonic = generateMnemonic();
33+
const privateKey = getPrivateKeyFromMnemonic(mnemonic);
34+
const publicKey = getPublicKeyFromPrivateKey(privateKey);
35+
const address = getAddressFromPublicKey(publicKey, 'bnb');
36+
37+
return {
38+
mnemonic,
39+
privateKey,
40+
address
41+
}
42+
}
43+
44+
/**
45+
*
46+
* @param mnemonic
47+
* @returns {Wallet}
48+
*/
49+
recoverWallet = (mnemonic: string): Wallet => {
50+
const privateKey = getPrivateKeyFromMnemonic(mnemonic);
51+
const publicKey = getPublicKeyFromPrivateKey(privateKey);
52+
const address = getAddressFromPublicKey(publicKey, 'bnb');
53+
54+
return {
55+
mnemonic,
56+
privateKey,
57+
address
58+
}
59+
}
60+
61+
/**
62+
*
63+
* @param privateKey
64+
* @returns {Wallet}
65+
*/
66+
importAccount = (privateKey: string): Wallet => {
67+
const publicKey = getPublicKeyFromPrivateKey(privateKey);
68+
const address = getAddressFromPublicKey(publicKey, 'bnb');
69+
70+
return {
71+
privateKey,
72+
address
73+
}
74+
}
75+
76+
/**
77+
*
78+
* @param rpcUrl
79+
* @param network
80+
* @param address
81+
* @returns {Promise<any>}
82+
*/
83+
getBalance = async (rpcUrl: string, network: 'mainnet' | 'testnet', address?: string): Promise<any> => {
84+
try {
85+
const client = new BncClient(rpcUrl)
86+
client.chooseNetwork(network)
87+
88+
const balance = await client.getBalance(address || this.address)
89+
90+
if(balance.length <= 0 || balance == null || balance == undefined || !balance.filter((asset: any) => asset.symbol === 'BNB')) {
91+
return 0
92+
} else {
93+
return balance.filter((asset: any) => {return asset.symbol === 'BNB'})[0].free
94+
}
95+
}
96+
catch(error) {
97+
throw error
98+
}
99+
}
100+
101+
/**
102+
*
103+
* @param rpcUrl
104+
* @param fromAddress
105+
* @param recipientAddress
106+
* @param amount
107+
* @param network
108+
* @param privateKey
109+
* @returns {Promise<{result: any, status: number}>}
110+
*/
111+
sendBNB = async (rpcUrl: string, fromAddress: string, recipientAddress: string, amount: any, network: 'mainnet' | 'testnet', privateKey?: string): Promise<{result: any, status: number}> => {
112+
const client = new BncClient(rpcUrl)
113+
client.chooseNetwork(network)
114+
client.setPrivateKey(privateKey || this.privateKey)
115+
116+
const tx = await client.transfer(fromAddress, recipientAddress, amount, 'BNB')
117+
return tx
118+
}
119+
120+
/**
121+
*
122+
* @param rpcUrl
123+
* @param fromAddress
124+
* @param recipientAddress
125+
* @param amount
126+
* @param network
127+
* @param asset
128+
* @param privateKey
129+
* @returns {Promise<{result: any, status: number}>}
130+
*/
131+
tokenTransfer = async (rpcUrl: string, fromAddress: string, recipientAddress: string, amount: any, network: 'mainnet' | 'testnet', asset: string, privateKey?: string): Promise<{result: any, status: number}> => {
132+
const client = new BncClient(rpcUrl);
133+
client.chooseNetwork(network);
134+
client.setPrivateKey(privateKey || this.privateKey);
135+
136+
const tx = await client.transfer(fromAddress, recipientAddress, amount, asset);
137+
return tx
138+
}
139+
}
140+
141+
export default BinanceWallet

src/wallet_class/ethereum/index.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ import { mnemonicToSeed } from 'bip39';
88
import {
99
ERC721_INTERFACE_ID,
1010
ERC1155_INTERFACE_ID,
11-
ETHEREUM_DEFAULT
1211
} from '../../constant';
1312
// import ABI
1413
import { erc20ABI, ecr721ABI, erc1155ABI } from '../../abi'

src/wallet_class/index.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import EthereumWallet from "./ethereum"
22
import SolanaWallet from "./solana"
3+
import BinanceWallet from "./binance"
34

4-
export { EthereumWallet, SolanaWallet }
5+
export { EthereumWallet, SolanaWallet, BinanceWallet }

test/sample_data.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,11 @@ const SAMPLE_DATA = {
1919
SAMPLE_ADDRESS: 'bc1qfgf0eukwjslus64vvcqt2l6t7e85hjp3e28lxp',
2020
TESTNET_ADDRESS_P2PKH: 'mnGd3ZgszE3BJNH6sxVcvJhNRz3qnXQgS7',
2121
TESTNET_ADDRESS_BECH32: 'tb1qfgf0eukwjslus64vvcqt2l6t7e85hjp3nvuvaj'
22+
},
23+
BEACON: {
24+
SAMPLE_PRIVATE_KEY: "ca9a92085b6ea19fbe6bc4c369c5652cc4f1a2f3c71280c5b863d372f300d063",
25+
SAMPLE_ADDRESS: "bnb1z79qgmv2e9xm6p24f2el0a2evxzyvnsnps0mvx",
26+
SAMPLE_SERVER_URL: "https://dex.binance.org/"
2227
}
2328
}
2429

test/wallet.test.ts

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
// import EthereumWallet from '../src/wallet_class/ethereum'
2-
import { EthereumWallet, SolanaWallet } from "../src"
2+
import { EthereumWallet, SolanaWallet, BinanceWallet } from "../src"
33
import SAMPLE_DATA from './sample_data'
44

55
jest.setTimeout(50000)
@@ -159,6 +159,38 @@ describe("Wallet Test", () => {
159159
})
160160
})
161161

162+
describe("BNB Beacon wallet test", () => {
163+
let beaconWallet: BinanceWallet
164+
165+
beforeAll(() => {
166+
beaconWallet = new BinanceWallet()
167+
})
168+
169+
it("Create wallet", () => {
170+
const wallet = beaconWallet.createWallet()
171+
172+
expect(typeof wallet).toBe("object")
173+
})
174+
175+
it("Recover wallet", () => {
176+
const wallet = beaconWallet.recoverWallet(SAMPLE_DATA.COMMON.MNEMONIC)
177+
178+
expect(typeof wallet).toBe("object")
179+
})
180+
181+
it("Import account", () => {
182+
const account = beaconWallet.importAccount(SAMPLE_DATA.BEACON.SAMPLE_PRIVATE_KEY)
183+
184+
expect(typeof account).toBe("object")
185+
})
186+
187+
it("Get balance", async () => {
188+
const balance = await beaconWallet.getBalance(SAMPLE_DATA.BEACON.SAMPLE_SERVER_URL, "mainnet", SAMPLE_DATA.BEACON.SAMPLE_ADDRESS)
189+
190+
expect(typeof balance).toBe("string")
191+
})
192+
})
193+
162194
// describe("Bitcoin Wellet Test", () => {
163195
// let bitcoinWallet: BitcoinWallet
164196

0 commit comments

Comments
 (0)