aboutsummaryrefslogtreecommitdiff
path: root/coreth.go
blob: 0f65ca9f332ba0723867cc2c8262ac29ce0d62d2 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
package coreth

import (
    "io"
    "crypto/ecdsa"

    "github.com/ethereum/go-ethereum/core/types"
    "github.com/ethereum/go-ethereum/common"
    //"github.com/ethereum/go-ethereum/eth"
    "github.com/ethereum/go-ethereum/event"
    "github.com/Determinant/coreth/eth"
    "github.com/Determinant/coreth/node"
    "github.com/ethereum/go-ethereum/crypto"
)

type Tx = types.Transaction
type Block = types.Block
type Hash = common.Hash

type ETHChain struct {
    backend *eth.Ethereum
}



func isLocalBlock(block *types.Block) bool {
    return false
}

func NewETHChain(config *eth.Config, etherBase *common.Address) *ETHChain {
    if config == nil {
        config = &eth.DefaultConfig
    }
    mux := new(event.TypeMux)
    ctx := node.NewServiceContext(mux)
    backend, _ := eth.New(&ctx, config)
    chain := &ETHChain { backend: backend }
    if etherBase == nil {
        etherBase = &common.Address{
            1, 0, 0, 0, 0, 0, 0, 0, 0, 0,
            0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
        }
    }
    backend.SetEtherbase(*etherBase)
    return chain
}

func (self *ETHChain) Start() {
    self.backend.StartMining(0)
}

func (self *ETHChain) Stop() {
    self.backend.StopPart()
}

func (self *ETHChain) AddRemoteTxs(txs []*types.Transaction) []error {
    return self.backend.TxPool().AddRemotes(txs)
}

func (self *ETHChain) AddLocalTxs(txs []*types.Transaction) []error {
    return self.backend.TxPool().AddLocals(txs)
}

type Key struct {
	Address common.Address
	PrivateKey *ecdsa.PrivateKey
}

func NewKeyFromECDSA(privateKeyECDSA *ecdsa.PrivateKey) *Key {
    key := &Key{
        Address:    crypto.PubkeyToAddress(privateKeyECDSA.PublicKey),
        PrivateKey: privateKeyECDSA,
    }
    return key
}

func NewKey(rand io.Reader) (*Key, error) {
    privateKeyECDSA, err := ecdsa.GenerateKey(crypto.S256(), rand)
    if err != nil {
        return nil, err
    }
    return NewKeyFromECDSA(privateKeyECDSA), nil
}