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
0 commit comments