github.com/inklabsfoundation/inkchain@v0.17.1-0.20181025012015-c3cef8062f19/core/wallet/wallet.go (about) 1 /* 2 Copyright Ziggurat Corp. 2017 All Rights Reserved. 3 4 SPDX-License-Identifier: Apache-2.0 5 */ 6 7 package wallet 8 9 import ( 10 "encoding/hex" 11 "math/big" 12 "strings" 13 ) 14 15 const ( 16 HashLength = 32 17 AddressLength = 20 18 HashStringLength = 64 19 AddressStringLength = 41 20 PriKeyLength = 32 21 PriKeyStringLength = 64 22 ADDRESS_PREFIX = "i" 23 WALLET_NAMESPACE = "ink" 24 MAIN_BALANCE_NAME = "INK" 25 ) 26 27 type Hash [HashLength]byte 28 type Address [AddressLength]byte 29 30 var InkMinimumFee *big.Int 31 var SignPrivateKey string 32 33 type Account struct { 34 Balance map[string]*big.Int `json:"balance"` 35 Counter uint64 `json:"counter"` 36 } 37 38 type TxData struct { 39 Sender *Address `json:"from"` 40 Recipient *Address `json:"to"` 41 BalanceType string `json:"balanceType"` 42 Amount *big.Int `json:"amount"` 43 } 44 45 // type GenAccount 46 type Token struct { 47 // token name 48 Name string `json:"tokenName"` 49 // total supply of the token 50 totalSupply *big.Int `json:"totalSupply"` 51 // initial address to issue 52 Address string `json:"address"` 53 // token status : Created, Delivered, Invalidate 54 Status string `json:"status"` 55 // token decimals 56 Decimals int `json:"decimals"` 57 } 58 59 func (a *Address) SetBytes(b []byte) { 60 if len(b) > AddressLength { 61 b = b[len(b)-AddressLength:] 62 } 63 copy(a[AddressLength-len(b):], b) 64 } 65 66 func BytesToAddress(b []byte) *Address { 67 a := Address{} 68 a.SetBytes(b) 69 return &a 70 } 71 72 func (a *Address) ToBytes() []byte { 73 return a[:] 74 } 75 76 func StringToAddress(b string) *Address { 77 if !strings.HasPrefix(b, ADDRESS_PREFIX) { 78 return nil 79 } 80 c := strings.TrimLeft(b, ADDRESS_PREFIX) 81 a := Address{} 82 bytes, err := hex.DecodeString(strings.ToLower(c)) 83 if err != nil { 84 return nil 85 } 86 a.SetBytes(bytes) 87 return &a 88 } 89 90 func (a *Address) ToString() string { 91 return string(ADDRESS_PREFIX + hex.EncodeToString(a[:])) 92 } 93 94 func (a *Hash) SetBytes(b []byte) { 95 if len(b) > HashLength { 96 b = b[len(b)-HashLength:] 97 } 98 copy(a[HashLength-len(b):], b) 99 } 100 101 func BytesToHash(b []byte) *Hash { 102 a := Hash{} 103 a.SetBytes(b) 104 return &a 105 } 106 107 func (a *Hash) ToBytes() []byte { 108 return a[:] 109 } 110 111 func SignatureStringToBytes(sig string) ([]byte, error) { 112 return hex.DecodeString(sig) 113 } 114 115 func SignatureBytesToString(sig []byte) string { 116 return hex.EncodeToString(sig) 117 }