iotaledger / iota.js

IOTA JavaScript
962 stars 287 forks source link

401 Status Error for sendTransfer using remote host/node #46

Closed crypto5000 closed 7 years ago

crypto5000 commented 7 years ago

Hi, I'm starting a new IOTA project (with IOTA as the data layer) and I'm getting a 401 (unauthorized) error when invoking sendTransfer for one of the Light Wallet Servers listed on iotasupport.com/lightwallet.shtml. Is it possible to invoke sendTransfer() with a Light Wallet Server and, if so, is there any documentation on how to do this?

I've run the leaderboard example by @domschiener at https://github.com/domschiener/leaderboard-example and replaced localhost with one of the wallet servers. getAccountData() works as expected and shows the correct balance from the Tangle, but any sendTransfer is 401'd.

Any guidance or help is super appreciated!

bmavity commented 7 years ago

What is the error message?

Most likely you are trying to ask the light-wallet server to do Proof of Work, but the server does not allow it. As of right now, the only way to complete the tutorial is to run your own full node.

Eventually there will be a sandbox you can use or the tutorials can be updated to do proof of work locally. (I do not know how to do this offhand, but the wallet does it so I know there's a way)

crypto5000 commented 7 years ago

Thanks @bmavity for the reply and help! It's just a standard error message: "Failed to load resource: the server responded with a status of 401 (Unauthorized)" in the console. I thought this may just have been a basic auth issue where I needed to send a user/pass with the data request. I had assumed there should be a way precisely because the web wallet seemed to be doing proof of work locally, but after looking at @danilind's repo, it doesn't seem as straightforward as I first thought. I think the web wallet may be using a full node as you suggest. There's a dedicated ip for one of the providers.

It seems then I'm going to take a 2-prong approach: 1) First, build a manual proof of concept for my project, running the transfers through the desktop wallet. This isn't scalable, but will show the use case. 2) Try to see if I can port some of the web wallet code with a full node to replace the manual approach.

Thanks again for the reply!

danilind commented 7 years ago

The web wallet does the pow locallyusing the pow lib for javascript. If you have any questions about how it's done in the web wallet, feel free to contact me in slack (@danili)

crypto5000 commented 7 years ago

Hi Daniel! Thanks for the offer to help and thanks for building the web wallet. It's awesome.

2 quick questions for you regarding the web wallet: 1) Are you running a full node to process the transactions or are you doing the PoW locally and then connecting to a Light Wallet Server remotely to process the transactions? In other words, a full node vs remote node approach. 2) Can you explain the sendTrytesWrapper function in iota.wrapper.js? This seems like a modification of the sendTransfer function in iota.lib.js. What's the purpose of the commented out ajax call to localhost (http://127.0.0.1:500)?

Perhaps I can explain what I'm trying to do and you can offer some advice, since I think the web wallet contains all the functionality for what I'm trying to build.

I'm trying to build a voting/survey/poll system with IOTA as the data layer. There can be many different types of polls (simple, reverse, uncapped, authenticated, etc), but for the basic case, consider the simple poll. A simple poll would have a cap of say 100 votes and a choice of say 2 options. The cap is represented by a starting balance of 100 iota in a wallet. With a simple poll, a "user" should not have to own or purchase IOTA (through bitfinex, etc). They should just come to a site, and drag a single iota-like coin into a ballet box (say the vote is whether they like the web wallet or the desktop wallet better). Once the user clicks or drags their vote into the box (say they vote for the web wallet), then an iota is sent from the starting wallet to a new wallet used to store the current results. The balance decreases to 99 for the remaining votes, and the address associated with the "web wallet ballet" now has a balance of 1.

The 2 technical elements that seem pertinent are securing the seed (because the user is just indicating a determined action between wallets) and actually sending the transaction between wallets. Ideally, I envisioned the scenario where the proof of work is done locally and the transaction is sent through a light-wallet server using the sendTransfer function in iota.lib.js.

Any help, guidance, or suggestions are welcome and appreciated!

danilind commented 7 years ago
  1. It does the pow locally, and uses a light node for the other operations.
  2. Yeah, thats exactly right. Because I'm doing pow locally, I cannot use sendTransfer, so I have to reimplement it (though most of it, except attachToTangle, is just copied). The commented out code is to make the pow on a server. It's part of a trial to outsource the pow to a remote device, as the current implementation of local pow is a bit limiting. The server will be running separate from the actual iota node, and it's supposed to be a hybrid solution between the existing one and remote pow. Basically the server and local browser both start the pow, and "race" to solve it. In this scenario the server may reject the query if it's busy, so the local pow is just complemented, not replaced.

In this case I believe the wrapper around sendTransfer I made i iota.wrapper.js is what you're looking for. You also need the curl package (https://github.com/iotaledger/curl.lib.js) to do the acutal pow. Note that the version I'm using had a slightly different output then the new version. I believe it now returns the trits instead of trytes, but you might want to talk to paul h about that.

You should keep in mind that it may detriment the user experience when the pow is done locally, as it does take a bit of time. This may especially be the case in such an application where the user except the voting to be a fluid action without waiting.

I'm also working on utilizing web workers to provide a better user experience (and hopefully a speed up), and I can keep you updated on that if you are interested.

crypto5000 commented 7 years ago

Thanks so much, Daniel. This is amazingly helpful stuff. I really appreciate it.

I'm going to follow your advice and use the wrapper approach. It seems that approach (in my opinion) is probably a lower bar for developers to just get started building web-like IOTA applications - rather than having to run a full node and manually tether. I'll definitely keep watch of your github repo to see how the code progresses and to see how the web workers improve the experience.

All the best, thanks again!

crypto5000 commented 7 years ago

This approach appears to work with no problems.