-
-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #70 from formysister/development
feature: BNB Beacon chain added
- Loading branch information
Showing
8 changed files
with
196 additions
and
15 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters