Commit 3c446e88 authored by geo-gs's avatar geo-gs Committed by Luke Champine

Add Unlock Hash From String Utility Function

We should add the ScanAddress and UnlockHashFromAddrStr functions to `types/encoding.go` so that we can access these funstions in `types/constants.go`. Doing so will allow us to supply the unlockHashes in string form which is the form that most users will be familiar with. This in turn will help with ease of documenting code and writing tutorials.
parent 33bf5199
......@@ -251,7 +251,7 @@ func (api *API) explorerHashHandler(w http.ResponseWriter, req *http.Request, ps
// address.
hash, err := scanHash(ps.ByName("hash"))
if err != nil {
addr, err := scanAddress(ps.ByName("hash"))
addr, err := types.ScanAddress(ps.ByName("hash"))
if err != nil {
WriteError(w, Error{err.Error()}, http.StatusBadRequest)
return
......
......@@ -19,15 +19,6 @@ func scanAmount(amount string) (types.Currency, bool) {
return types.NewCurrency(i), true
}
// scanAddress scans a types.UnlockHash from a string.
func scanAddress(addrStr string) (addr types.UnlockHash, err error) {
err = addr.LoadString(addrStr)
if err != nil {
return types.UnlockHash{}, err
}
return addr, nil
}
// scanHash scans a crypto.Hash from a string.
func scanHash(s string) (h crypto.Hash, err error) {
err = h.LoadString(s)
......
......@@ -483,7 +483,7 @@ func (api *API) walletSiacoinsHandler(w http.ResponseWriter, req *http.Request,
WriteError(w, Error{"could not read amount from POST call to /wallet/siacoins"}, http.StatusBadRequest)
return
}
dest, err := scanAddress(req.FormValue("destination"))
dest, err := types.ScanAddress(req.FormValue("destination"))
if err != nil {
WriteError(w, Error{"could not read address from POST call to /wallet/siacoins"}, http.StatusBadRequest)
return
......@@ -513,7 +513,7 @@ func (api *API) walletSiafundsHandler(w http.ResponseWriter, req *http.Request,
WriteError(w, Error{"could not read 'amount' from POST call to /wallet/siafunds"}, http.StatusBadRequest)
return
}
dest, err := scanAddress(req.FormValue("destination"))
dest, err := types.ScanAddress(req.FormValue("destination"))
if err != nil {
WriteError(w, Error{"error when calling /wallet/siafunds: " + err.Error()}, http.StatusBadRequest)
return
......
......@@ -210,11 +210,11 @@ func init() {
GenesisSiafundAllocation = []SiafundOutput{
{
Value: NewCurrency64(2000),
UnlockHash: UnlockHash{214, 166, 197, 164, 29, 201, 53, 236, 106, 239, 10, 158, 127, 131, 20, 138, 63, 221, 230, 16, 98, 247, 32, 77, 210, 68, 116, 12, 241, 89, 27, 223},
UnlockHash: UnlockHashFromAddrStr("d6a6c5a41dc935ec6aef0a9e7f83148a3fdde61062f7204dd244740cf1591bdfc10dca990dd5"),
},
{
Value: NewCurrency64(7000),
UnlockHash: UnlockHash{209, 246, 228, 60, 248, 78, 242, 110, 9, 8, 227, 248, 225, 216, 163, 52, 142, 93, 47, 176, 103, 41, 137, 80, 212, 8, 132, 58, 241, 189, 2, 17},
UnlockHash: UnlockHashFromAddrStr("7d0c44f7664e2d34e53efde0661a6f628ec9264785ae8e3cd7c973e8d190c3c97b5e3ecbc567"),
},
{
Value: NewCurrency64(1000),
......@@ -258,11 +258,11 @@ func init() {
GenesisSiafundAllocation = []SiafundOutput{
{
Value: NewCurrency64(2000),
UnlockHash: UnlockHash{214, 166, 197, 164, 29, 201, 53, 236, 106, 239, 10, 158, 127, 131, 20, 138, 63, 221, 230, 16, 98, 247, 32, 77, 210, 68, 116, 12, 241, 89, 27, 223},
UnlockHash: UnlockHashFromAddrStr("d6a6c5a41dc935ec6aef0a9e7f83148a3fdde61062f7204dd244740cf1591bdfc10dca990dd5"),
},
{
Value: NewCurrency64(7000),
UnlockHash: UnlockHash{209, 246, 228, 60, 248, 78, 242, 110, 9, 8, 227, 248, 225, 216, 163, 52, 142, 93, 47, 176, 103, 41, 137, 80, 212, 8, 132, 58, 241, 189, 2, 17},
UnlockHash: UnlockHashFromAddrStr("7d0c44f7664e2d34e53efde0661a6f628ec9264785ae8e3cd7c973e8d190c3c97b5e3ecbc567"),
},
{
Value: NewCurrency64(1000),
......
......@@ -1016,3 +1016,21 @@ func (uh *UnlockHash) Scan(s fmt.ScanState, ch rune) error {
}
return uh.LoadString(string(tok))
}
// ScanAddress scans a types.UnlockHash from a string.
func ScanAddress(addrStr string) (addr UnlockHash, err error) {
err = addr.LoadString(addrStr)
if err != nil {
return UnlockHash{}, err
}
return addr, nil
}
// UnlockHashFromAddrStr convert string address to UnlockHash
func UnlockHashFromAddrStr(addrStr string) (addr UnlockHash) {
dest, err := ScanAddress(addrStr)
if err != nil {
return UnlockHash{}
}
return dest
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment