Skip to content

Commit

Permalink
Add Elrond implementation (#22)
Browse files Browse the repository at this point in the history
* Add Elrond implementation.

* Update copyright notices

Co-authored-by: Catenocrypt <[email protected]>
  • Loading branch information
optout21 and Catenocrypt authored Dec 11, 2020
1 parent 2c98687 commit c47c61d
Show file tree
Hide file tree
Showing 11 changed files with 133 additions and 66 deletions.
6 changes: 3 additions & 3 deletions bech32_address.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,13 @@ import (
)

const (
keyhashLength = 20
Bech32DefaultKeyhashLength = 20
)

var errorBech32KeyHash = errors.New("A Bech32 address key hash must be 20 bytes")

// Bech32AddressDecodeToBytes converts the input string to a byte array
func Bech32AddressDecodeToBytes(hrp string, input string) ([]byte, error) {
func Bech32AddressDecodeToBytes(hrp string, input string, keyhashLength int) ([]byte, error) {
var empty []byte
bytes, err := Bech32DecodeToBytes(input, hrp)
if err != nil {
Expand All @@ -24,7 +24,7 @@ func Bech32AddressDecodeToBytes(hrp string, input string) ([]byte, error) {
}

// Bech32AddressEncodeToString converts the input byte array to a string representation of the bech32 address.
func Bech32AddressEncodeToString(hrp string, bytes []byte) (string, error) {
func Bech32AddressEncodeToString(hrp string, bytes []byte, keyhashLength int) (string, error) {
if len(bytes) != keyhashLength {
return "", errorBech32KeyHash
}
Expand Down
4 changes: 2 additions & 2 deletions bnb.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,12 @@ func init() {

// BNBDecodeToBytes converts the input string to a byte array
func BNBDecodeToBytes(input string) ([]byte, error) {
bytes, err := Bech32AddressDecodeToBytes(hrpBNB, input)
bytes, err := Bech32AddressDecodeToBytes(hrpBNB, input, Bech32DefaultKeyhashLength)
return bytes, err
}

// BNBEncodeToString converts the input byte array to a string representation of the BNB address.
func BNBEncodeToString(bytes []byte) (string, error) {
output, err := Bech32AddressEncodeToString(hrpBNB, bytes)
output, err := Bech32AddressEncodeToString(hrpBNB, bytes, Bech32DefaultKeyhashLength)
return output, err
}
13 changes: 0 additions & 13 deletions codec.go
Original file line number Diff line number Diff line change
@@ -1,16 +1,3 @@
// Copyright © 2019 Weald Technology Trading
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package coincodec

import (
Expand Down
13 changes: 0 additions & 13 deletions codec_test.go
Original file line number Diff line number Diff line change
@@ -1,16 +1,3 @@
// Copyright © 2019 Weald Technology Trading
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package coincodec

import (
Expand Down
4 changes: 2 additions & 2 deletions cosmos.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,12 @@ func init() {

// CosmosDecodeToBytes converts the input string to a byte array
func CosmosDecodeToBytes(input string) ([]byte, error) {
bytes, err := Bech32AddressDecodeToBytes(hrp, input)
bytes, err := Bech32AddressDecodeToBytes(hrp, input, Bech32DefaultKeyhashLength)
return bytes, err
}

// CosmosEncodeToString converts the input byte array to a string representation of the Cosmos address.
func CosmosEncodeToString(bytes []byte) (string, error) {
output, err := Bech32AddressEncodeToString(hrp, bytes)
output, err := Bech32AddressEncodeToString(hrp, bytes, Bech32DefaultKeyhashLength)
return output, err
}
24 changes: 24 additions & 0 deletions elrond.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package coincodec

const (
hrpElrond = "erd"
ElrondKeyhashLength = 32
)

func init() {
// 508 slip44.ELROND
toBytesMap[508] = ElrondDecodeToBytes
toStringMap[508] = ElrondEncodeToString
}

// ElrondDecodeToBytes converts the input string to a byte array
func ElrondDecodeToBytes(input string) ([]byte, error) {
bytes, err := Bech32AddressDecodeToBytes(hrpElrond, input, ElrondKeyhashLength)
return bytes, err
}

// ElrondEncodeToString converts the input byte array to a string representation of the Elrond address.
func ElrondEncodeToString(bytes []byte) (string, error) {
output, err := Bech32AddressEncodeToString(hrpElrond, bytes, ElrondKeyhashLength)
return output, err
}
95 changes: 95 additions & 0 deletions elrond_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
package coincodec

import (
"testing"

"github.com/pkg/errors"
)

func TestElrondEncodeToBytes(t *testing.T) {
tests := []TestcaseEncode{
{
name: "Alice",
input: "erd1l453hd0gt5gzdp7czpuall8ggt2dcv5zwmfdf3sd3lguxseux2fsmsgldz",
output: "fd691bb5e85d102687d81079dffce842d4dc328276d2d4c60d8fd1c3433c3293",
},
{
name: "Bob",
input: "erd1cux02zersde0l7hhklzhywcxk4u9n4py5tdxyx7vrvhnza2r4gmq4vw35r",
output: "c70cf50b238372fffaf7b7c5723b06b57859d424a2da621bcc1b2f317543aa36",
},
{
name: "Carol",
input: "erd19nu5t7hszckwah5nlcadmk5rlchtugzplznskffpwecygcu0520s9tnyy0",
output: "2cf945faf0162ceede93fe3addda83fe2ebe2041f8a70b2521767044638fa29f",
},
// invalid
{
name: "Empty",
input: "",
err: errors.New("decoding bech32 failed"),
},
{
name: "Invalid",
input: "foo",
err: errors.New("decoding bech32 failed"),
},
{
name: "Invalid 2",
input: "10z9xdugayn528ksaesdwlhf006fw5sg2qmmm0h52fvxczwgesyvq5pwemr",
err: errors.New("decoding bech32 failed"),
},
{
name: "Invalid 3",
input: "xerd10z9xdugayn528ksaesdwlhf006fw5sg2qmmm0h52fvxczwgesyvq5pwemr",
err: errors.New("decoding bech32 failed"),
},
{
name: "Invalid 4",
input: "foo10z9xdugayn528ksaesdwlhf006fw5sg2qmmm0h52fvxczwgesyvq5pwemr",
err: errors.New("decoding bech32 failed"),
},
{
name: "Invalid Hex",
input: "fd691bb5e85d102687d81079dffce842d4dc328276d2d4c60d8fd1c3433c3293",
err: errors.New("decoding bech32 failed"),
},
}

// 508 slip44.ELROND
RunTestsEncode(t, 508, tests)
}

func TestElrondDecodeToString(t *testing.T) {
tests := []TestcaseDecode{
{
name: "Alice",
input: "fd691bb5e85d102687d81079dffce842d4dc328276d2d4c60d8fd1c3433c3293",
output: "erd1l453hd0gt5gzdp7czpuall8ggt2dcv5zwmfdf3sd3lguxseux2fsmsgldz",
},
{
name: "Bob",
input: "c70cf50b238372fffaf7b7c5723b06b57859d424a2da621bcc1b2f317543aa36",
output: "erd1cux02zersde0l7hhklzhywcxk4u9n4py5tdxyx7vrvhnza2r4gmq4vw35r",
},
{
name: "Carol",
input: "2cf945faf0162ceede93fe3addda83fe2ebe2041f8a70b2521767044638fa29f",
output: "erd19nu5t7hszckwah5nlcadmk5rlchtugzplznskffpwecygcu0520s9tnyy0",
},
// invalid
{
name: "Empty",
input: "",
err: errors.New("A Bech32 address key hash must be 20 bytes"),
},
{
name: "Too short",
input: "fd691bb5e85d102687d81079dffce842d4dc328276d2d4c60d8fd1c3433c32",
err: errors.New("A Bech32 address key hash must be 20 bytes"),
},
}

// 508 slip44.ELROND
RunTestsDecode(t, 508, tests)
}
13 changes: 0 additions & 13 deletions ethereum.go
Original file line number Diff line number Diff line change
@@ -1,16 +1,3 @@
// Copyright © 2019 Weald Technology Trading
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package coincodec

import (
Expand Down
19 changes: 3 additions & 16 deletions ethereum_test.go
Original file line number Diff line number Diff line change
@@ -1,16 +1,3 @@
// Copyright © 2019 Weald Technology Trading
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package coincodec

import (
Expand All @@ -21,7 +8,7 @@ import (
)

func TestEtherToBytes(t *testing.T) {
tests := []TestcaseEncode {
tests := []TestcaseEncode{
{
name: "Empty",
input: "",
Expand Down Expand Up @@ -68,7 +55,7 @@ func TestEtherToBytes(t *testing.T) {
}

func TestEtherToString(t *testing.T) {
tests := []TestcaseDecode {
tests := []TestcaseDecode{
{
name: "Empty",
input: "",
Expand All @@ -81,7 +68,7 @@ func TestEtherToString(t *testing.T) {
},
{
name: "Good",
input: "0102030405060708090a0b0c0d0e0f1011121314",
input: "0102030405060708090a0b0c0d0e0f1011121314",
output: "0x0102030405060708090a0B0c0d0e0f1011121314",
},
}
Expand Down
4 changes: 2 additions & 2 deletions iotex.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,12 @@ func init() {

// IoTexDecodeToBytes converts the input string to a byte array
func IoTexDecodeToBytes(input string) ([]byte, error) {
bytes, err := Bech32AddressDecodeToBytes(hrpIOTEX, input)
bytes, err := Bech32AddressDecodeToBytes(hrpIOTEX, input, Bech32DefaultKeyhashLength)
return bytes, err
}

// IoTexEncodeToString converts the input byte array to a string representation of the Cosmos address.
func IoTexEncodeToString(bytes []byte) (string, error) {
output, err := Bech32AddressEncodeToString(hrpIOTEX, bytes)
output, err := Bech32AddressEncodeToString(hrpIOTEX, bytes, Bech32DefaultKeyhashLength)
return output, err
}
4 changes: 2 additions & 2 deletions zilliqa.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,12 @@ func init() {

// ZilliqaDecodeToBytes converts the input string to a byte array
func ZilliqaDecodeToBytes(input string) ([]byte, error) {
bytes, err := Bech32AddressDecodeToBytes(hrpZIL, input)
bytes, err := Bech32AddressDecodeToBytes(hrpZIL, input, Bech32DefaultKeyhashLength)
return bytes, err
}

// ZilliqaEncodeToString converts the input byte array to a string representation of the Zilliqa address.
func ZilliqaEncodeToString(bytes []byte) (string, error) {
output, err := Bech32AddressEncodeToString(hrpZIL, bytes)
output, err := Bech32AddressEncodeToString(hrpZIL, bytes, Bech32DefaultKeyhashLength)
return output, err
}

0 comments on commit c47c61d

Please sign in to comment.