Skip to content

Commit

Permalink
feat(BUX-296): refactor BUMP structure
Browse files Browse the repository at this point in the history
  • Loading branch information
pawellewandowski98 committed Oct 26, 2023
1 parent 864ffb3 commit 0267926
Show file tree
Hide file tree
Showing 3 changed files with 128 additions and 150 deletions.
88 changes: 73 additions & 15 deletions bump.go
Original file line number Diff line number Diff line change
@@ -1,33 +1,91 @@
package paymail

// BUMPTx is a struct that represents a single BUMP transaction
type BUMPTx struct {
hash string
txId bool
duplicate bool
}

// BUMPMap is a map of BUMPTx where offset is the key
type BUMPMap []map[uint64]BUMPTx
import (
"errors"
"github.com/libsv/go-bc"
)

// BUMPSlice is a slice of BUMPMap which contain transactions required to calculate merkle roots
type BUMPSlice []BUMPMap
// BUMPPaths represents BUMP format for all inputs
type BUMPPaths []BUMP

// BUMP is a struct that represents a whole BUMP format
type BUMP struct {
blockHeight uint64
path BUMPSlice
path []BUMPPathMap
}

func (b BUMPMap) calculateMerkleRoots() ([]string, error) {
// BUMPPathMap is a map of BUMPTx where offset is the key
type BUMPPathMap map[uint64]BUMPPathElement

// BUMPPathElement is a struct that represents a single BUMP transaction
type BUMPPathElement struct {
hash string
txId bool
duplicate bool
}

func (b BUMP) calculateMerkleRoots() ([]string, error) {
merkleRoots := make([]string, 0)

for offset, bumpTx := range b[0] {
merkleRoot, err := calculateMerkleRoot(bumpTx, offset, b)
for offset, pathElement := range b.path[0] {
merkleRoot, err := calculateMerkleRoot(pathElement, offset, b.path)
if err != nil {
return nil, err
}
merkleRoots = append(merkleRoots, merkleRoot)
}
//}
return merkleRoots, nil
}

func calculateMerkleRoot(baseElement BUMPPathElement, offset uint64, bumpPathMaps []BUMPPathMap) (string, error) {
calculatedHash := baseElement.hash

for _, bPathMap := range bumpPathMaps {
newOffset := offset - 1
if offset%2 == 0 {
newOffset = offset + 1
}
pairElement := bPathMap[newOffset]
if &pairElement == nil {
return "", errors.New("could not find pair")
}

leftNode, rightNode := prepareNodes(baseElement, offset, pairElement, newOffset)

str, err := bc.MerkleTreeParentStr(leftNode, rightNode)
if err != nil {
return "", err
}
calculatedHash = str

offset = offset / 2

baseElement = BUMPPathElement{
hash: calculatedHash,
}
}

return calculatedHash, nil
}

func prepareNodes(baseTx BUMPPathElement, offset uint64, pairElement BUMPPathElement, newOffset uint64) (string, string) {
var txHash, tx2Hash string

if baseTx.duplicate {
txHash = pairElement.hash
} else {
txHash = baseTx.hash
}

if pairElement.duplicate {
tx2Hash = baseTx.hash
} else {
tx2Hash = pairElement.hash
}

if newOffset > offset {
return txHash, tx2Hash
}
return tx2Hash, txHash
}
15 changes: 12 additions & 3 deletions examples/client/bump/bump.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,21 @@
package main

import "github.com/bitcoin-sv/go-paymail"
import (
"fmt"
"github.com/bitcoin-sv/go-paymail"
)

func main() {
hex := "fe8a6a0c000c04fde80b0011774f01d26412f0d16ea3f0447be0b5ebec67b0782e321a7a01cbdf7f734e30fde90b02004e53753e3fe4667073063a17987292cfdea278824e9888e52180581d7188d8fdea0b025e441996fc53f0191d649e68a200e752fb5f39e0d5617083408fa179ddc5c998fdeb0b0102fdf405000671394f72237d08a4277f4435e5b6edf7adc272f25effef27cdfe805ce71a81fdf50500262bccabec6c4af3ed00cc7a7414edea9c5efa92fb8623dd6160a001450a528201fdfb020101fd7c010093b3efca9b77ddec914f8effac691ecb54e2c81d0ab81cbc4c4b93befe418e8501bf01015e005881826eb6973c54003a02118fe270f03d46d02681c8bc71cd44c613e86302f8012e00e07a2bb8bb75e5accff266022e1e5e6e7b4d6d943a04faadcf2ab4a22f796ff30116008120cafa17309c0bb0e0ffce835286b3a2dcae48e4497ae2d2b7ced4f051507d010a00502e59ac92f46543c23006bff855d96f5e648043f0fb87a7a5949e6a9bebae430104001ccd9f8f64f4d0489b30cc815351cf425e0e78ad79a589350e4341ac165dbe45010301010000af8764ce7e1cc132ab5ed2229a005c87201c9a5ee15c0f91dd53eff31ab30cd4"
_, err := paymail.DecodeBUMP(hex)
beefHex := "0100beef01fe636d0c0007021400fe507c0c7aa754cef1f7889d5fd395cf1f785dd7de98eed895dbedfe4e5bc70d1502ac4e164f5bc16746bb0868404292ac8318bbac3800e4aad13a014da427adce3e010b00bc4ff395efd11719b277694cface5aa50d085a0bb81f613f70313acd28cf4557010400574b2d9142b8d28b61d88e3b2c3f44d858411356b49a28a4643b6d1a6a092a5201030051a05fc84d531b5d250c23f4f886f6812f9fe3f402d61607f977b4ecd2701c19010000fd781529d58fc2523cf396a7f25440b409857e7e221766c57214b1d38c7b481f01010062f542f45ea3660f86c013ced80534cb5fd4c19d66c56e7e8c5d4bf2d40acc5e010100b121e91836fd7cd5102b654e9f72f3cf6fdbfd0b161c53a9c54b12c841126331020100000001cd4e4cac3c7b56920d1e7655e7e260d31f29d9a388d04910f1bbd72304a79029010000006b483045022100e75279a205a547c445719420aa3138bf14743e3f42618e5f86a19bde14bb95f7022064777d34776b05d816daf1699493fcdf2ef5a5ab1ad710d9c97bfb5b8f7cef3641210263e2dee22b1ddc5e11f6fab8bcd2378bdd19580d640501ea956ec0e786f93e76ffffffff013e660000000000001976a9146bfd5c7fbe21529d45803dbcf0c87dd3c71efbc288ac0000000001000100000001ac4e164f5bc16746bb0868404292ac8318bbac3800e4aad13a014da427adce3e000000006a47304402203a61a2e931612b4bda08d541cfb980885173b8dcf64a3471238ae7abcd368d6402204cbf24f04b9aa2256d8901f0ed97866603d2be8324c2bfb7a37bf8fc90edd5b441210263e2dee22b1ddc5e11f6fab8bcd2378bdd19580d640501ea956ec0e786f93e76ffffffff013c660000000000001976a9146bfd5c7fbe21529d45803dbcf0c87dd3c71efbc288ac0000000000"
beef, err := paymail.DecodeBEEF(beefHex)
if err != nil {
panic(err)
}
fmt.Println(beef)

roots, err := beef.GetMerkleRoots()
if err != nil {
panic(err)
}
fmt.Println(roots)
}
Loading

0 comments on commit 0267926

Please sign in to comment.