-
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.
Merge pull request #16 from catenocrypt/polkadot
Add Polkadot support
- Loading branch information
Showing
8 changed files
with
223 additions
and
21 deletions.
There are no files selected for viewing
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
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
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
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,24 @@ | ||
package coincodec | ||
|
||
import ( | ||
"github.com/wealdtech/go-slip44" | ||
) | ||
|
||
const ( | ||
PolkadotNetwork = 0 | ||
) | ||
|
||
func init() { | ||
toBytesMap[slip44.POLKADOT] = PolkadotDecodeToBytes | ||
toStringMap[slip44.POLKADOT] = PolkadotEncodeToString | ||
} | ||
|
||
// PolkadotDecodeToBytes converts the input string to a byte array | ||
func PolkadotDecodeToBytes(input string) ([]byte, error) { | ||
return SS58AddressDecodeToBytes(input, PolkadotNetwork) | ||
} | ||
|
||
// PolkadotEncodeToString converts the input byte array to a string representation of the Polkadot address. | ||
func PolkadotEncodeToString(data []byte) (string, error) { | ||
return SS58AddressEncodeToString(data, PolkadotNetwork) | ||
} |
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,107 @@ | ||
package coincodec | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/pkg/errors" | ||
"github.com/wealdtech/go-slip44" | ||
) | ||
|
||
func TestPolkadotEncodeToBytes(t *testing.T) { | ||
tests := []TestcaseEncode{ | ||
{ | ||
name: "Normal1", | ||
input: "15KRsCq9LLNmCxNFhGk55s5bEyazKefunDxUH24GFZwsTxyu", | ||
output: "beff0e5d6f6e6e6d573d3044f3e2bfb353400375dc281da3337468d4aa527908", | ||
}, | ||
{ | ||
name: "Normal2", | ||
input: "15AeCjMpcSt3Fwa47jJBd7JzQ395Kr2cuyF5Zp4UBf1g9ony", | ||
output: "b84b605a51045e43740bc74db62d0077ff9d971e49d588f9b307915721bb3251", | ||
}, | ||
{ | ||
name: "Normal3 from ensdomains/address-encoder", | ||
input: "1FRMM8PEiWXYax7rpS6X4XZX1aAAxSWx1CrKTyrVYhV24fg", | ||
output: "0aff6865635ae11013a83835c019d44ec3f865145943f487ae82a8e7bed3a66b", | ||
}, | ||
{ | ||
name: "Too short", | ||
input: "15KR", | ||
err: errors.New("Base58 string too short"), | ||
}, | ||
{ | ||
name: "Invalid characters", | ||
input: "1a+-/=", | ||
err: errors.New("Bad Base58 string"), | ||
}, | ||
{ | ||
name: "Bad checksum", | ||
input: "15KRsCq9LLNmCxNFhGk55s5bEyazKefunDxUH24GFZwsTwoF", | ||
err: errors.New("Invalid checksum"), | ||
}, | ||
{ | ||
name: "Substrate ed25519", | ||
input: "5FqqU2rytGPhcwQosKRtW1E3ha6BJKAjHgtcodh71dSyXhoZ", | ||
err: errors.New("Invalid network"), | ||
}, | ||
{ | ||
name: "Bitcoin", | ||
input: "1ES14c7qLb5CYhLMUekctxLgc1FV2Ti9DA", | ||
err: errors.New("Invalid length"), | ||
}, | ||
{ | ||
name: "Kusama ed25519", | ||
input: "FHKAe66mnbk8ke8zVWE9hFVFrJN1mprFPVmD5rrevotkcDZ", | ||
err: errors.New("Invalid network"), | ||
}, | ||
{ | ||
name: "Kusama secp256k1", | ||
input: "FxQFyTorsjVsjjMyjdgq8w5vGx8LiA1qhWbRYcFijxKKchx", | ||
err: errors.New("Invalid network"), | ||
}, | ||
{ | ||
name: "Kusama sr25519", | ||
input: "EJ5UJ12GShfh7EWrcNZFLiYU79oogdtXFUuDDZzk7Wb2vCe", | ||
err: errors.New("Invalid network"), | ||
}, | ||
} | ||
|
||
RunTestsEncode(t, slip44.POLKADOT, tests) | ||
} | ||
|
||
func TestPolkadotDecodeToString(t *testing.T) { | ||
tests := []TestcaseDecode{ | ||
{ | ||
name: "Good1", | ||
input: "beff0e5d6f6e6e6d573d3044f3e2bfb353400375dc281da3337468d4aa527908", | ||
output: "15KRsCq9LLNmCxNFhGk55s5bEyazKefunDxUH24GFZwsTxyu", | ||
}, | ||
{ | ||
name: "Good2", | ||
input: "b84b605a51045e43740bc74db62d0077ff9d971e49d588f9b307915721bb3251", | ||
output: "15AeCjMpcSt3Fwa47jJBd7JzQ395Kr2cuyF5Zp4UBf1g9ony", | ||
}, | ||
{ | ||
name: "Good3 from ensdomains/address-encoder", | ||
input: "0aff6865635ae11013a83835c019d44ec3f865145943f487ae82a8e7bed3a66b", | ||
output: "1FRMM8PEiWXYax7rpS6X4XZX1aAAxSWx1CrKTyrVYhV24fg", | ||
}, | ||
{ | ||
name: "Empty", | ||
input: "", | ||
err: errors.New("Invalid decoded address length"), | ||
}, | ||
{ | ||
name: "Too short", | ||
input: "06a1a1a7f2ff4762", | ||
err: errors.New("Invalid decoded address length"), | ||
}, | ||
{ | ||
name: "Too long", | ||
input: "06a1a1a7f2ff476205a51045e43740bc74db62d0077ff9d971e49d588f9b307915721bb3", | ||
err: errors.New("Invalid decoded address length"), | ||
}, | ||
} | ||
|
||
RunTestsDecode(t, slip44.POLKADOT, tests) | ||
} |
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,62 @@ | ||
package coincodec | ||
|
||
import ( | ||
"bytes" | ||
"errors" | ||
|
||
"golang.org/x/crypto/blake2b" | ||
) | ||
|
||
const ( | ||
ss58AddressLength = 32 | ||
ss58AddressChecksumSize = 2 | ||
ss58AddressTotalLength = ss58AddressLength + 1 + ss58AddressChecksumSize // incl. network byte and checksum | ||
) | ||
|
||
var ( | ||
ss58Prefix = []byte("SS58PRE") | ||
) | ||
|
||
func computeChecksum(data []byte) []byte { | ||
prefixed := append(ss58Prefix, data...) | ||
hashres := blake2b.Sum512(prefixed) | ||
checksum := hashres[:ss58AddressChecksumSize] | ||
return checksum | ||
} | ||
|
||
// SS58AddressDecodeToBytes converts the input string to a byte array | ||
func SS58AddressDecodeToBytes(input string, network byte) ([]byte, error) { | ||
decoded, err := Base58Decode(input, Base58DefaultAlphabet) | ||
if err != nil { | ||
return nil, err | ||
} | ||
if len(decoded) != ss58AddressTotalLength { | ||
return nil, errors.New("Invalid length") | ||
} | ||
// check network | ||
networkActual := decoded[0] | ||
if networkActual != network { | ||
return nil, errors.New("Invalid network") | ||
} | ||
checksum := computeChecksum(decoded[:len(decoded)-ss58AddressChecksumSize]) | ||
// compare checksum | ||
if !bytes.Equal(checksum, decoded[len(decoded)-ss58AddressChecksumSize:]) { | ||
return nil, errors.New("Invalid checksum") | ||
} | ||
// strip network, checksum | ||
return decoded[1 : len(decoded)-2], nil | ||
} | ||
|
||
// SS58AddressEncodeToString converts the input byte array to a string representation of the address. | ||
func SS58AddressEncodeToString(data []byte, network byte) (string, error) { | ||
if len(data) != ss58AddressLength { | ||
return "", errors.New("Invalid decoded address length") | ||
} | ||
networked := []byte{} | ||
networked = append(networked, network) | ||
networked = append(networked, data...) | ||
checksum := computeChecksum(networked) | ||
checksummed := append(networked, checksum...) | ||
encoded := Base58Encode(checksummed, Base58DefaultAlphabet) | ||
return encoded, nil | ||
} |
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
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