/
account.go
186 lines (175 loc) · 5.3 KB
/
account.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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
package account
import (
"fmt"
dht "github.com/archoncloud/archon-dht/archon"
"github.com/dustin/go-humanize"
"github.com/archoncloud/archoncloud-go/blockchainAPI/neo"
. "github.com/archoncloud/archoncloud-go/common"
ifc "github.com/archoncloud/archoncloud-go/interfaces"
"github.com/archoncloud/archon-dht/permission_layer"
"github.com/archoncloud/archoncloud-go/shards"
"github.com/pkg/errors"
"io"
"io/ioutil"
"os"
"strings"
)
func NewIAccount(walletPath string, password string) (iaccount ifc.IAccount, err error) {
path := DefaultToExecutable(walletPath)
if !FileExists(path) {
err = fmt.Errorf("Cannot find file %q", path)
return
}
buf, err := ioutil.ReadFile(DefaultToExecutable(walletPath))
if err != nil {return}
s := string(buf)
if strings.Contains(s, `"contract"`) && strings.Contains(s, `"scrypt"`) {
// Neo
return NewNeoAccount(walletPath,password)
}
if strings.Contains(s, `"crypto"`) && strings.Contains(s, `"ciphertext"`) {
// Eth
return NewEthAccount(walletPath,password)
}
err = fmt.Errorf("unknown wallet type")
return
}
func ArchonSignatureFor(r io.Reader, acc ifc.IAccount) (string, []byte, int64, error) {
hash, n, err := GetArchonHashOf(r)
if err != nil {return "", nil, 0, err}
sig, err := acc.Sign(hash)
return BytesToString(hash), sig, n, err
}
func PermLayerID(acc ifc.IAccount) permission_layer.PermissionLayerID {
if acc != nil {
switch acc.GetAccountType() {
case ifc.EthAccountType: return permission_layer.EthPermissionId
case ifc.NeoAccountType: return permission_layer.NeoPermissionId
}
}
return permission_layer.NotPermissionId
}
func IsEth(acc ifc.IAccount) bool {
return acc.GetAccountType() == ifc.EthAccountType;
}
func PrettyCurrencyForAccount(acc ifc.IAccount, amount int64) string {
var p string
switch acc.GetAccountType() {
case ifc.EthAccountType:
p = WeiString(amount)
case ifc.NeoAccountType:
p = neo.CgasString(amount)
default:
return ""
}
f := float64(amount)
f /= float64(acc.HundredthOfCent() * 100)
if f > 100.0 {
dollars := humanize.CommafWithDigits(f/100.0, 2)
p += fmt.Sprintf(" ($%s)", dollars)
} else {
cents := humanize.CommafWithDigits(f, 6)
p += fmt.Sprintf(" (%sc)", cents)
}
return p
}
func confirmPrice(acc ifc.IAccount, numBytes int64, sps StorageProviders, maxPayment int64) (price int64, err error) {
// For Eth, the price is in Wei for Neo in CGAS
price = sps.PriceOfUpload(numBytes)
if maxPayment > 0 {
if maxPayment < price {
err = fmt.Errorf("price (%d) exceeds MaxPayment (%d)", price, maxPayment)
return
}
}
priceS := acc.PrettyCurrency(price)
if maxPayment >= 0 {
fmt.Printf("Charge is=%s\n", priceS)
} else if !Yes(fmt.Sprintf("You will be charged %s for the upload. Is this OK?", priceS)) {
// Rejected
os.Exit(2)
}
return
}
func GenerateNewWalletFile(accT ifc.AccountType, showPassword bool) {
var ns, nl, cur string
switch accT {
case ifc.EthAccountType:
ns = "Eth"
nl = "Ethereum"
cur = "Ethers"
case ifc.NeoAccountType:
ns = "Neo"
nl = "Neo"
cur = "Gas"
default: return
}
path := DefaultToExecutable(fmt.Sprintf("new%sWallet.json", ns))
password := GetPassword("Password for new Ethereum wallet to be generated", showPassword)
if password == "" {
AbortWithString("Aborted")
}
confirm := GetPassword("Confirm passord", showPassword)
if password != confirm {
AbortWithString("Passwords don't match")
}
var err error
private := ""
switch accT {
case ifc.EthAccountType:
if Yes("Do you want to provide an existing private key") {
private = PromptForInput("Enter your private key")
if private == "" {
AbortWithString("Aborted")
}
}
err = GenerateNewEthWallet(path,password,private)
case ifc.NeoAccountType:
// The WIF format is to add prefix 0x80 and suffix 0x01 in the original 32 bytes private key, and get string of Base58Check encoding.
// private key: 32 bytes (UInt256)
if Yes("Do you want to provide an existing WIF string") {
private = PromptForInput("Enter your WIF string")
if private == "" {
AbortWithString("Aborted")
}
}
err = GenerateNewNeoWallet(path,password,private)
}
Abort(err)
acc, err := NewIAccount(path, password)
Abort(err)
fmt.Printf("%s wallet generated in %q\n", nl, path)
fmt.Printf("Please remember the password you just entered\n")
fmt.Printf("Rename this file as you wish and then enter it in the .config file\n")
if private != "" {
fmt.Printf("You may need to transfer %s into it before you can use it\n", cur)
fmt.Printf("The account address is: %q\n", acc.AddressString())
}
}
// if maxPayment is negative, then the user will be prompted
func ProposeUpload(acc ifc.IAccount, fc *shards.FileContainer, s *shards.ShardsContainer, a *ArchonUrl, sps StorageProviders, maxPayment int64) (txId string, price int64, err error) {
switch acc.GetAccountType() {
case ifc.EthAccountType:
ea := acc.(*EthAccount)
return ea.ProposeUpload(fc,s, a, sps, maxPayment)
case ifc.NeoAccountType:
na := acc.(*NeoAccount)
txIds, price2, err2 := na.ProposeUpload(fc,s, a, sps, maxPayment)
if err2 != nil {
err = err2
return
}
price = price2
if len(sps) == 1 {
txId = txIds[sps[0].Address]
}
}
err = errors.New("unknown account type")
return
}
func GetNodeId(acc ifc.IAccount) (nodeId string, err error) {
nId, err := dht.GetNodeID(ifc.GetSeed(acc))
if err != nil {return}
nodeId = nId.Pretty()
return
}