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,260 points

retagged Nov 22, 2018 by Priyaj 61 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
• 68,860 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,090 points
159 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 16, 2018 in Blockchain by Shashank
• 10,360 points
51 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,810 points
205 views
0 votes
1 answer
0 votes
1 answer

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

yes all are over TCP/IP connections secured by TLS encryption in hashgraph architecture-hashgraph, ...READ MORE

answered Aug 6, 2018 in Blockchain by Aryya
• 7,350 points
123 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
• 68,860 points
154 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,260 points
524 views