ADR-28 derive address functions (#9088)
* update Derive functions * update adr-028 * changelog update * Apply suggestions from code review Co-authored-by: Marie Gauthier <marie.gauthier63@gmail.com> * review updates * remove DeriveMulti and rollback some changes in CHANGELOG * add noop error check Co-authored-by: Marie Gauthier <marie.gauthier63@gmail.com>
This commit is contained in:
co-authored by
Marie Gauthier
parent
261c7ebd89
commit
e43edc4749
+11
-5
@@ -20,20 +20,21 @@ type Addressable interface {
|
||||
// Hash creates a new address from address type and key
|
||||
func Hash(typ string, key []byte) []byte {
|
||||
hasher := sha256.New()
|
||||
hasher.Write(conv.UnsafeStrToBytes(typ))
|
||||
_, err := hasher.Write(conv.UnsafeStrToBytes(typ))
|
||||
// the error always nil, it's here only to satisfy the io.Writer interface
|
||||
errors.AssertNil(err)
|
||||
th := hasher.Sum(nil)
|
||||
|
||||
hasher.Reset()
|
||||
_, err := hasher.Write(th)
|
||||
// the error always nil, it's here only to satisfy the io.Writer interface
|
||||
_, err = hasher.Write(th)
|
||||
errors.AssertNil(err)
|
||||
_, err = hasher.Write(key)
|
||||
errors.AssertNil(err)
|
||||
return hasher.Sum(nil)
|
||||
}
|
||||
|
||||
// NewComposed creates a new address based on sub addresses.
|
||||
func NewComposed(typ string, subAddresses []Addressable) ([]byte, error) {
|
||||
// Compose creates a new address based on sub addresses.
|
||||
func Compose(typ string, subAddresses []Addressable) ([]byte, error) {
|
||||
as := make([][]byte, len(subAddresses))
|
||||
totalLen := 0
|
||||
var err error
|
||||
@@ -62,3 +63,8 @@ func Module(moduleName string, key []byte) []byte {
|
||||
mKey := append([]byte(moduleName), 0)
|
||||
return Hash("module", append(mKey, key...))
|
||||
}
|
||||
|
||||
// Derive derives a new address from the main `address` and a derivation `key`.
|
||||
func Derive(address []byte, key []byte) []byte {
|
||||
return Hash(conv.UnsafeBytesToStr(address), key)
|
||||
}
|
||||
|
||||
@@ -34,7 +34,7 @@ func (suite *AddressSuite) TestComposed() {
|
||||
a2 := addrMock{[]byte{21, 22}}
|
||||
|
||||
typ := "multisig"
|
||||
ac, err := NewComposed(typ, []Addressable{a1, a2})
|
||||
ac, err := Compose(typ, []Addressable{a1, a2})
|
||||
assert.NoError(err)
|
||||
assert.Len(ac, Len)
|
||||
|
||||
@@ -45,18 +45,18 @@ func (suite *AddressSuite) TestComposed() {
|
||||
assert.Equal(ac, ac2, "NewComposed works correctly")
|
||||
|
||||
// changing order of addresses shouldn't impact a composed address
|
||||
ac2, err = NewComposed(typ, []Addressable{a2, a1})
|
||||
ac2, err = Compose(typ, []Addressable{a2, a1})
|
||||
assert.NoError(err)
|
||||
assert.Len(ac2, Len)
|
||||
assert.Equal(ac, ac2, "NewComposed is not sensitive for order")
|
||||
|
||||
// changing a type should change composed address
|
||||
ac2, err = NewComposed(typ+"other", []Addressable{a2, a1})
|
||||
ac2, err = Compose(typ+"other", []Addressable{a2, a1})
|
||||
assert.NoError(err)
|
||||
assert.NotEqual(ac, ac2, "NewComposed must be sensitive to type")
|
||||
|
||||
// changing order of addresses shouldn't impact a composed address
|
||||
ac2, err = NewComposed(typ, []Addressable{a1, addrMock{make([]byte, 300, 300)}})
|
||||
ac2, err = Compose(typ, []Addressable{a1, addrMock{make([]byte, 300, 300)}})
|
||||
assert.Error(err)
|
||||
assert.Contains(err.Error(), "should be max 255 bytes, got 300")
|
||||
}
|
||||
@@ -75,6 +75,21 @@ func (suite *AddressSuite) TestModule() {
|
||||
assert.NotEqual(addr2, addr3, "changing key must change address")
|
||||
}
|
||||
|
||||
func (suite *AddressSuite) TestDerive() {
|
||||
assert := suite.Assert()
|
||||
var addr, key1, key2 = []byte{1, 2}, []byte{3, 4}, []byte{1, 2}
|
||||
d1 := Derive(addr, key1)
|
||||
d2 := Derive(addr, key2)
|
||||
d3 := Derive(key1, key2)
|
||||
assert.Len(d1, Len)
|
||||
assert.Len(d2, Len)
|
||||
assert.Len(d3, Len)
|
||||
|
||||
assert.NotEqual(d1, d2)
|
||||
assert.NotEqual(d1, d3)
|
||||
assert.NotEqual(d2, d3)
|
||||
}
|
||||
|
||||
type addrMock struct {
|
||||
Addr []byte
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user