admin.go 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800
  1. package main
  2. import (
  3. "encoding/json"
  4. "errors"
  5. "fmt"
  6. "math/big"
  7. "strconv"
  8. "time"
  9. "github.com/ethereum/go-ethereum/accounts"
  10. "github.com/ethereum/go-ethereum/cmd/utils"
  11. "github.com/ethereum/go-ethereum/common"
  12. "github.com/ethereum/go-ethereum/common/compiler"
  13. "github.com/ethereum/go-ethereum/common/natspec"
  14. "github.com/ethereum/go-ethereum/common/resolver"
  15. "github.com/ethereum/go-ethereum/core/state"
  16. "github.com/ethereum/go-ethereum/core/types"
  17. "github.com/ethereum/go-ethereum/core/vm"
  18. "github.com/ethereum/go-ethereum/crypto"
  19. "github.com/ethereum/go-ethereum/logger/glog"
  20. "github.com/ethereum/go-ethereum/rlp"
  21. "github.com/ethereum/go-ethereum/rpc"
  22. "github.com/ethereum/go-ethereum/xeth"
  23. "github.com/robertkrimen/otto"
  24. "gopkg.in/fatih/set.v0"
  25. )
  26. /*
  27. node admin bindings
  28. */
  29. func (js *jsre) adminBindings() {
  30. ethO, _ := js.re.Get("eth")
  31. eth := ethO.Object()
  32. eth.Set("pendingTransactions", js.pendingTransactions)
  33. eth.Set("resend", js.resend)
  34. eth.Set("sign", js.sign)
  35. js.re.Set("admin", struct{}{})
  36. t, _ := js.re.Get("admin")
  37. admin := t.Object()
  38. admin.Set("addPeer", js.addPeer)
  39. admin.Set("startRPC", js.startRPC)
  40. admin.Set("stopRPC", js.stopRPC)
  41. admin.Set("nodeInfo", js.nodeInfo)
  42. admin.Set("peers", js.peers)
  43. admin.Set("newAccount", js.newAccount)
  44. admin.Set("unlock", js.unlock)
  45. admin.Set("import", js.importChain)
  46. admin.Set("export", js.exportChain)
  47. admin.Set("verbosity", js.verbosity)
  48. admin.Set("progress", js.downloadProgress)
  49. admin.Set("setSolc", js.setSolc)
  50. admin.Set("contractInfo", struct{}{})
  51. t, _ = admin.Get("contractInfo")
  52. cinfo := t.Object()
  53. // newRegistry officially not documented temporary option
  54. cinfo.Set("start", js.startNatSpec)
  55. cinfo.Set("stop", js.stopNatSpec)
  56. cinfo.Set("newRegistry", js.newRegistry)
  57. cinfo.Set("get", js.getContractInfo)
  58. cinfo.Set("register", js.register)
  59. cinfo.Set("registerUrl", js.registerUrl)
  60. // cinfo.Set("verify", js.verify)
  61. admin.Set("miner", struct{}{})
  62. t, _ = admin.Get("miner")
  63. miner := t.Object()
  64. miner.Set("start", js.startMining)
  65. miner.Set("stop", js.stopMining)
  66. miner.Set("hashrate", js.hashrate)
  67. miner.Set("setExtra", js.setExtra)
  68. miner.Set("setGasPrice", js.setGasPrice)
  69. admin.Set("debug", struct{}{})
  70. t, _ = admin.Get("debug")
  71. debug := t.Object()
  72. js.re.Set("sleep", js.sleep)
  73. debug.Set("backtrace", js.backtrace)
  74. debug.Set("printBlock", js.printBlock)
  75. debug.Set("dumpBlock", js.dumpBlock)
  76. debug.Set("getBlockRlp", js.getBlockRlp)
  77. debug.Set("setHead", js.setHead)
  78. debug.Set("processBlock", js.debugBlock)
  79. // undocumented temporary
  80. debug.Set("waitForBlocks", js.waitForBlocks)
  81. }
  82. // generic helper to getBlock by Number/Height or Hex depending on autodetected input
  83. // if argument is missing the current block is returned
  84. // if block is not found or there is problem with decoding
  85. // the appropriate value is returned and block is guaranteed to be nil
  86. func (js *jsre) getBlock(call otto.FunctionCall) (*types.Block, error) {
  87. var block *types.Block
  88. if len(call.ArgumentList) > 0 {
  89. if call.Argument(0).IsNumber() {
  90. num, _ := call.Argument(0).ToInteger()
  91. block = js.ethereum.ChainManager().GetBlockByNumber(uint64(num))
  92. } else if call.Argument(0).IsString() {
  93. hash, _ := call.Argument(0).ToString()
  94. block = js.ethereum.ChainManager().GetBlock(common.HexToHash(hash))
  95. } else {
  96. return nil, errors.New("invalid argument for dump. Either hex string or number")
  97. }
  98. } else {
  99. block = js.ethereum.ChainManager().CurrentBlock()
  100. }
  101. if block == nil {
  102. return nil, errors.New("block not found")
  103. }
  104. return block, nil
  105. }
  106. func (js *jsre) pendingTransactions(call otto.FunctionCall) otto.Value {
  107. txs := js.ethereum.TxPool().GetTransactions()
  108. // grab the accounts from the account manager. This will help with determening which
  109. // transactions should be returned.
  110. accounts, err := js.ethereum.AccountManager().Accounts()
  111. if err != nil {
  112. fmt.Println(err)
  113. return otto.UndefinedValue()
  114. }
  115. // Add the accouns to a new set
  116. accountSet := set.New()
  117. for _, account := range accounts {
  118. accountSet.Add(account.Address)
  119. }
  120. //ltxs := make([]*tx, len(txs))
  121. var ltxs []*tx
  122. for _, tx := range txs {
  123. // no need to check err
  124. if from, _ := tx.From(); accountSet.Has(from) {
  125. ltxs = append(ltxs, newTx(tx))
  126. }
  127. }
  128. return js.re.ToVal(ltxs)
  129. }
  130. func (js *jsre) resend(call otto.FunctionCall) otto.Value {
  131. if len(call.ArgumentList) == 0 {
  132. fmt.Println("first argument must be a transaction")
  133. return otto.FalseValue()
  134. }
  135. v, err := call.Argument(0).Export()
  136. if err != nil {
  137. fmt.Println(err)
  138. return otto.FalseValue()
  139. }
  140. if tx, ok := v.(*tx); ok {
  141. gl, gp := tx.GasLimit, tx.GasPrice
  142. if len(call.ArgumentList) > 1 {
  143. gp = call.Argument(1).String()
  144. }
  145. if len(call.ArgumentList) > 2 {
  146. gl = call.Argument(2).String()
  147. }
  148. ret, err := js.xeth.Transact(tx.From, tx.To, tx.Nonce, tx.Value, gl, gp, tx.Data)
  149. if err != nil {
  150. fmt.Println(err)
  151. return otto.FalseValue()
  152. }
  153. js.ethereum.TxPool().RemoveTransactions(types.Transactions{tx.tx})
  154. return js.re.ToVal(ret)
  155. }
  156. fmt.Println("first argument must be a transaction")
  157. return otto.FalseValue()
  158. }
  159. func (js *jsre) sign(call otto.FunctionCall) otto.Value {
  160. if len(call.ArgumentList) != 2 {
  161. fmt.Println("requires 2 arguments: eth.sign(signer, data)")
  162. return otto.UndefinedValue()
  163. }
  164. signer, err := call.Argument(0).ToString()
  165. if err != nil {
  166. fmt.Println(err)
  167. return otto.UndefinedValue()
  168. }
  169. data, err := call.Argument(1).ToString()
  170. if err != nil {
  171. fmt.Println(err)
  172. return otto.UndefinedValue()
  173. }
  174. v, err := js.xeth.Sign(signer, data, false)
  175. if err != nil {
  176. fmt.Println(err)
  177. return otto.UndefinedValue()
  178. }
  179. return js.re.ToVal(v)
  180. }
  181. func (js *jsre) debugBlock(call otto.FunctionCall) otto.Value {
  182. block, err := js.getBlock(call)
  183. if err != nil {
  184. fmt.Println(err)
  185. return otto.UndefinedValue()
  186. }
  187. old := vm.Debug
  188. vm.Debug = true
  189. _, err = js.ethereum.BlockProcessor().RetryProcess(block)
  190. if err != nil {
  191. glog.Infoln(err)
  192. }
  193. vm.Debug = old
  194. return otto.UndefinedValue()
  195. }
  196. func (js *jsre) setHead(call otto.FunctionCall) otto.Value {
  197. block, err := js.getBlock(call)
  198. if err != nil {
  199. fmt.Println(err)
  200. return otto.UndefinedValue()
  201. }
  202. js.ethereum.ChainManager().SetHead(block)
  203. return otto.UndefinedValue()
  204. }
  205. func (js *jsre) downloadProgress(call otto.FunctionCall) otto.Value {
  206. current, max := js.ethereum.Downloader().Stats()
  207. return js.re.ToVal(fmt.Sprintf("%d/%d", current, max))
  208. }
  209. func (js *jsre) getBlockRlp(call otto.FunctionCall) otto.Value {
  210. block, err := js.getBlock(call)
  211. if err != nil {
  212. fmt.Println(err)
  213. return otto.UndefinedValue()
  214. }
  215. encoded, _ := rlp.EncodeToBytes(block)
  216. return js.re.ToVal(fmt.Sprintf("%x", encoded))
  217. }
  218. func (js *jsre) setExtra(call otto.FunctionCall) otto.Value {
  219. extra, err := call.Argument(0).ToString()
  220. if err != nil {
  221. fmt.Println(err)
  222. return otto.UndefinedValue()
  223. }
  224. if len(extra) > 1024 {
  225. fmt.Println("error: cannot exceed 1024 bytes")
  226. return otto.UndefinedValue()
  227. }
  228. js.ethereum.Miner().SetExtra([]byte(extra))
  229. return otto.UndefinedValue()
  230. }
  231. func (js *jsre) setGasPrice(call otto.FunctionCall) otto.Value {
  232. gasPrice, err := call.Argument(0).ToString()
  233. if err != nil {
  234. fmt.Println(err)
  235. return otto.UndefinedValue()
  236. }
  237. js.ethereum.Miner().SetGasPrice(common.String2Big(gasPrice))
  238. return otto.UndefinedValue()
  239. }
  240. func (js *jsre) hashrate(otto.FunctionCall) otto.Value {
  241. return js.re.ToVal(js.ethereum.Miner().HashRate())
  242. }
  243. func (js *jsre) backtrace(call otto.FunctionCall) otto.Value {
  244. tracestr, err := call.Argument(0).ToString()
  245. if err != nil {
  246. fmt.Println(err)
  247. return otto.UndefinedValue()
  248. }
  249. glog.GetTraceLocation().Set(tracestr)
  250. return otto.UndefinedValue()
  251. }
  252. func (js *jsre) verbosity(call otto.FunctionCall) otto.Value {
  253. v, err := call.Argument(0).ToInteger()
  254. if err != nil {
  255. fmt.Println(err)
  256. return otto.UndefinedValue()
  257. }
  258. glog.SetV(int(v))
  259. return otto.UndefinedValue()
  260. }
  261. func (js *jsre) startMining(call otto.FunctionCall) otto.Value {
  262. var (
  263. threads int64
  264. err error
  265. )
  266. if len(call.ArgumentList) > 0 {
  267. threads, err = call.Argument(0).ToInteger()
  268. if err != nil {
  269. fmt.Println(err)
  270. return otto.FalseValue()
  271. }
  272. } else {
  273. threads = int64(js.ethereum.MinerThreads)
  274. }
  275. err = js.ethereum.StartMining(int(threads))
  276. if err != nil {
  277. fmt.Println(err)
  278. return otto.FalseValue()
  279. }
  280. return otto.TrueValue()
  281. }
  282. func (js *jsre) stopMining(call otto.FunctionCall) otto.Value {
  283. js.ethereum.StopMining()
  284. return otto.TrueValue()
  285. }
  286. func (js *jsre) startRPC(call otto.FunctionCall) otto.Value {
  287. addr, err := call.Argument(0).ToString()
  288. if err != nil {
  289. fmt.Println(err)
  290. return otto.FalseValue()
  291. }
  292. port, err := call.Argument(1).ToInteger()
  293. if err != nil {
  294. fmt.Println(err)
  295. return otto.FalseValue()
  296. }
  297. corsDomain := js.corsDomain
  298. if len(call.ArgumentList) > 2 {
  299. corsDomain, err = call.Argument(2).ToString()
  300. if err != nil {
  301. fmt.Println(err)
  302. return otto.FalseValue()
  303. }
  304. }
  305. config := rpc.RpcConfig{
  306. ListenAddress: addr,
  307. ListenPort: uint(port),
  308. CorsDomain: corsDomain,
  309. }
  310. xeth := xeth.New(js.ethereum, nil)
  311. err = rpc.Start(xeth, config)
  312. if err != nil {
  313. fmt.Println(err)
  314. return otto.FalseValue()
  315. }
  316. return otto.TrueValue()
  317. }
  318. func (js *jsre) stopRPC(call otto.FunctionCall) otto.Value {
  319. if rpc.Stop() == nil {
  320. return otto.TrueValue()
  321. }
  322. return otto.FalseValue()
  323. }
  324. func (js *jsre) addPeer(call otto.FunctionCall) otto.Value {
  325. nodeURL, err := call.Argument(0).ToString()
  326. if err != nil {
  327. fmt.Println(err)
  328. return otto.FalseValue()
  329. }
  330. err = js.ethereum.AddPeer(nodeURL)
  331. if err != nil {
  332. fmt.Println(err)
  333. return otto.FalseValue()
  334. }
  335. return otto.TrueValue()
  336. }
  337. func (js *jsre) unlock(call otto.FunctionCall) otto.Value {
  338. addr, err := call.Argument(0).ToString()
  339. if err != nil {
  340. fmt.Println(err)
  341. return otto.FalseValue()
  342. }
  343. seconds, err := call.Argument(2).ToInteger()
  344. if err != nil {
  345. fmt.Println(err)
  346. return otto.FalseValue()
  347. }
  348. if seconds == 0 {
  349. seconds = accounts.DefaultAccountUnlockDuration
  350. }
  351. arg := call.Argument(1)
  352. var passphrase string
  353. if arg.IsUndefined() {
  354. fmt.Println("Please enter a passphrase now.")
  355. passphrase, err = utils.PromptPassword("Passphrase: ", true)
  356. if err != nil {
  357. fmt.Println(err)
  358. return otto.FalseValue()
  359. }
  360. } else {
  361. passphrase, err = arg.ToString()
  362. if err != nil {
  363. fmt.Println(err)
  364. return otto.FalseValue()
  365. }
  366. }
  367. am := js.ethereum.AccountManager()
  368. err = am.TimedUnlock(common.HexToAddress(addr), passphrase, time.Duration(seconds)*time.Second)
  369. if err != nil {
  370. fmt.Printf("Unlock account failed '%v'\n", err)
  371. return otto.FalseValue()
  372. }
  373. return otto.TrueValue()
  374. }
  375. func (js *jsre) newAccount(call otto.FunctionCall) otto.Value {
  376. arg := call.Argument(0)
  377. var passphrase string
  378. if arg.IsUndefined() {
  379. fmt.Println("The new account will be encrypted with a passphrase.")
  380. fmt.Println("Please enter a passphrase now.")
  381. auth, err := utils.PromptPassword("Passphrase: ", true)
  382. if err != nil {
  383. fmt.Println(err)
  384. return otto.FalseValue()
  385. }
  386. confirm, err := utils.PromptPassword("Repeat Passphrase: ", false)
  387. if err != nil {
  388. fmt.Println(err)
  389. return otto.FalseValue()
  390. }
  391. if auth != confirm {
  392. fmt.Println("Passphrases did not match.")
  393. return otto.FalseValue()
  394. }
  395. passphrase = auth
  396. } else {
  397. var err error
  398. passphrase, err = arg.ToString()
  399. if err != nil {
  400. fmt.Println(err)
  401. return otto.FalseValue()
  402. }
  403. }
  404. acct, err := js.ethereum.AccountManager().NewAccount(passphrase)
  405. if err != nil {
  406. fmt.Printf("Could not create the account: %v", err)
  407. return otto.UndefinedValue()
  408. }
  409. return js.re.ToVal(acct.Address.Hex())
  410. }
  411. func (js *jsre) nodeInfo(call otto.FunctionCall) otto.Value {
  412. return js.re.ToVal(js.ethereum.NodeInfo())
  413. }
  414. func (js *jsre) peers(call otto.FunctionCall) otto.Value {
  415. return js.re.ToVal(js.ethereum.PeersInfo())
  416. }
  417. func (js *jsre) importChain(call otto.FunctionCall) otto.Value {
  418. if len(call.ArgumentList) == 0 {
  419. fmt.Println("require file name. admin.importChain(filename)")
  420. return otto.FalseValue()
  421. }
  422. fn, err := call.Argument(0).ToString()
  423. if err != nil {
  424. fmt.Println(err)
  425. return otto.FalseValue()
  426. }
  427. if err := utils.ImportChain(js.ethereum.ChainManager(), fn); err != nil {
  428. fmt.Println("Import error: ", err)
  429. return otto.FalseValue()
  430. }
  431. return otto.TrueValue()
  432. }
  433. func (js *jsre) exportChain(call otto.FunctionCall) otto.Value {
  434. if len(call.ArgumentList) == 0 {
  435. fmt.Println("require file name: admin.exportChain(filename)")
  436. return otto.FalseValue()
  437. }
  438. fn, err := call.Argument(0).ToString()
  439. if err != nil {
  440. fmt.Println(err)
  441. return otto.FalseValue()
  442. }
  443. if err := utils.ExportChain(js.ethereum.ChainManager(), fn); err != nil {
  444. fmt.Println(err)
  445. return otto.FalseValue()
  446. }
  447. return otto.TrueValue()
  448. }
  449. func (js *jsre) printBlock(call otto.FunctionCall) otto.Value {
  450. block, err := js.getBlock(call)
  451. if err != nil {
  452. fmt.Println(err)
  453. return otto.UndefinedValue()
  454. }
  455. fmt.Println(block)
  456. return otto.UndefinedValue()
  457. }
  458. func (js *jsre) dumpBlock(call otto.FunctionCall) otto.Value {
  459. block, err := js.getBlock(call)
  460. if err != nil {
  461. fmt.Println(err)
  462. return otto.UndefinedValue()
  463. }
  464. statedb := state.New(block.Root(), js.ethereum.StateDb())
  465. dump := statedb.RawDump()
  466. return js.re.ToVal(dump)
  467. }
  468. func (js *jsre) waitForBlocks(call otto.FunctionCall) otto.Value {
  469. if len(call.ArgumentList) > 2 {
  470. fmt.Println("requires 0, 1 or 2 arguments: admin.debug.waitForBlock(minHeight, timeout)")
  471. return otto.FalseValue()
  472. }
  473. var n, timeout int64
  474. var timer <-chan time.Time
  475. var height *big.Int
  476. var err error
  477. args := len(call.ArgumentList)
  478. if args == 2 {
  479. timeout, err = call.Argument(1).ToInteger()
  480. if err != nil {
  481. fmt.Println(err)
  482. return otto.UndefinedValue()
  483. }
  484. timer = time.NewTimer(time.Duration(timeout) * time.Second).C
  485. }
  486. if args >= 1 {
  487. n, err = call.Argument(0).ToInteger()
  488. if err != nil {
  489. fmt.Println(err)
  490. return otto.UndefinedValue()
  491. }
  492. height = big.NewInt(n)
  493. }
  494. if args == 0 {
  495. height = js.xeth.CurrentBlock().Number()
  496. height.Add(height, common.Big1)
  497. }
  498. wait := js.wait
  499. js.wait <- height
  500. select {
  501. case <-timer:
  502. // if times out make sure the xeth loop does not block
  503. go func() {
  504. select {
  505. case wait <- nil:
  506. case <-wait:
  507. }
  508. }()
  509. return otto.UndefinedValue()
  510. case height = <-wait:
  511. }
  512. return js.re.ToVal(height.Uint64())
  513. }
  514. func (js *jsre) sleep(call otto.FunctionCall) otto.Value {
  515. sec, err := call.Argument(0).ToInteger()
  516. if err != nil {
  517. fmt.Println(err)
  518. return otto.FalseValue()
  519. }
  520. time.Sleep(time.Duration(sec) * time.Second)
  521. return otto.UndefinedValue()
  522. }
  523. func (js *jsre) setSolc(call otto.FunctionCall) otto.Value {
  524. if len(call.ArgumentList) != 1 {
  525. fmt.Println("needs 1 argument: admin.contractInfo.setSolc(solcPath)")
  526. return otto.FalseValue()
  527. }
  528. solcPath, err := call.Argument(0).ToString()
  529. if err != nil {
  530. return otto.FalseValue()
  531. }
  532. solc, err := js.xeth.SetSolc(solcPath)
  533. if err != nil {
  534. fmt.Println(err)
  535. return otto.FalseValue()
  536. }
  537. fmt.Println(solc.Info())
  538. return otto.TrueValue()
  539. }
  540. func (js *jsre) register(call otto.FunctionCall) otto.Value {
  541. if len(call.ArgumentList) != 4 {
  542. fmt.Println("requires 4 arguments: admin.contractInfo.register(fromaddress, contractaddress, contract, filename)")
  543. return otto.UndefinedValue()
  544. }
  545. sender, err := call.Argument(0).ToString()
  546. if err != nil {
  547. fmt.Println(err)
  548. return otto.UndefinedValue()
  549. }
  550. address, err := call.Argument(1).ToString()
  551. if err != nil {
  552. fmt.Println(err)
  553. return otto.UndefinedValue()
  554. }
  555. raw, err := call.Argument(2).Export()
  556. if err != nil {
  557. fmt.Println(err)
  558. return otto.UndefinedValue()
  559. }
  560. jsonraw, err := json.Marshal(raw)
  561. if err != nil {
  562. fmt.Println(err)
  563. return otto.UndefinedValue()
  564. }
  565. var contract compiler.Contract
  566. err = json.Unmarshal(jsonraw, &contract)
  567. if err != nil {
  568. fmt.Println(err)
  569. return otto.UndefinedValue()
  570. }
  571. filename, err := call.Argument(3).ToString()
  572. if err != nil {
  573. fmt.Println(err)
  574. return otto.UndefinedValue()
  575. }
  576. contenthash, err := compiler.ExtractInfo(&contract, filename)
  577. if err != nil {
  578. fmt.Println(err)
  579. return otto.UndefinedValue()
  580. }
  581. // sender and contract address are passed as hex strings
  582. codeb := js.xeth.CodeAtBytes(address)
  583. codehash := common.BytesToHash(crypto.Sha3(codeb))
  584. if err != nil {
  585. fmt.Println(err)
  586. return otto.UndefinedValue()
  587. }
  588. registry := resolver.New(js.xeth)
  589. _, err = registry.RegisterContentHash(common.HexToAddress(sender), codehash, contenthash)
  590. if err != nil {
  591. fmt.Println(err)
  592. return otto.UndefinedValue()
  593. }
  594. return js.re.ToVal(contenthash.Hex())
  595. }
  596. func (js *jsre) registerUrl(call otto.FunctionCall) otto.Value {
  597. if len(call.ArgumentList) != 3 {
  598. fmt.Println("requires 3 arguments: admin.contractInfo.register(fromaddress, contenthash, filename)")
  599. return otto.FalseValue()
  600. }
  601. sender, err := call.Argument(0).ToString()
  602. if err != nil {
  603. fmt.Println(err)
  604. return otto.FalseValue()
  605. }
  606. contenthash, err := call.Argument(1).ToString()
  607. if err != nil {
  608. fmt.Println(err)
  609. return otto.FalseValue()
  610. }
  611. url, err := call.Argument(2).ToString()
  612. if err != nil {
  613. fmt.Println(err)
  614. return otto.FalseValue()
  615. }
  616. registry := resolver.New(js.xeth)
  617. _, err = registry.RegisterUrl(common.HexToAddress(sender), common.HexToHash(contenthash), url)
  618. if err != nil {
  619. fmt.Println(err)
  620. return otto.FalseValue()
  621. }
  622. return otto.TrueValue()
  623. }
  624. func (js *jsre) getContractInfo(call otto.FunctionCall) otto.Value {
  625. if len(call.ArgumentList) != 1 {
  626. fmt.Println("requires 1 argument: admin.contractInfo.register(contractaddress)")
  627. return otto.FalseValue()
  628. }
  629. addr, err := call.Argument(0).ToString()
  630. if err != nil {
  631. fmt.Println(err)
  632. return otto.FalseValue()
  633. }
  634. infoDoc, err := natspec.FetchDocsForContract(addr, js.xeth, ds)
  635. if err != nil {
  636. fmt.Println(err)
  637. return otto.UndefinedValue()
  638. }
  639. var info compiler.ContractInfo
  640. err = json.Unmarshal(infoDoc, &info)
  641. if err != nil {
  642. fmt.Println(err)
  643. return otto.UndefinedValue()
  644. }
  645. return js.re.ToVal(info)
  646. }
  647. func (js *jsre) startNatSpec(call otto.FunctionCall) otto.Value {
  648. js.ethereum.NatSpec = true
  649. return otto.TrueValue()
  650. }
  651. func (js *jsre) stopNatSpec(call otto.FunctionCall) otto.Value {
  652. js.ethereum.NatSpec = false
  653. return otto.TrueValue()
  654. }
  655. func (js *jsre) newRegistry(call otto.FunctionCall) otto.Value {
  656. if len(call.ArgumentList) != 1 {
  657. fmt.Println("requires 1 argument: admin.contractInfo.newRegistry(adminaddress)")
  658. return otto.FalseValue()
  659. }
  660. addr, err := call.Argument(0).ToString()
  661. if err != nil {
  662. fmt.Println(err)
  663. return otto.FalseValue()
  664. }
  665. registry := resolver.New(js.xeth)
  666. err = registry.CreateContracts(common.HexToAddress(addr))
  667. if err != nil {
  668. fmt.Println(err)
  669. return otto.FalseValue()
  670. }
  671. return otto.TrueValue()
  672. }
  673. // internal transaction type which will allow us to resend transactions using `eth.resend`
  674. type tx struct {
  675. tx *types.Transaction
  676. To string
  677. From string
  678. Nonce string
  679. Value string
  680. Data string
  681. GasLimit string
  682. GasPrice string
  683. }
  684. func newTx(t *types.Transaction) *tx {
  685. from, _ := t.From()
  686. var to string
  687. if t := t.To(); t != nil {
  688. to = t.Hex()
  689. }
  690. return &tx{
  691. tx: t,
  692. To: to,
  693. From: from.Hex(),
  694. Value: t.Amount.String(),
  695. Nonce: strconv.Itoa(int(t.Nonce())),
  696. Data: "0x" + common.Bytes2Hex(t.Data()),
  697. GasLimit: t.GasLimit.String(),
  698. GasPrice: t.GasPrice().String(),
  699. }
  700. }