forked from trangmaiq/golden-wallet-react-native
-
Notifications
You must be signed in to change notification settings - Fork 0
/
starypto.doc.js
176 lines (149 loc) · 4.97 KB
/
starypto.doc.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
import Starypto from './Libs/react-native-starypto'
// {
// mnemonic,
// ethIndex: 0,
// ethWallet: [{
// name: 'Viet',
// address: '0x728372187ab'
// pk: '0x121212'
// }]
// }
const mnemonic = 'away resist vote symbol forward puppy upon mesh race give escape bamboo'
function generateMasterNode() {
return Starypto.generateMnemonic().then((masterNode) => {
console.log(masterNode.mnemonic)
return masterNode
})
}
function getMasterNodeFromMnemonic() {
return Starypto.fromMnemonic(mnemonic)
}
function createNewETHWallet(masterNode, index = 0, network = 'mainnet') {
const childNode = masterNode.derivePathWithCoinType(Starypto.coinTypes.ETH).derive(index)
const wallet = Starypto.fromHDNode(childNode, network)
console.log('New ETH Wallet: ', wallet.address, wallet.privateKey, wallet.publicKey)
return wallet
}
function createNewETHWalletTest(masterNode, index = 0, network = 'mainnet') {
masterNode.derivePathWithCoinTypeAsync(Starypto.coinTypes.ETH)
.then(node => node.derive(index))
.then(node => Starypto.fromHDNode(node, network))
}
function getETHWalletWithAddress(address, network = 'mainnet') {
return new Starypto.Wallet({ coinType: Starypto.coinTypes.ETH, network }, null, address)
}
/*
Network: string (mainnet || ropsten || rinkeby || kovan)
privateKey: string
*/
function importETHWalletFromPrivateKey(privateKey, network = 'mainnet') {
const walletETHFromPK = Starypto.fromPrivateKey(privateKey, Starypto.coinTypes.ETH, network)
console.log('Import ETH Wallet from PrivateKey: ', walletETHFromPK.address, walletETHFromPK.privateKey, walletETHFromPK.publicKey)
return walletETHFromPK
}
// wallet: {
// address: 03903904,
// privateKey: eiheihfeihfi,
// publictKey: efheufheuf
// }
function getETHWalletBalance(wallet) {
wallet.initProvider('Infura', 'qMZ7EIind33NY9Azu836')
wallet.getBalance().then((res) => {
console.log(`${wallet.address} Balance:`, Starypto.Units.formatEther(res))
})
}
function canSendTransaction(wallet) {
return wallet.canSendTransaction()
}
function sendTransaction(wallet, toAddress, ethString = '0.001') {
const transaction = {
to: toAddress,
value: Starypto.Units.parseEther(ethString)
}
wallet.initProvider('Infura', 'qMZ7EIind33NY9Azu836')
wallet.provider.estimateGas(transaction).then((res) => {
transaction.gasLimit = res
wallet.sendTransaction(transaction).then(tx => console.log(tx))
})
}
function hashPassword(password) {
return Starypto.hashPassword(password)
}
/**
import { randomBytes } from 'react-native-randombytes'
@example
let data = {
Mnemonic: "strong dad page loud fence increase can major knock dinosaur pilot jeans"
PrivateKey: "0xfeaec43f655ff69cd57971f821f809efd9b8e96f031664ae74172f7fa2fd943a",
PublicKey: "0x02b22f81e6a4adaaf3d913ae8c51571c20804e2b13f1a78eb892da8e8f1d745568",
Address: "0x477B8B29849A7e1B46Ff13DD5E89C13d4060752c"
}
dataString = JSON.stringify(data)
// Generate iv ( MUST STORE IV )
const iv = randomBytes(16).toString('hex').slice(0, 16)
**/
function encryptDataAES256(dataString, password, iv, algorithm = 'aes-256-cbc') {
// dataEncrypted:
return Starypto.encryptString(dataString, password, iv, algorithm)
}
function decryptDataAES256(dataEncrypted, password, iv, algorithm = 'aes-256-cbc') {
// dataDecrypted:
return Starypto.decryptString(dataEncrypted, password, iv, algorithm)
}
function sendERC20Token(
tokenAddress = '0xf230b790E05390FC8295F4d3F60332c93BEd42e2',
toAddress = '0x929a19641205817ecf9d7c17194fcb7ab656276f',
valueStr = '1.0',
wallet
) {
Starypto.ContractUtils.parseContract(tokenAddress)
.then((contract) => {
// set provider if not set
wallet.initProvider('Infura', 'qMZ7EIind33NY9Azu836')
// Calculate value token to send
const numberOfDecimals = contract.tokenInfo.decimals
const numberOfTokens = Starypto.Units.parseUnits(valueStr, numberOfDecimals)
const ct = new Starypto.Contract(tokenAddress, contract.abi, wallet)
// tx is transaction hash
ct.transfer(toAddress, numberOfTokens).then(tx => console.log(tx))
})
}
function getNEOWalletBalance(wallet) {
wallet.initProvider('CityOfZion')
wallet.getBalance().then((res) => {
console.log(`${wallet.address} NEO: ${res.NEO.balance} ; GAS: ${res.GAS.balance}`)
})
}
/**
* @param
* const transaction = {
to: 'AKcvyzZVTgv5EC9h4FS5Mjsxbr1VwmysNx', (toAddress)
value: 0.1,
assetName: 'GAS' || 'NEO'
}
return last transaction ID
*/
function sendNEOTransaction(wallet, transaction) {
wallet.initProvider('CityOfZion')
wallet.sendTransaction(transaction).then((res) => {
console.log(res)
})
}
export default {
mnemonic,
generateMasterNode,
getMasterNodeFromMnemonic,
createNewETHWallet,
getETHWalletWithAddress,
importETHWalletFromPrivateKey,
getETHWalletBalance,
canSendTransaction,
sendTransaction,
encryptDataAES256,
decryptDataAES256,
hashPassword,
sendERC20Token,
getNEOWalletBalance,
sendNEOTransaction,
createNewETHWalletTest
}