This repository has been archived by the owner on Dec 6, 2019. It is now read-only.
forked from smilofoundation/go-smilo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
api_vault.go
101 lines (88 loc) · 3.19 KB
/
api_vault.go
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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
// Copyright 2019 The go-smilo Authors
// Copyright 2015 The go-ethereum Authors
// This file is part of the go-ethereum library.
//
// The go-ethereum library is free software: you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// The go-ethereum library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public License
// along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
package ethapi
import (
"context"
"encoding/hex"
"fmt"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/common/hexutil"
"github.com/ethereum/go-ethereum/log"
"github.com/ethereum/go-ethereum/rlp"
"go-smilo/src/blockchain/smilobft/core/types"
"go-smilo/src/blockchain/smilobft/vault"
)
// SendRawTxArgs represents the arguments to submit a new signed private transaction into the transaction pool.
type VaultSendRawTxArgs struct {
SharedWith []string `json:"sharedWith"`
}
// SendRawTransactionVault will add the signed transaction to the Vault and to the transaction pool.
// The sender is responsible for signing the transaction and using the correct nonce.
func (s *PublicTransactionPoolAPI) SendRawTransactionVault(ctx context.Context, encodedTx hexutil.Bytes, args VaultSendRawTxArgs) (common.Hash, error) {
if vault.VaultInstance == nil {
return common.Hash{}, fmt.Errorf("vault is not enabled")
}
tx := new(types.Transaction)
if err := rlp.DecodeBytes(encodedTx, tx); err != nil {
return common.Hash{}, err
}
data := tx.Data()
isVault := args.SharedWith != nil
if isVault {
if len(data) > 0 {
log.Info("sending vault tx", "data", fmt.Sprintf("%x", data), "vaultfrom", args.SharedWith, "sharedwith", args.SharedWith)
data, err := vault.VaultInstance.PostRawTransaction(data, args.SharedWith)
log.Info("sent vault tx", "data", fmt.Sprintf("%x", data), "vaultfrom", args.SharedWith, "sharedwith", args.SharedWith)
if err != nil {
return common.Hash{}, err
}
}
} else {
return common.Hash{}, fmt.Errorf("transaction is not vault type")
}
return SubmitTransaction(ctx, s.b, tx, isVault)
}
// Get the Vault Transaction content
func (s *PublicBlockChainAPI) GetVaultTransaction(digestHex string) (data string, err error) {
if vault.VaultInstance == nil {
err = fmt.Errorf("vault is not enabled")
return data, err
}
if len(digestHex) < 3 {
err = fmt.Errorf("invalid digest hex")
return data, err
}
if digestHex[:2] == "0x" {
digestHex = digestHex[2:]
}
var b []byte
b, err = hex.DecodeString(digestHex)
if err != nil {
return data, err
}
if len(b) != 64 {
err = fmt.Errorf("expected a digest of length 64, but got %d", len(b))
return data, err
}
var responseData []byte
responseData, err = vault.VaultInstance.Get(b)
if err != nil {
return data, err
}
data = fmt.Sprintf("0x%x", responseData)
return data, nil
}