admin.go 18 KB

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