Skip to content

Commit

Permalink
fix nil ptr ref
Browse files Browse the repository at this point in the history
  • Loading branch information
rohenaz committed Mar 20, 2023
1 parent 538adaf commit cc5f3ad
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 18 deletions.
40 changes: 22 additions & 18 deletions bob.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,35 +87,39 @@ func (b *Boost) FromTape(tape *bpu.Tape) (err error) {
// set difficulty
b.Spend.Difficulty = *flt

if len(tape.Cell) > 20 && tape.Cell[18].Ops != nil && (*tape.Cell[18].Ops == "OP_5" || *tape.Cell[19].Ops == "OP_5") {
if len(tape.Cell) > 20 && (tape.Cell[18].Ops != nil && *tape.Cell[18].Ops == "OP_5") || (tape.Cell[19].Ops != nil && *tape.Cell[19].Ops == "OP_5") {
b.Spend.Version = 1
} else if len(tape.Cell) > 20 && tape.Cell[18].Ops != nil && (*tape.Cell[18].Ops == "OP_6" || *tape.Cell[19].Ops == "OP_6") {
b.Spend.Version = 2
}

// Set topic / tag
dataTagB, errDataTag := base64.StdEncoding.DecodeString(*tape.Cell[baseIdx+2].B)
if tape.Cell[baseIdx+2].B != nil {
dataTagB, errDataTag := base64.StdEncoding.DecodeString(*tape.Cell[baseIdx+2].B)

if errDataTag != nil {
log.Fatal("error:", errDataTag)
err = errDataTag
return
}
if len(dataTagB) > 0 {
topic := string(dataTagB)
b.Spend.Topic = &topic
if errDataTag != nil {
log.Fatal("error:", errDataTag)
err = errDataTag
return
}
if len(dataTagB) > 0 {
topic := string(dataTagB)
b.Spend.Topic = &topic
}
}

// Set nonce
dataNonce, errNonce := base64.StdEncoding.DecodeString(*tape.Cell[baseIdx+3].B)
if tape.Cell[baseIdx+3].B != nil {
// Set nonce
dataNonce, errNonce := base64.StdEncoding.DecodeString(*tape.Cell[baseIdx+3].B)

if errNonce != nil {
log.Fatal("error:", errNonce)
err = errNonce
return
}
if errNonce != nil {
log.Fatal("error:", errNonce)
err = errNonce
return
}

b.Spend.Nonce = binary.LittleEndian.Uint32(dataNonce)
b.Spend.Nonce = binary.LittleEndian.Uint32(dataNonce)
}

// Set additional data
b.Spend.AdditionalData = tape.Cell[baseIdx+4].S
Expand Down
28 changes: 28 additions & 0 deletions boost_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -301,3 +301,31 @@ func TestV2ContractRedeem(t *testing.T) {
t.Fatalf("expected version: %d got: %d\n", expected.Version, b.Redeem.Version)
}
}

func TestSampleBoostBreakerRaw(t *testing.T) {

// redeem - a512c846b0154d23325f40ef87a088d747252fc2a179cca067a6026ee59c5ea6
// expected values
expectedTxid := "d4cb7c93f874a9246c5545231b5892c7c88c99bb45540c81182838cd4a9d6346"

var expected = &BoostSpend{
Nonce: 3901135,
Version: 2,
}

// Get BOB data from raw tx string
bobData, err := bob.NewFromRawTxString(sampleBoostBreakerRaw)
if err != nil {
t.Fatalf("error occurred: %s", err.Error())
}

var b *Boost
b, err = NewFromTape(&bobData.Out[0].Tape[0])
if err != nil {
t.Fatalf("error occurred: %s\n", err.Error())
} else if expectedTxid != bobData.Tx.H {
t.Fatalf("expected txid: %s got: %s\n", expectedTxid, bobData.Tx.H)
} else if b.Spend.Category != expected.Category {
t.Fatalf("expected signature: %d got: %d\n", expected.Category, b.Spend.Category)
}
}
2 changes: 2 additions & 0 deletions tx_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -127,3 +127,5 @@ const contractV2SpendRaw = "01000000016e76d978836a9c99ad11840be3ee15f040c14b22d9
// extra_nonce_1 : 3783406472
// }
const contractV2RedeemRaw = "0100000001a2f02f8b812d56af2f30e439c60374689cbc9a5512bbe94809fb3cd314e3266a000000008747304402201f94a12ace389cd389ef129dc9b68eb1a357ff6f71a508aa0b3accd90736007702206d316fce43e5ae24a6b07acc342e0f7a5c0d0366a2a00dee00acbb25b8f4f6a9412103e0fd48907c0117600a6326aafe7d43adbc9421a4381bb6579f1ab4912cd25e3704cf863b00046b22f963084f22be6e277ead9004e18233880498948494ffffffff0194860100000000001976a914c96cbf841881982fa3eb2f996269ebed3f72d6a388ac00000000"

const sampleBoostBreakerRaw = "010000000120955f9a2d539d54114b615b9460f37dff979847892e00b3f7b174b180c91c09020000006b483045022100c7d1cc030e0675deebe658812603fdb4d15e917137b3ad89b7411a5578c7d81802201d4e047b888b6bb0503bd66484aa42b7d9f53abf21dc31e339311f87d3343caa4121020bd029230032c81edc403492d9828d174bb9a8d40f18bcd53d45c931f44f5979ffffffff032102000000000000ad08626f6f7374706f77750400000000205056187ecc862bc94e54ecc0ae010e3b5ea9dbfdfc4958517ecbb9f7216e20e104f09f0f1e0004d87e60be007e7c557a766b7e52796b557a8254887e557a8258887e7c7eaa7c6b7e7e7c8254887e6c7e7c8254887eaa01007e816c825488537f7681530121a5696b768100a0691d00000000000000000000000000000000000000000000000000000000007e6c539458959901007e819f6976a96c88ac00000000000000002f006a2231487948587459577947655072485669736e4e645339333156743643716f7555795a0972656c6179782e696f9a9d1c00000000001976a9149bcc623c910f75f203de79ea9f9286dc189faf0e88ac00000000"

0 comments on commit cc5f3ad

Please sign in to comment.