OSDN Git Service

fj
authorHAOYUatHZ <haoyu@protonmail.com>
Mon, 17 Jun 2019 08:42:06 +0000 (16:42 +0800)
committerHAOYUatHZ <haoyu@protonmail.com>
Mon, 17 Jun 2019 08:42:06 +0000 (16:42 +0800)
federation/util/hash.go
main.go

index 570cb2e..e077602 100644 (file)
@@ -1,20 +1,27 @@
 package util
 
 import (
-       "encoding/hex"
+       // "encoding/hex"
 
-       "github.com/vapor/errors"
+       // "github.com/vapor/errors"
        "github.com/vapor/protocol/bc"
 )
 
-func stringToAssetID(s string) (*bc.AssetID, error) {
-       h, err := hex.DecodeString(s)
-       if err != nil {
-               return nil, errors.Wrap(err, "decode asset string")
+func StringToAssetID(s string) (*bc.Hash, error) {
+       // h, err := hex.DecodeString(s)
+       // if err != nil {
+       //      return nil, errors.Wrap(err, "decode asset string")
+       // }
+
+       // panic(len(h))
+
+       // var b [32]byte
+       // copy(b[:], h)
+       // assetID := bc.NewAssetID(b)
+       assetID := &bc.Hash{}
+       if err := assetID.UnmarshalText([]byte(s)); err != nil {
+               panic(err)
        }
 
-       var b [32]byte
-       copy(b[:], h)
-       assetID := bc.NewAssetID(b)
-       return &assetID, nil
+       return assetID, nil
 }
diff --git a/main.go b/main.go
index e6bcfa2..5c4888e 100644 (file)
--- a/main.go
+++ b/main.go
@@ -16,11 +16,16 @@ import (
        "github.com/vapor/federation/common"
        "github.com/vapor/federation/database/orm"
        "github.com/vapor/federation/service"
+       "github.com/vapor/federation/util"
        vaporTypes "github.com/vapor/protocol/bc/types"
        // "github.com/vapor/federation/synchron"
 )
 
 func main() {
+       str := "ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff"
+       asset, _ := util.StringToAssetID(str)
+       fmt.Println(asset)
+
        dsnTemplate := "%s:%s@tcp(%s:%d)/%s?charset=utf8&parseTime=true&loc=Local"
        dsn := fmt.Sprintf(dsnTemplate, "root", "toor", "127.0.0.1", 3306, "federation")
        db, err := gorm.Open("mysql", dsn)