parser: finish first pass transaction parsing
This commit is contained in:
parent
4bd20d9dab
commit
dfb05c261d
|
@ -16,8 +16,7 @@ type rawTransaction struct {
|
||||||
valueBalance int64
|
valueBalance int64
|
||||||
shieldedSpends []*spend
|
shieldedSpends []*spend
|
||||||
shieldedOutputs []*output
|
shieldedOutputs []*output
|
||||||
joinSplitsPHGR13 []*phgr13JoinSplit
|
joinSplits []*joinSplit
|
||||||
joinSplitsGroth16 []*groth16JoinSplit
|
|
||||||
joinSplitPubKey []byte
|
joinSplitPubKey []byte
|
||||||
joinSplitSig []byte
|
joinSplitSig []byte
|
||||||
bindingSig []byte
|
bindingSig []byte
|
||||||
|
@ -165,12 +164,86 @@ func (p *output) ParseFromSlice(data []byte) ([]byte, error) {
|
||||||
return []byte(s), nil
|
return []byte(s), nil
|
||||||
}
|
}
|
||||||
|
|
||||||
type phgr13JoinSplit struct {
|
// joinSplit is a JoinSplit description as described in 7.2 of the Zcash
|
||||||
// something
|
// protocol spec. Its exact contents differ by transaction version and network
|
||||||
|
// upgrade level.
|
||||||
|
type joinSplit struct {
|
||||||
|
vpubOld uint64
|
||||||
|
vpubNew uint64
|
||||||
|
anchor []byte // 32
|
||||||
|
nullifiers [2][]byte // 64 [N_old][32]byte
|
||||||
|
commitments [2][]byte // 64 [N_new][32]byte
|
||||||
|
ephemeralKey []byte // 32
|
||||||
|
randomSeed []byte // 32
|
||||||
|
vmacs [2][]byte // 64 [N_old][32]byte
|
||||||
|
proofPHGR13 []byte // 296
|
||||||
|
proofGroth16 []byte // 192
|
||||||
|
encCiphertexts []byte // 1202 [N_new][601]byte
|
||||||
|
|
||||||
|
// not actually in the format, but needed for parsing
|
||||||
|
version uint32
|
||||||
}
|
}
|
||||||
|
|
||||||
type groth16JoinSplit struct {
|
func (p *JoinSplit) ParseFromSlice(data []byte) ([]byte, error) {
|
||||||
// something
|
s := bytestring.String(data)
|
||||||
|
|
||||||
|
if ok := s.ReadUint64(&p.vpubOld); !ok {
|
||||||
|
return nil, errors.New("could not read vpubOld")
|
||||||
|
}
|
||||||
|
|
||||||
|
if ok := s.ReadUint64(&p.vpubNew); !ok {
|
||||||
|
return nil, errors.New("could not read vpubNew")
|
||||||
|
}
|
||||||
|
|
||||||
|
if ok := s.ReadBytes(&p.anchor, 32); !ok {
|
||||||
|
return nil, errors.New("could not read anchor")
|
||||||
|
}
|
||||||
|
|
||||||
|
for i := 0; i < 2; i++ {
|
||||||
|
if ok := s.ReadBytes(&p.nullifiers[i], 32); !ok {
|
||||||
|
return nil, errors.New("could not read a nullifier")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
for i := 0; i < 2; i++ {
|
||||||
|
if ok := s.ReadBytes(&p.commitments[i], 32); !ok {
|
||||||
|
return nil, errors.New("could not read a commitment")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if ok := s.ReadBytes(&p.ephemeralKey, 32); !ok {
|
||||||
|
return nil, errors.New("could not read ephemeralKey")
|
||||||
|
}
|
||||||
|
|
||||||
|
if ok := s.ReadBytes(&p.randomSeed, 32); !ok {
|
||||||
|
return nil, errors.New("could not read randomSeed")
|
||||||
|
}
|
||||||
|
|
||||||
|
for i := 0; i < 2; i++ {
|
||||||
|
if ok := s.ReadBytes(&p.vmacs[i], 32); !ok {
|
||||||
|
return nil, errors.New("could not read a vmac")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if p.version == 2 || p.version == 3 {
|
||||||
|
if ok := s.ReadBytes(&p.proofPHGR13, 296); !ok {
|
||||||
|
return nil, errors.New("could not read PHGR13 proof")
|
||||||
|
}
|
||||||
|
} else if p.version >= 4 {
|
||||||
|
if ok := s.ReadBytes(&p.proofGroth16, 192); !ok {
|
||||||
|
return nil, errors.New("could not read Groth16 proof")
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
return nil, errors.New("unexpected transaction version")
|
||||||
|
}
|
||||||
|
|
||||||
|
for i := 0; i < 2; i++ {
|
||||||
|
if ok := s.ReadBytes(&p.encCiphertexts[i], 601); !ok {
|
||||||
|
return nil, errors.New("could not read an encCiphertext")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return []byte(s), nil
|
||||||
}
|
}
|
||||||
|
|
||||||
type transaction struct {
|
type transaction struct {
|
||||||
|
@ -188,9 +261,11 @@ func (tx *transaction) ParseFromSlice(data []byte) ([]byte, error) {
|
||||||
tx.fOverwintered = (header >> 31) == 1
|
tx.fOverwintered = (header >> 31) == 1
|
||||||
tx.version = header & 0x7FFFFFFF
|
tx.version = header & 0x7FFFFFFF
|
||||||
|
|
||||||
|
if tx.version >= 3 {
|
||||||
if ok := s.ReadUint32(&tx.nVersionGroupId); !ok {
|
if ok := s.ReadUint32(&tx.nVersionGroupId); !ok {
|
||||||
return nil, errors.New("could not read nVersionGroupId")
|
return nil, errors.New("could not read nVersionGroupId")
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
var txInCount uint64
|
var txInCount uint64
|
||||||
if ok := s.ReadCompactSize(&txInCount); !ok {
|
if ok := s.ReadCompactSize(&txInCount); !ok {
|
||||||
|
@ -201,41 +276,42 @@ func (tx *transaction) ParseFromSlice(data []byte) ([]byte, error) {
|
||||||
// TODO: vector. At the moment we're assuming trusted input.
|
// TODO: vector. At the moment we're assuming trusted input.
|
||||||
// See https://nvd.nist.gov/vuln/detail/CVE-2018-17144 for an example.
|
// See https://nvd.nist.gov/vuln/detail/CVE-2018-17144 for an example.
|
||||||
|
|
||||||
txInputs := make([]*txIn, txInCount)
|
tx.transparentInputs = make([]*txIn, txInCount)
|
||||||
for i := 0; i < txInCount; i++ {
|
for i := 0; i < txInCount; i++ {
|
||||||
ti := &txIn{}
|
ti := &txIn{}
|
||||||
s, err = ti.ParseFromSlice([]byte(s))
|
s, err = ti.ParseFromSlice([]byte(s))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, errors.Wrap(err, "while parsing transparent input")
|
return nil, errors.Wrap(err, "while parsing transparent input")
|
||||||
}
|
}
|
||||||
txInputs[i] = ti
|
tx.transparentInputs[i] = ti
|
||||||
}
|
}
|
||||||
tx.transparentInputs = txInputs
|
|
||||||
|
|
||||||
var txOutCount uint64
|
var txOutCount uint64
|
||||||
if ok := s.ReadCompactSize(&txOutCount); !ok {
|
if ok := s.ReadCompactSize(&txOutCount); !ok {
|
||||||
return nil, errors.New("could not read tx_out_count")
|
return nil, errors.New("could not read tx_out_count")
|
||||||
}
|
}
|
||||||
|
|
||||||
txOutputs := make([]*txOut, txOutCount)
|
tx.transparentOutputs = make([]*txOut, txOutCount)
|
||||||
for i := 0; i < txOutCount; i++ {
|
for i := 0; i < txOutCount; i++ {
|
||||||
to := &txOut{}
|
to := &txOut{}
|
||||||
s, err = to.ParseFromSlice([]byte(s))
|
s, err = to.ParseFromSlice([]byte(s))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, errors.Wrap(err, "while parsing transparent output")
|
return nil, errors.Wrap(err, "while parsing transparent output")
|
||||||
}
|
}
|
||||||
txOutputs[i] = to
|
tx.transparentOutputs[i] = to
|
||||||
}
|
}
|
||||||
tx.transparentOutputs = txOutputs
|
|
||||||
|
|
||||||
if ok := s.ReadUint32(&tx.nLockTime); !ok {
|
if ok := s.ReadUint32(&tx.nLockTime); !ok {
|
||||||
return nil, errors.New("could not read nLockTime")
|
return nil, errors.New("could not read nLockTime")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if tx.version >= 3 {
|
||||||
if ok := s.ReadUint32(&tx.nExpiryHeight); !ok {
|
if ok := s.ReadUint32(&tx.nExpiryHeight); !ok {
|
||||||
return nil, errors.New("could not read nExpiryHeight")
|
return nil, errors.New("could not read nExpiryHeight")
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if tx.version >= 4 {
|
||||||
if ok := s.ReadInt64(&tx.valueBalance); !ok {
|
if ok := s.ReadInt64(&tx.valueBalance); !ok {
|
||||||
return nil, errors.New("could not read valueBalance")
|
return nil, errors.New("could not read valueBalance")
|
||||||
}
|
}
|
||||||
|
@ -245,32 +321,66 @@ func (tx *transaction) ParseFromSlice(data []byte) ([]byte, error) {
|
||||||
return nil, errors.New("could not read nShieldedSpend")
|
return nil, errors.New("could not read nShieldedSpend")
|
||||||
}
|
}
|
||||||
|
|
||||||
txSpends := make([]*spend, spendCount)
|
tx.shieldedSpends = make([]*spend, spendCount)
|
||||||
for i := 0; i < spendCount; i++ {
|
for i := 0; i < spendCount; i++ {
|
||||||
newSpend := &spend{}
|
newSpend := &spend{}
|
||||||
s, err = newSpend.ParseFromSlice([]byte(s))
|
s, err = newSpend.ParseFromSlice([]byte(s))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, errors.Wrap(err, "while parsing shielded Spend")
|
return nil, errors.Wrap(err, "while parsing shielded Spend")
|
||||||
}
|
}
|
||||||
txSpends[i] = newSpend
|
tx.shieldedSpends[i] = newSpend
|
||||||
}
|
}
|
||||||
tx.shieldedSpends = txSpends
|
|
||||||
|
|
||||||
var outputCount uint64
|
var outputCount uint64
|
||||||
if ok := s.ReadCompactSize(&outputCount); !ok {
|
if ok := s.ReadCompactSize(&outputCount); !ok {
|
||||||
return nil, errors.New("could not read nShieldedOutput")
|
return nil, errors.New("could not read nShieldedOutput")
|
||||||
}
|
}
|
||||||
|
|
||||||
txOutputs := make([]*output, outputCount)
|
tx.shieldedOutputs = make([]*output, outputCount)
|
||||||
for i := 0; i < outputCount; i++ {
|
for i := 0; i < outputCount; i++ {
|
||||||
newOutput := &output{}
|
newOutput := &output{}
|
||||||
s, err = newOutput.ParseFromSlice([]byte(s))
|
s, err = newOutput.ParseFromSlice([]byte(s))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, errors.Wrap(err, "while parsing shielded Output")
|
return nil, errors.Wrap(err, "while parsing shielded Output")
|
||||||
}
|
}
|
||||||
txOutputs[i] = newOutput
|
tx.shieldedOutputs[i] = newOutput
|
||||||
}
|
}
|
||||||
tx.shieldedOutputs = txOutputs
|
}
|
||||||
|
|
||||||
|
if tx.version >= 2 {
|
||||||
|
var joinSplitCount uint64
|
||||||
|
if ok := s.ReadCompactSize(&joinSplitCount); !ok {
|
||||||
|
return nil, errors.New("could not read nJoinSplit")
|
||||||
|
}
|
||||||
|
|
||||||
|
if joinSplitCount > 0 {
|
||||||
|
tx.joinSplits = make([]*joinSplit, joinSplitCount)
|
||||||
|
for i := 0; i < joinSplitCount; i++ {
|
||||||
|
js := &joinSplit{version: tx.version}
|
||||||
|
s, err = js.ParseFromSlice([]byte(s))
|
||||||
|
if err != nil {
|
||||||
|
return nil, errors.Wrap(err, "while parsing JoinSplit")
|
||||||
|
}
|
||||||
|
tx.joinSplits[i] = js
|
||||||
|
}
|
||||||
|
|
||||||
|
if ok := s.ReadBytes(&tx.joinSplitPubKey, 32); !ok {
|
||||||
|
return nil, errors.New("could not read joinSplitPubKey")
|
||||||
|
}
|
||||||
|
|
||||||
|
if ok := s.ReadBytes(&tx.joinSplitSig, 64); !ok {
|
||||||
|
return nil, errors.New("could not read joinSplitSig")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if tx.version >= 4 && (spendCount+outputCount > 0) {
|
||||||
|
if ok := s.ReadBytes(&tx.bindingSig, 64); !ok {
|
||||||
|
return nil, errors.New("could not read bindingSig")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return []byte(s), nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func newTransaction() *transaction {
|
func newTransaction() *transaction {
|
||||||
|
|
Loading…
Reference in New Issue