diff --git a/wallet/nockhash.go b/wallet/nockhash.go index d6b79da..70023f1 100644 --- a/wallet/nockhash.go +++ b/wallet/nockhash.go @@ -1,6 +1,7 @@ package wallet import ( + "fmt" "math/big" "github.com/btcsuite/btcd/btcutil/base58" @@ -152,23 +153,6 @@ func HashSource(source *nockchain.NockchainSource) [5]uint64 { } } -func HashParent(note *nockchain.NockchainNote) ([5]uint64, error) { - nameHash := HashName(note.Name) - lockHash, err := HashLock(note.Lock) - if err != nil { - return [5]uint64{}, err - } - - sourceHash := HashSource(note.Source) - assetHash := crypto.Tip5HashBelts([]crypto.Belt{{Value: 1}, {Value: uint64(note.Asset)}}) - - hashSourceAsset := crypto.Tip5RehashTenCell(sourceHash, assetHash) - - q := crypto.Tip5RehashTenCell(nameHash, crypto.Tip5RehashTenCell(lockHash, hashSourceAsset)) - - return q, nil -} - func HashSeedWithoutSource(seed *nockchain.NockchainSeed) ([5]uint64, error) { lockHash, err := HashLock(seed.Recipient) if err != nil { @@ -214,6 +198,10 @@ func HashNonce(pkPoint crypto.CheetahPoint, message [5]uint64) ([]crypto.Belt, [ func HashSpend(spend *nockchain.NockchainSpend) ([5]uint64, error) { // TODO: handle multiple sig + if len(spend.Signatures) == 0 { + return [5]uint64{}, fmt.Errorf("signatures can not be empty") + } + sigHash, err := HashSignature(spend.Signatures[0]) if err != nil { return [5]uint64{}, err @@ -386,13 +374,16 @@ func ParseBalanceEntry(entry *nockchain.BalanceEntry) nockchain.NockchainNote { }, Source: &nockchain.NockchainSource{ Source: crypto.Tip5HashToBase58(sourceHash), - IsCoinbase: entry.Note.Source.Coinbase, + IsCoinbase: true, }, Asset: entry.Note.Assets.Value, } } func ParseTimelockIntent(timelock *nockchain.TimeLockIntent) *nockchain.TimelockIntent { + if timelock == nil { + return nil + } switch timelock.Value.(type) { case *nockchain.TimeLockIntent_Absolute: return &nockchain.TimelockIntent{ diff --git a/wallet/service.go b/wallet/service.go index 417b0f8..ebde3df 100644 --- a/wallet/service.go +++ b/wallet/service.go @@ -331,20 +331,11 @@ func (h *GprcHandler) CreateTx(ctx context.Context, req *nockchain.CreateTxReque var masterKey *crypto.MasterKey chainCode := base58.Decode(req.ChainCode) key := base58.Decode(req.Key) - _, err := crypto.CheetaPointFromBytes(key) + masterKey, err := crypto.MasterKeyFromPrivKey(chainCode, key) if err != nil { - // priv key - masterKey, err = crypto.MasterKeyFromPrivKey(chainCode, key) - if err != nil { - return nil, err - } - } else { - masterKey = &crypto.MasterKey{ - PrivateKey: []byte{}, - PublicKey: key, - ChainCode: chainCode, - } + return nil, err } + if !req.IsMasterKey { index := req.Index if index > 1<<32 { @@ -399,7 +390,7 @@ func (h *GprcHandler) CreateTx(ctx context.Context, req *nockchain.CreateTxReque return nil, fmt.Errorf("note not enough balance") } - parentHash, err := HashParent(notes[i]) + parentHash, err := HashNote(notes[i]) if err != nil { return nil, err } @@ -437,20 +428,18 @@ func (h *GprcHandler) CreateTx(ctx context.Context, req *nockchain.CreateTxReque return nil, err } - if len(masterKey.PrivateKey) != 0 { - // sign - chalT8, sigT8, err := ComputeSig(*masterKey, msg) - if err != nil { - return nil, err - } + // sign + chalT8, sigT8, err := ComputeSig(*masterKey, msg) + if err != nil { + return nil, err + } - spend.Signatures = []*nockchain.NockchainSignature{ - { - Pubkey: base58.Encode(masterKey.PublicKey), - Chal: chalT8[:], - Sig: sigT8[:], - }, - } + spend.Signatures = []*nockchain.NockchainSignature{ + { + Pubkey: base58.Encode(masterKey.PublicKey), + Chal: chalT8[:], + Sig: sigT8[:], + }, } input := nockchain.NockchainInput{