Skip to content

Commit c9c23e4

Browse files
authored
Verify proof method + Refactor confidential pkg (#143)
* Move functions for converting amounts from confidential to new pkg * Refactor of confidential pkg * rename unblinding method * add unblinding method for issuance * add method to verify surjection proof * use internal functions to increase readability * add tests for new methods * Add mthod to verify blinding and use it in e2e tests
1 parent 31092ee commit c9c23e4

13 files changed

Lines changed: 1024 additions & 691 deletions

File tree

confidential/confidential.go

Lines changed: 391 additions & 195 deletions
Large diffs are not rendered by default.

confidential/confidential_test.go

Lines changed: 158 additions & 221 deletions
Large diffs are not rendered by default.

confidential/data/confidential.json

Lines changed: 60 additions & 0 deletions
Large diffs are not rendered by default.

internal/elementsutil/amount.go

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package elementsutil
2+
3+
import (
4+
"bytes"
5+
"encoding/binary"
6+
"errors"
7+
8+
"github.com/vulpemventures/go-elements/internal/bufferutil"
9+
)
10+
11+
// SatoshiToElementsValue method converts Satoshi value to Elements value
12+
func SatoshiToElementsValue(val uint64) ([]byte, error) {
13+
unconfPrefix := byte(1)
14+
b := bytes.NewBuffer([]byte{})
15+
if err := bufferutil.BinarySerializer.PutUint64(b, binary.LittleEndian, val); err != nil {
16+
return nil, err
17+
}
18+
res := append([]byte{unconfPrefix}, bufferutil.ReverseBytes(b.Bytes())...)
19+
return res, nil
20+
}
21+
22+
// ElementsToSatoshiValue method converts Elements value to Satoshi value
23+
func ElementsToSatoshiValue(val []byte) (uint64, error) {
24+
if len(val) != 9 {
25+
return 0, errors.New("invalid elements value lenght")
26+
}
27+
if val[0] != byte(1) {
28+
return 0, errors.New("invalid prefix")
29+
}
30+
reverseValueBuffer := bufferutil.ReverseBytes(val[1:])
31+
d := bufferutil.NewDeserializer(bytes.NewBuffer(reverseValueBuffer))
32+
return d.ReadUint64()
33+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package elementsutil
2+
3+
import (
4+
"crypto/rand"
5+
"math/big"
6+
"testing"
7+
8+
"github.com/stretchr/testify/assert"
9+
)
10+
11+
func TestSatoshiToElementsValueRoundTrip(t *testing.T) {
12+
bigInt, err := rand.Int(rand.Reader, big.NewInt(1000000000))
13+
if err != nil {
14+
panic(err)
15+
}
16+
satoshi := bigInt.Uint64()
17+
elementsValue, err := SatoshiToElementsValue(satoshi)
18+
if !assert.NoError(t, err) {
19+
t.FailNow()
20+
}
21+
22+
satoshiValue, err := ElementsToSatoshiValue(elementsValue)
23+
if !assert.NoError(t, err) {
24+
t.FailNow()
25+
}
26+
27+
assert.Equal(t, satoshi, satoshiValue)
28+
}

0 commit comments

Comments
 (0)