-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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
- Loading branch information
Showing
13 changed files
with
1,024 additions
and
691 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
Large diffs are not rendered by default.
Oops, something went wrong.
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
package elementsutil | ||
|
||
import ( | ||
"bytes" | ||
"encoding/binary" | ||
"errors" | ||
|
||
"github.com/vulpemventures/go-elements/internal/bufferutil" | ||
) | ||
|
||
// SatoshiToElementsValue method converts Satoshi value to Elements value | ||
func SatoshiToElementsValue(val uint64) ([]byte, error) { | ||
unconfPrefix := byte(1) | ||
b := bytes.NewBuffer([]byte{}) | ||
if err := bufferutil.BinarySerializer.PutUint64(b, binary.LittleEndian, val); err != nil { | ||
return nil, err | ||
} | ||
res := append([]byte{unconfPrefix}, bufferutil.ReverseBytes(b.Bytes())...) | ||
return res, nil | ||
} | ||
|
||
// ElementsToSatoshiValue method converts Elements value to Satoshi value | ||
func ElementsToSatoshiValue(val []byte) (uint64, error) { | ||
if len(val) != 9 { | ||
return 0, errors.New("invalid elements value lenght") | ||
} | ||
if val[0] != byte(1) { | ||
return 0, errors.New("invalid prefix") | ||
} | ||
reverseValueBuffer := bufferutil.ReverseBytes(val[1:]) | ||
d := bufferutil.NewDeserializer(bytes.NewBuffer(reverseValueBuffer)) | ||
return d.ReadUint64() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
package elementsutil | ||
|
||
import ( | ||
"crypto/rand" | ||
"math/big" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestSatoshiToElementsValueRoundTrip(t *testing.T) { | ||
bigInt, err := rand.Int(rand.Reader, big.NewInt(1000000000)) | ||
if err != nil { | ||
panic(err) | ||
} | ||
satoshi := bigInt.Uint64() | ||
elementsValue, err := SatoshiToElementsValue(satoshi) | ||
if !assert.NoError(t, err) { | ||
t.FailNow() | ||
} | ||
|
||
satoshiValue, err := ElementsToSatoshiValue(elementsValue) | ||
if !assert.NoError(t, err) { | ||
t.FailNow() | ||
} | ||
|
||
assert.Equal(t, satoshi, satoshiValue) | ||
} |
Oops, something went wrong.