Skip to content

Commit 95ee424

Browse files
committed
Merged in coinplugin/go-metadium (pull request #20)
Master Approved-by: Uh Sado <[email protected]>
2 parents ebca4c9 + 5b05cb2 commit 95ee424

File tree

14 files changed

+1121
-52
lines changed

14 files changed

+1121
-52
lines changed

Makefile

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -35,9 +35,11 @@ endif
3535
metadium: gmet logrot
3636
@[ -d build/conf ] || mkdir -p build/conf
3737
@cp -p metadium/scripts/gmet.sh metadium/scripts/solc.sh build/bin/
38-
@cp -p metadium/scripts/config.json.example \
39-
metadium/scripts/genesis-template.json \
40-
metadium/contracts/MetadiumGovernance.js build/conf/
38+
@cp -p metadium/scripts/config.json.example \
39+
metadium/scripts/genesis-template.json \
40+
metadium/contracts/MetadiumGovernance.js \
41+
metadium/scripts/deploy-governance.js \
42+
build/conf/
4143
@(cd build; tar cfz metadium.tar.gz bin conf)
4244
@echo "Done building build/metadium.tar.gz"
4345

@@ -210,7 +212,7 @@ ifeq ($(shell uname), Linux)
210212
if [ ! $$? = 0 ]; then \
211213
echo "Docker not found."; \
212214
else \
213-
docker run -e HOME=/tmp -it --rm \
215+
docker run -e HOME=/tmp --rm \
214216
-v /etc/passwd:/etc/passwd:ro \
215217
-v /etc/group:/etc/group:ro \
216218
-v ~/src:/home/$${USER}/src \
@@ -223,7 +225,7 @@ else
223225
if [ ! $$? = 0 ]; then \
224226
echo "Docker not found."; \
225227
else \
226-
docker run -e HOME=/tmp -it --rm -v $(shell pwd):/data \
228+
docker run -e HOME=/tmp --rm -v $(shell pwd):/data \
227229
-w /data metadium/bobthe:latest \
228230
make USE_ROCKSDB=$(USE_ROCKSDB); \
229231
fi

accounts/usbwallet/offline.go

Lines changed: 179 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,179 @@
1+
// Copyright 2019 The go-ethereum / go-metadium Authors
2+
// This file is part of the go-ethereum library.
3+
//
4+
// The go-ethereum library is free software: you can redistribute it and/or modify
5+
// it under the terms of the GNU Lesser General Public License as published by
6+
// the Free Software Foundation, either version 3 of the License, or
7+
// (at your option) any later version.
8+
//
9+
// The go-ethereum library is distributed in the hope that it will be useful,
10+
// but WITHOUT ANY WARRANTY; without even the implied warranty of
11+
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12+
// GNU Lesser General Public License for more details.
13+
//
14+
// You should have received a copy of the GNU Lesser General Public License
15+
// along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
16+
17+
package usbwallet
18+
19+
import (
20+
"errors"
21+
"io"
22+
"math/big"
23+
24+
"github.com/ethereum/go-ethereum/accounts"
25+
"github.com/ethereum/go-ethereum/common"
26+
"github.com/ethereum/go-ethereum/core/types"
27+
"github.com/ethereum/go-ethereum/log"
28+
"github.com/karalabe/hid"
29+
)
30+
31+
// a simple wrapper for offline use, mainly for Close()
32+
type offlineWallet struct {
33+
driver driver
34+
info hid.DeviceInfo
35+
device *hid.Device
36+
log log.Logger
37+
}
38+
39+
// dummy Hub structures for offline wallet use
40+
var (
41+
offlineHubs []*Hub
42+
)
43+
44+
func init() {
45+
// nano ledger
46+
offlineHubs = append(offlineHubs, &Hub{
47+
scheme: LedgerScheme,
48+
vendorID: 0x2c97,
49+
productIDs: []uint16{0x0000, 0x0001},
50+
usageID: 0xffa0,
51+
endpointID: 0,
52+
})
53+
// trezor 1
54+
offlineHubs = append(offlineHubs, &Hub{
55+
scheme: TrezorScheme,
56+
vendorID: 0x534c,
57+
productIDs: []uint16{0x0001},
58+
usageID: 0xff00,
59+
endpointID: 0,
60+
})
61+
// trezor 2, this doesn't seem to work yet.
62+
offlineHubs = append(offlineHubs, &Hub{
63+
scheme: TrezorScheme,
64+
vendorID: 0x1209,
65+
productIDs: []uint16{0x53c0, 0x53c1},
66+
usageID: 0xf1d0,
67+
endpointID: -1,
68+
})
69+
}
70+
71+
func (w *offlineWallet) Status() (string, error) {
72+
return w.driver.Status()
73+
}
74+
75+
func (w *offlineWallet) Open(device io.ReadWriter, passphrase string) error {
76+
return w.driver.Open(device, passphrase)
77+
}
78+
79+
func (w *offlineWallet) Close() error {
80+
if w.device == nil {
81+
return nil
82+
}
83+
w.driver.Close()
84+
w.device.Close()
85+
w.device = nil
86+
return nil
87+
}
88+
89+
func (w *offlineWallet) Heartbeat() error {
90+
return w.driver.Heartbeat()
91+
}
92+
93+
func (w *offlineWallet) Derive(path accounts.DerivationPath) (common.Address, error) {
94+
return w.driver.Derive(path)
95+
}
96+
97+
func (w *offlineWallet) SignTx(path accounts.DerivationPath, tx *types.Transaction, chainID *big.Int) (common.Address, *types.Transaction, error) {
98+
return w.driver.SignTx(path, tx, chainID)
99+
}
100+
101+
// ListDevices returns the array of ledger or trezor device paths
102+
// for mostly informational use
103+
func ListDevices(scheme string) []string {
104+
var devices []string
105+
106+
// triple loop!
107+
for _, hub := range offlineHubs {
108+
if len(scheme) > 0 && hub.scheme != scheme {
109+
continue
110+
}
111+
for _, info := range hid.Enumerate(hub.vendorID, 0) {
112+
for _, id := range hub.productIDs {
113+
if info.ProductID == id && (info.UsagePage == hub.usageID || info.Interface == hub.endpointID) {
114+
devices = append(devices, info.Path)
115+
break
116+
}
117+
}
118+
}
119+
}
120+
return devices
121+
}
122+
123+
// OpenOffline opens usb wallet for offline use.
124+
// If path is not specified, the first device is used.
125+
// Note that Trezor 1 after firmware upgrade doesn't work any more.
126+
func OpenOffline(scheme, path string) (interface{}, error) {
127+
if scheme != LedgerScheme && scheme != TrezorScheme {
128+
return nil, errors.New("Not Supported")
129+
}
130+
if path == "" {
131+
paths := ListDevices(scheme)
132+
if len(paths) == 0 {
133+
return nil, errors.New("Not Found")
134+
}
135+
path = paths[0]
136+
}
137+
138+
for _, hub := range offlineHubs {
139+
if hub.scheme != scheme {
140+
continue
141+
}
142+
for _, info := range hid.Enumerate(hub.vendorID, 0) {
143+
if info.Path == path {
144+
if scheme == TrezorScheme && info.Interface == -1 {
145+
// Trezor 2. It's going to hang. Stop here.
146+
return nil, errors.New("Trezor 2 doesn't work yet. You shouldn't have upgraded the firmware.")
147+
}
148+
149+
logger := log.New("url", accounts.URL{Scheme: scheme, Path: path})
150+
var drv driver
151+
switch scheme {
152+
case LedgerScheme:
153+
drv = newLedgerDriver(logger)
154+
case TrezorScheme:
155+
drv = newTrezorDriver(logger)
156+
}
157+
device, err := info.Open()
158+
if err != nil {
159+
return nil, err
160+
}
161+
err = drv.Open(device, "")
162+
if err != nil {
163+
return nil, err
164+
}
165+
w := &offlineWallet{
166+
driver: drv,
167+
info: info,
168+
device: device,
169+
log: logger,
170+
}
171+
return w, nil
172+
}
173+
}
174+
}
175+
176+
return nil, errors.New("Not Found")
177+
}
178+
179+
// EOF

console/console.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -220,6 +220,8 @@ func (c *Console) init(preload []string) error {
220220
}
221221
c.prompter.SetWordCompleter(c.AutoCompleteInput)
222222
}
223+
// Metadium: set console prompter
224+
jsre.PromptPassword = promptPassword
223225
return nil
224226
}
225227

console/prompter.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -170,3 +170,8 @@ func (p *terminalPrompter) ClearHistory() {
170170
func (p *terminalPrompter) SetWordCompleter(completer WordCompleter) {
171171
p.State.SetWordCompleter(liner.WordCompleter(completer))
172172
}
173+
174+
// Metadium. For offline wallet use
175+
func promptPassword(prompt string) (string, error) {
176+
return Stdin.PromptPassword(prompt)
177+
}

ethclient/ethclient.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -506,6 +506,18 @@ func (ec *Client) SendTransaction(ctx context.Context, tx *types.Transaction) er
506506
return ec.c.CallContext(ctx, nil, "eth_sendRawTransaction", common.ToHex(data))
507507
}
508508

509+
// SendTransactions injects signed transactions into the pending pool for execution.
510+
//
511+
// If the transaction was a contract creation use the TransactionReceipt method to get the
512+
// contract address after the transaction has been mined.
513+
func (ec *Client) SendTransactions(ctx context.Context, txs types.Transactions) error {
514+
data, err := rlp.EncodeToBytes(txs)
515+
if err != nil {
516+
return err
517+
}
518+
return ec.c.CallContext(ctx, nil, "eth_sendRawTransactions", common.ToHex(data))
519+
}
520+
509521
func toCallArg(msg ethereum.CallMsg) interface{} {
510522
arg := map[string]interface{}{
511523
"from": msg.From,

internal/ethapi/api.go

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1337,6 +1337,26 @@ func (s *PublicTransactionPoolAPI) SendRawTransaction(ctx context.Context, encod
13371337
return submitTransaction(ctx, s.b, tx)
13381338
}
13391339

1340+
// SendRawTransactions will add the signed transactions to the transaction pool.
1341+
// The sender is responsible for signing the transactions and using the correct nonces.
1342+
func (s *PublicTransactionPoolAPI) SendRawTransactions(ctx context.Context, encodedTxs []hexutil.Bytes) ([]common.Hash, error) {
1343+
var hashes []common.Hash
1344+
for _, etx := range encodedTxs {
1345+
tx := new(types.Transaction)
1346+
if err := rlp.DecodeBytes(etx, tx); err != nil {
1347+
hashes = append(hashes, common.Hash{})
1348+
continue
1349+
}
1350+
hash, err := submitTransaction(ctx, s.b, tx)
1351+
if err == nil {
1352+
hashes = append(hashes, hash)
1353+
} else {
1354+
hashes = append(hashes, common.Hash{})
1355+
}
1356+
}
1357+
return hashes, nil
1358+
}
1359+
13401360
// Sign calculates an ECDSA signature for:
13411361
// keccack256("\x19Ethereum Signed Message:\n" + len(message) + message).
13421362
//

internal/jsre/deps/bindata.go

Lines changed: 55 additions & 14 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

internal/jsre/deps/web3.js

Lines changed: 8 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

internal/jsre/jsre.go

Lines changed: 35 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,13 @@ func New(assetPath string, output io.Writer) *JSRE {
7878
go re.runEventLoop()
7979
re.Set("loadScript", re.loadScript)
8080
re.Set("inspect", re.prettyPrintJS)
81+
re.Set("loadFile", re.loadFile)
82+
re.Set("msleep", re.msleep)
83+
re.Set("offlineWalletOpen", re.offlineWalletOpen)
84+
re.Set("offlineWalletAddress", re.offlineWalletAddress)
85+
re.Set("offlineWalletClose", re.offlineWalletClose)
86+
re.Set("offlineWalletSignTx", re.offlineWalletSignTx)
87+
re.Set("offlineWalletList", re.offlineWalletList)
8188
return re
8289
}
8390

@@ -296,7 +303,6 @@ func (re *JSRE) loadScript(call otto.FunctionCall) otto.Value {
296303
}
297304
if _, err := compileAndRun(call.Otto, file, source); err != nil {
298305
// TODO: throw exception
299-
fmt.Println("err:", err)
300306
return otto.FalseValue()
301307
}
302308
// TODO: return evaluation result
@@ -333,3 +339,31 @@ func compileAndRun(vm *otto.Otto, filename string, src interface{}) (otto.Value,
333339
}
334340
return vm.Run(script)
335341
}
342+
343+
// loadFile loads a file content as string
344+
func (re *JSRE) loadFile(call otto.FunctionCall) otto.Value {
345+
file, err := call.Argument(0).ToString()
346+
if err != nil {
347+
return otto.UndefinedValue()
348+
}
349+
file = common.AbsolutePath(re.assetPath, file)
350+
source, err := ioutil.ReadFile(file)
351+
if err != nil {
352+
return otto.UndefinedValue()
353+
}
354+
value, err := otto.ToValue(string(source))
355+
if err != nil {
356+
return otto.UndefinedValue()
357+
}
358+
return value
359+
}
360+
361+
// msleep sleeps in ms resolution
362+
func (re *JSRE) msleep(call otto.FunctionCall) otto.Value {
363+
delay, err := call.Argument(0).ToInteger()
364+
if err != nil {
365+
return otto.FalseValue()
366+
}
367+
time.Sleep(time.Duration(delay) * time.Millisecond)
368+
return otto.TrueValue()
369+
}

0 commit comments

Comments
 (0)