How can I inject and use this javascript library web3 with JavascriptCore

0 votes

I have been trying to use the web3 library with JavascriptCore in Swift. A web3 instance is created by running

var web3 = new Web3(new Web3.providers.HttpProvider("[the provider im using]"))

This is the code I am trying to do this in Swift with:

if let url = Bundle.main.url(forResource: "web3", withExtension: "js"){
     let lib = try! String(contentsOfFile: url.path)
     let jsvirtualmachine = JSVirtualMachine()
     let context = JSContext(virtualMachine: jsvirtualmachine)
     context.evaluateScript(lib)

     let web3 = context.evaluateScript("var web3 = new Web3(new Web3.providers.HttpProvider('[myprovider]')")
     let fn = context.objectForKeyedSubscript("web3")
     let fn1 = fn?.construct(withArguments: [])
     let latestBlockNumber = context.evaluateScript("web3.eth.blockNumber")
     print(latestBlockNumber)
}

I have tried printing out latestBlockNumber, web3, fn, and fn1 and all of them return undefined

Oct 8, 2018 in Blockchain by slayer
• 29,370 points

retagged Nov 22, 2018 by Priyaj 936 views

1 answer to this question.

0 votes

The web3.js library has dependencies on bignumber.js and crypto-js.js (See dependencies here). You will need to add these two JS libs to the bundle and load them in a similar way, e.g.

do {
    let bigNumberJS = try String(contentsOf: Bundle.main.url(forResource: "bignumber.min", withExtension: "js")!, encoding: .utf8)
    let cryptoJS = try String(contentsOf: Bundle.main.url(forResource: "crypto-js", withExtension: "js")!, encoding: .utf8)
    let web3JS = try String(contentsOf: Bundle.main.url(forResource: "web3-light.min", withExtension: "js")!, encoding: .utf8)
    context.evaluateScript(bigNumberJS)
    context.evaluateScript(cryptoJS)
    context.evaluateScript(web3JS)
    context.evaluateScript("var Web3 = require('web3'); var web3 = new Web3(new Web3.providers.HttpProvider('http://localhost:8545'));")
    if let version = context.evaluateScript("web3.version.api") {
        print("Web3 version is \(version)")
    }
}
catch {
    print("An error occurred")
}
answered Oct 8, 2018 by Omkar
• 69,220 points

Related Questions In Blockchain

+1 vote
1 answer

Hyperledger - How can I use transaction certificates to enforce privacy?

The transaction certificates don't actually address privacy ...READ MORE

answered Apr 17, 2018 in Blockchain by Perry
• 17,100 points
867 views
+1 vote
1 answer
+1 vote
2 answers
0 votes
1 answer

How can I sync geth and testrpc private nodes locally?

node running on testrpc cannot be connected ...READ MORE

answered May 17, 2018 in Blockchain by Shashank
• 10,400 points
728 views
0 votes
1 answer

Truffle tests not running after truffle init

This was a bug. They've fixed it. ...READ MORE

answered Sep 11, 2018 in Blockchain by Christine
• 15,790 points
1,902 views
0 votes
1 answer

Hyperledger Sawtooth vs Quorum in concurrency and speed Ask

Summary: Both should provide similar reliability of ...READ MORE

answered Sep 26, 2018 in IoT (Internet of Things) by Upasana
• 8,620 points
1,431 views
+1 vote
1 answer

Protocols used in a distributed/dlt system for the nodes to establish communication

yes all are over TCP/IP connections secured ...READ MORE

answered Aug 6, 2018 in Blockchain by aryya
• 7,460 points
1,393 views
0 votes
1 answer

How can i integrate Omnet++ with ethereum ?? or is there any other simulator which can do this??

You can use Hive Validator. Using this, you ...READ MORE

answered Apr 9, 2019 in Blockchain by Omkar
• 69,220 points
1,474 views
0 votes
2 answers

How do I interact with a smart contract on a private network through web3.js

I found a blog that explains how ...READ MORE

answered Aug 20, 2018 in Blockchain by slayer
• 29,370 points
3,938 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP