parser: implement transaction GetHash()
Manually tested against the blocks in testdata.
This commit is contained in:
parent
f42dea2b1e
commit
768e5242dc
|
@ -1,6 +1,8 @@
|
||||||
package parser
|
package parser
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"crypto/sha256"
|
||||||
|
|
||||||
"github.com/gtank/ctxd/parser/internal/bytestring"
|
"github.com/gtank/ctxd/parser/internal/bytestring"
|
||||||
"github.com/pkg/errors"
|
"github.com/pkg/errors"
|
||||||
)
|
)
|
||||||
|
@ -248,6 +250,29 @@ func (p *joinSplit) ParseFromSlice(data []byte) ([]byte, error) {
|
||||||
|
|
||||||
type transaction struct {
|
type transaction struct {
|
||||||
*rawTransaction
|
*rawTransaction
|
||||||
|
rawBytes []byte
|
||||||
|
txId []byte
|
||||||
|
}
|
||||||
|
|
||||||
|
func (tx *transaction) GetHash() []byte {
|
||||||
|
if tx.txId != nil {
|
||||||
|
return tx.txId
|
||||||
|
}
|
||||||
|
|
||||||
|
// SHA256d
|
||||||
|
digest := sha256.Sum256(tx.rawBytes)
|
||||||
|
digest = sha256.Sum256(digest[:])
|
||||||
|
|
||||||
|
// Reverse byte order
|
||||||
|
for i := 0; i < len(digest)/2; i++ {
|
||||||
|
j := len(digest) - 1 - i
|
||||||
|
digest[i], digest[j] = digest[j], digest[i]
|
||||||
|
}
|
||||||
|
|
||||||
|
tx.txId = digest[:]
|
||||||
|
return tx.txId
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (tx *transaction) ParseFromSlice(data []byte) ([]byte, error) {
|
func (tx *transaction) ParseFromSlice(data []byte) ([]byte, error) {
|
||||||
|
@ -391,6 +416,10 @@ func (tx *transaction) ParseFromSlice(data []byte) ([]byte, error) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// TODO: implement rawBytes with MarshalBinary() instead
|
||||||
|
txLen := len(data) - len(s)
|
||||||
|
tx.rawBytes = data[:txLen]
|
||||||
|
|
||||||
return []byte(s), nil
|
return []byte(s), nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue