Got v2 coinbase tx's to parse properly

This commit is contained in:
Jimmy Song 2017-05-12 20:37:42 -07:00
parent e5151327a2
commit 435925b60c
3 changed files with 41 additions and 13 deletions

View file

@ -133,6 +133,11 @@ func (r *RctSigBase) SerializeBase() (result []byte) {
return
}
func (r *RctSigBase) BaseHash() (result Hash) {
result = Keccak256(r.SerializeBase())
return
}
func (r *RctSig) SerializePrunable() (result []byte) {
if r.sigType == RCTTypeNull {
return
@ -146,6 +151,14 @@ func (r *RctSig) SerializePrunable() (result []byte) {
return
}
func (r *RctSig) PrunableHash() (result Hash) {
if r.sigType == RCTTypeNull {
return
}
result = Keccak256(r.SerializePrunable())
return
}
func ParseKey(buf io.Reader) (result Key, err error) {
key := make([]byte, KeyLength)
if _, err = buf.Read(key); err != nil {
@ -202,13 +215,13 @@ func ParseRingCtSignature(buf io.Reader, nInputs, nOutputs, nMixin int) (result
return
}
r.sigType = uint8(sigType[0])
if r.sigType != RCTTypeNull || r.sigType != RCTTypeFull || r.sigType != RCTTypeSimple {
err = fmt.Errorf("Bad sigType %d", r.sigType)
}
if r.sigType == RCTTypeNull {
result = r
return
}
if r.sigType != RCTTypeFull || r.sigType != RCTTypeSimple {
err = fmt.Errorf("Bad sigType %d", r.sigType)
}
r.txFee, err = ReadVarInt(buf)
if err != nil {
return

View file

@ -236,9 +236,9 @@ func (t *Transaction) GetHash() (result Hash) {
// version 2 requires first computing 3 separate hashes
// prefix, rctBase and rctPrunable
// and then hashing the hashes together to get the final hash
prefixHash := Keccak256(t.SerializePrefix())
rctBaseHash := Keccak256(t.rctSignature.SerializeBase())
rctPrunableHash := Keccak256(t.rctSignature.SerializePrunable())
prefixHash := t.PrefixHash()
rctBaseHash := t.rctSignature.BaseHash()
rctPrunableHash := t.rctSignature.PrunableHash()
result = Keccak256(prefixHash[:], rctBaseHash[:], rctPrunableHash[:])
}
return
@ -437,13 +437,17 @@ func ParseTransaction(buf io.Reader) (transaction *Transaction, err error) {
if err != nil {
return
}
if version == 1 {
if t.version == 1 {
t.signatures, err = ParseSignatures(mixinLengths, buf)
if err != nil {
return
}
} else {
t.rctSignature, err = ParseRingCtSignature(buf, int(numInputs), int(numOutputs), mixinLengths[0]-1)
var nMixins int
if len(mixinLengths) > 0 {
nMixins = mixinLengths[0] - 1
}
t.rctSignature, err = ParseRingCtSignature(buf, int(numInputs), int(numOutputs), nMixins)
if err != nil {
return
}

View file

@ -93,20 +93,31 @@ func TestCoinbaseTransaction(t *testing.T) {
"ee1a068ee5d4bfe3414e89f9ba05b5b1d899060dc609025f03a49ef9a4d9daa3",
},
},
{
name: "block 1302238",
txHex: "029abe4f01ffdebd4f01a3caca99eaea01021804724f0b0938d83473fcb7fbb93a2991ec98139020b2fe8e8dcaf65888d3dd2b0141d60c73bd6cfd6eddd30039279aefba252747167e5e77ad1fd373916d2a273f020800000049259da0dd00",
hashHex: "be30ee0ac38d83c86d84326c64b13eea5b40897a321004d17e589241d49199f7",
version: 2,
outputSum: 8068686587171,
blockNum: 1302238,
outputKeys: []string{
"1804724f0b0938d83473fcb7fbb93a2991ec98139020b2fe8e8dcaf65888d3dd",
},
},
}
for _, test := range tests {
serializedTx, _ := hex.DecodeString(test.txHex)
expectedHash, _ := hex.DecodeString(test.hashHex)
hash := Keccak256(serializedTx)
if bytes.Compare(expectedHash, hash[:]) != 0 {
t.Errorf("%s: want %x, got %x", test.name, expectedHash, hash)
}
buffer := new(bytes.Buffer)
buffer.Write(serializedTx)
transaction, err := ParseTransaction(buffer)
if err != nil {
t.Errorf("%s: error parsing tx: %s", test.name, err)
}
wantHash := HexToBytes(test.hashHex)
gotHash := transaction.GetHash()
if wantHash != gotHash {
t.Errorf("%s: want %x, got %x", test.name, wantHash, gotHash)
}
if test.version != transaction.version {
t.Errorf("%s: version: want %d, got %d", test.name, test.version, transaction.version)
}