connect to trustwallet and get bsc, eth, btc addresses and balances - ethereum

right now I use this code:
https://github.com/Web3Modal/web3modal-vanilla-js-example
but it connects only eth address from trust wallet
I tried to change provider options to this:
const providerOptions = {
walletconnect: {
package: WalletConnectProvider,
options: {
rpc: {
56: "https://bsc-dataseed.binance.org/"}
}
},
But didn't work
In my trust wallet I have btc address, eth address, bnb address and I want to know all this addresses and balances. But right now I can only get eth address
I tried to do this:
const chainId = 56//await web3.eth.getChainId();
// Get list of accounts of the connected wallet
const accounts = await web3.eth.getAccounts();
But this gives me only eth accounts.
So what do I do?

You need to ask the user to change the chain in the wallet itself to get access to accounts on different chains. Currently there is no API or way to force the user t change a chain.

In my case, it worked on Trust wallet, and did not work on metamask mobile app:
walletconnect: {
package: WalletConnectProvider, // required
options: {
rpc: {
56: 'https://bsc-dataseed.binance.org/',
97: 'https://data-seed-prebsc-1-s1.binance.org:8545/',
},
network: "binance", // --> this will be use to determine chain id 56
},
},
I'm not sure the bellow code is required:
web3Modal = new Web3Modal({
network: 'binance', // optional
cacheProvider: true,
providerOptions, // required
})
You can find out how to configure by reading the source code directly:
// Select BSC work on Trust wallet but dont work on metamask
https://github.com/Web3Modal/web3modal/blob/72596699b97d231dfaa5ef04110b61b8dc77d57d/src/providers/connectors/walletconnect.ts#L30
https://github.com/Web3Modal/web3modal/blob/72596699b97d231dfaa5ef04110b61b8dc77d57d/src/helpers/utils.ts#L198

Related

Is there an API for ethereum address monitoring? Or how to get notifications when an address is paided

Consider the simple scenario: some address transfers 100 usdt to address A; is there a method or api to watch on address A to get automated notifications? What is the best practice for these payin notifications of many (~100+) addresses?
You can subscribe to Transfer event logs emitted by the USDT contract. As defined in the ERC-20 standard, the second param of the event log is the recipient address.
Example using web3js:
const options = {
address: "0xdAC17F958D2ee523a2206206994597C13D831ec7", // USDT contract on Ethereum
topics: [
web3.utils.keccak256("Transfer(address,address,uint256)"), // hash of the event definition
null, // any sender
["0xFFfFfFffFFfffFFfFFfFFFFFffFFFffffFfFFFfF"] // recipient addresses
]
};
web3.eth.subscribe("logs", options, (err, data) => {
console.log(data);
});

blockchain tutorial Error: The send transactions "from" field must be defined

I am following a blockchain tutorial from dappuniversity.
When I create the task in the line
await App.todoList.createTask(content)
from line
https://github.com/dappuniversity/eth-todo-list/blob/e3ed9a2cefb581c09730250c56c9d30a19cc63c8/src/app.js#L115
I get the following error :
Uncaught (in promise) Error: The send transactions "from" field must be defined!
at Method.inputTransactionFormatter (truffle-contract.js:50747)
at truffle-contract.js:51228
at Array.map (<anonymous>)
at Method.formatInput (truffle-contract.js:51226)
at Method.toPayload (truffle-contract.js:51261)
at Eth.send [as sendTransaction] (truffle-contract.js:51551)
Do I need to define a 'from' field somewhere?
With the latest dependencies, none of the above answers were working for me.
I had to edit the loadAccount method:
loadAccount: async () => {
// Set the current blockchain account
const accounts = await web3.eth.getAccounts();
App.account = accounts[0];
},
then pass the App's account to the createTask method: await App.todoList.createTask(content, { from: App.account })
I was running into issues with the above answer which appears due to recent Metamask updates and that moving from web3.currentProvider to window.ethereum
I was able to make this work using
await App.todoList.createTask(content, {from: App.account})
I am using the latest truffle/contract. I needed to specify the from account in my createTask method like the following:
await App.todoList.createTask(content, { from: web3.eth.defaultAccount})
This worked.
Same happened to me on Remix while interacting with the contract deployed on Rinkeby. I chose injected web3 for the environment, But account field was empty.
It happended because I rejected the connection to metamask first and then did not get any other request to connect with metamask. So I refreshed remix, locked and unlocked metamask. It sent request to connect to metamask

Default account for Ropsten deployment using truffle-hd-wallet

I'm trying to deploy a custom ERC20 Token to Ropsten network by using truffle-hd-wallet. The transaction was went well, however, the weird thing was the newly deployed custom ERC20 token main holder account it wasn't my desired account but an unknown account. When I added the token to my own Ropsten account the amount is ZERO which supposed to have the initial values. Is there a way I can set my desire deployment contract address to the truffle? Please advise.
Thank you.
Desire Address : 0xd61794624e9542495A72Cfac7Cc10B4275b8f8E5
Actual Address : 0xEDD4C3676c8579D25463040fd196626a9B7C60a2
ropsten: {
provider: function() {
return new HDWalletProvider(MNEMONIC, "https://ropsten.infura.io/v3/" + INFURA_API_KEY, 0);
},
network_id: 3,
gas: 4700000
}
},
module.exports = function(deployer) {
deployer.deploy(CToken).then(function () {
let walletA = walletAaddr;
let walletB = walletBaddr;
return deployer.deploy(
CTokenSale,
CToken.address,
walletA,
walletB
).then(function () {
// token ownership setting
CToken.deployed().then(function(instance) {
let fptc = instance;
return fptc.transferOwnership(CTokenSale.address, {gas:1000000});
}).then(function(result) {
console.log("transferOwnership successful!")
}).catch(function(e) {
console.log("Ownership Transfer failed!")
});
CToken.deployed().then(function(instance) {
let fptc = instance;
return fptc.transfer(CTokenSale.address, 100, {gas:1000000});
}).then(function(result) {
console.log("Sales Token Ready!")
}).catch(function(e) {
console.log("Sales Token failed to deploy!")
});
});
});
};
You haven't included your token smart contract code. I assume that the account deploying the tokens either is given the initial supply, or has a minter role.
I suggest you confirm that the first account derived from your 12 word seed phrase (mnemonic) is the address that you want: e.g. 0xd61794624e9542495A72Cfac7Cc10B4275b8f8E5.
You are specifying the first account with 0 as the address_index.
With Truffle HDWallet Provider you can specify a derivation path if you are using something different from the default.
https://github.com/trufflesuite/truffle/tree/develop/packages/truffle-hdwallet-provider
I recommend reading the OpenZeppelin documentation (if you haven't already) on:
Tokens: https://docs.openzeppelin.org/v2.3.0/tokens
Crowdsales: https://docs.openzeppelin.org/v2.3.0/crowdsales
You can also ask questions at:
Ethereum Stack Exchange: https://ethereum.stackexchange.com/
Zeppelin Community Forum: https://forum.zeppelin.solutions/

web3.eth.accounts.create method doesn't actually create new account

I try to create an eth account via RPC in private network.
What I have done so far are:
launch geth node and create private network.
create simple javascript program using web3 1.0.0, typescript
run and get result as below but the account isn't created
Code:
const result = await web3.eth.personal.unlockAccount(senderId, senderPassword, duration)
if (result === true) {
// const newAccountResult = await web3.eth.personal.newAccount('password')
const newAccountResult = await web3.eth.accounts.create('user01')
console.log(newAccountResult)
}
Result:
web3.eth.accounts.create returns the following result
{ address: '0xf10105f862C1cB10550F4EeB38697308c7A290Fc',
privateKey: '0x5cba6b397fc8a96d006988388553ec17a000f7da9783d906979a2e1c482e7fcb',
signTransaction: [Function: signTransaction],
sign: [Function: sign],
encrypt: [Function: encrypt] }
But web3.eth.getAccounts method returns only 1 account.
[ '0xaf0034c41928Db81E570061c58c249f61CFF57f2' ]
Seems web3.eth.accounts.create method has succeeded as the result includes account address and private key.
But I don't understand why web3.eth.getAccounts method doesn't include the created account.
I also checked geth via console, the result is same.
> eth.accounts
["0xaf0034c41928db81e570061c58c249f61cff57f2"]
And eth.personal.newAccount didn't work.
Do I need to do something after web3.eth.accounts.create?
I appreciate any help.
If i got it right, web.eth.accounts.create is a way to create accounts without storing them on the local node, so its basically a way to get a valid keypair on-the-fly without storing anything on the keystore)
web3.eth.personal.newAccount() should be availabel if you have the personal-API activated on your geth node (which is default behavior for ganache, with geth you need to activate it via geth --dev/testnet --rpc --rpcapi eth,web3,personal (note: of course you should be very careful with allowing personal-API on mainnet, make sure that RPC access is restricted so only you/privileged users can access it)
(async () => {
let newAccount = await web3.eth.personal.newAccount();
console.log(newAccount);
let accounts = await web3.eth.getAccounts();
console.log(accounts);
})();
Should give something like
0xb71DCf0191E2B90efCD2638781DE40797895De66
[
...
'0xb71DCf0191E2B90efCD2638781DE40797895De66' ]
Refs https://medium.com/#andthentherewere0/should-i-use-web3-eth-accounts-or-web3-eth-personal-for-account-creation-15eded74d0eb

How to prevent multiple logins in mobile applications

If we want to prevent multiple logins with same credentials in mobile application how can we do that and we do not have any sessions in mobile so can we do with token based authentication please give me some ideas how to do this.
I am implementing ionic 2 application with back end as node js. I would be very grateful to get suggestions. Thank you in advance.
You can save every login connection and disconnect all previously saved login every time a new connection is created.
If you want to restrict multiple logins on same device and same application then
Two possible solutions:
1.
you can use device uuid Device plugin
Device uuid will be unique. (Don't use device uuid as authentication token)
Install it:
$ ionic plugin add cordova-plugin-device
ans usage
import { Device } from 'ionic-native';
console.log('Device UUID is: ' + Device.uuid);
On login you can use token base authentication.when ever user login save token in localStorage.and when ever app open (index page) check if token is present and still valid than takes user to dashboard otherwise to the login page.
If you don't have any sessions, then it's best to utilize tokens.
There's a great Node.JS API project that already implements tokens on Github, which I've forked and used in the past. You could browse this as an example.
Inside of this project, there's a config for the tokens like so:
var mongoose = require('mongoose');
var Schema = mongoose.Schema;
// AccessToken
var AccessToken = new Schema({
userId: {
type: String,
required: true
},
clientId: {
type: String,
required: true
},
token: {
type: String,
unique: true,
required: true
},
created: {
type: Date,
default: Date.now
}
});
module.exports = mongoose.model('AccessToken', AccessToken);
All you would need to do is change the logic to check for the clientId and token for authentication. If the clientId (which is auto-generated each time) is different, then log the user out; forcing them to re-authenticate.
With modern mobile devices, this can easily be done in Javascript with local storage:
var testObject = { 'one': 1, 'two': 2, 'three': 3 };
// Put the object into storage
localStorage.setItem('testObject', JSON.stringify(testObject))…
This allows infinite use on one device, but only ever one device at a time. You could also easily set an expiration on the token as well if you'd like to auto-log out after a period of time.