identity

package
v0.0.0-...-55eee13 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Oct 25, 2018 License: GPL-3.0 Imports: 8 Imported by: 2

README

Identity

The Identity library is an official reference implementation of identities and addresses in the Republic Protocol. It supports the generation and use of ECDSA SECP256K private keys, Republic Protocol IDs, Republic Protocol addresses, and multi-addresses.

Key Pairs

An identity in Republic Protocol is defined by an ECDSA SECP256K private key, the same kind of private key used by both Bitcoin and Ethereum.

keyPair, err := identity.NewKeyPair()

Republic Protocol IDs

Republic Protocol IDs are generated from your private key, and are used to identify traders and nodes in Republic Protocol. To generate a Republic Protocol ID, take the last 20 bytes of the Keccak256 hash of your ECDSA public key.

keyPair, err := identity.NewKeyPair()
id := keyPair.ID()
fmt.Println(id)

An example output of this code snippet is given below.

[247 107 192 130 208 33 69 182 183 127 237 24 22 83 157 99 207 221 9 243]

Republic Protocol addresses

Republic addresses are URL compatible encoding of an ID. To generate an address, take the Base58 encoding of the multi-hash of an ID. The Bitcoin alphabet for Base58 encoding must be used. For more information about multi-hashes, see https://multiformats.io/multihash.

keyPair, err := identity.NewKeyPair()
address := keyPair.Address()
fmt.Println(address)

An example output of this code snippet is given below.

8MGHyHWxQ5zUzXXnK5b6LkWGFCWSyX

Multi-address

Republic multi-addresses are a form of network address that can represent multiple different networking layers in a single address. In the Republic Protocol, they are used to hold network addresses and Republic addresses. At the moment, the network address is excepted to be an IPv4 address and a TCP port. In future, it may change to be an I2P address that provides a greater level of anonymity. For more information about multi-addresses, see https://multiformats.io/multiaddr.

keyPair, err := identity.NewKeyPair()
multi, err := keyPair.MultiAddress()
multi, err = identity.NewMultiAddressFromString(fmt.Sprintf("/ip4/127.0.0.1/tcp/80/republic/%s", multi.String())
fmt.Println(multi.String())
fmt.Println(multi.ValueForProtocol(identity.RepublicCode))
fmt.Println(multi.ValueForProtocol(identity.IP4Code))
fmt.Println(multi.ValueForProtocol(identity.TCPCode))

An example output of this code snippet is given below.

/ip4/127.0.0.1/tcp/80/republic/8MGfbzAMS59Gb4cSjpm34soGNYsM2f
8MGfbzAMS59Gb4cSjpm34soGNYsM2f <nil>
127.0.0.1 <nil>
80 <nil>

Tests

To run the test suite, install Ginkgo.

go get github.com/onsi/ginkgo/ginkgo

Now we can run the tests.

ginkgo -v

License

The Identity library was developed by the Republic Protocol team and is available under the MIT license. For more information, see our website https://republicprotocol.com.

c0ee5d01eece4eab67cd4e16179098407a1f32ae

Documentation

Index

Constants

View Source
const (
	IP4Code      = 0x0004
	IP6Code      = 0x0029
	TCPCode      = 0x0006
	RepublicCode = 0x0065
)

Codes for extracting specific protocol values from a MultiAddress.

View Source
const AddressLength = 30

AddressLength is the number of bytes in an Address.

View Source
const IDLength = 20

IDLength is the number of bytes in an ID.

Variables

View Source
var (
	ErrFailToDecode       = fmt.Errorf("fail to decode the string")
	ErrWrongAddressLength = fmt.Errorf("wrong address length")
)

Errors returned by the package.

Functions

func Closer

func Closer(left, right, target Address) (bool, error)

Closer returns true if the left Address is closer to the target than the right Address, otherwise it returns false. In the case that both Addresses are equal distances from the target, it returns true.

func ProtocolWithCode

func ProtocolWithCode(c int) multiaddr.Protocol

ProtocolWithCode returns the Protocol description with the given code.

func ProtocolWithName

func ProtocolWithName(s string) multiaddr.Protocol

ProtocolWithName returns the Protocol description with the given name.

Types

type Address

type Address string

An Address string is generated from an ID.

func (Address) Distance

func (address Address) Distance(other Address) ([]byte, error)

Distance uses a bitwise XOR to calculate distance between two Addresses.

func (Address) Hash

func (address Address) Hash() []byte

Hash implements the crypto.Hasher interface for signing.

func (Address) ID

func (address Address) ID() ID

ID returns the ID of the Address

func (Address) MultiAddress

func (address Address) MultiAddress() (MultiAddress, error)

MultiAddress returns the Republic multi-address of the Address. It can be appended with other MultiAddresses.

func (Address) SamePrefixLength

func (address Address) SamePrefixLength(other Address) (int, error)

SamePrefixLength returns the number of prefix bits that match with those of another Address, excluding the first 2 bytes.

func (Address) String

func (address Address) String() string

String returns the Address as a string.

type Addresses

type Addresses []Address

Addresses is an alias.

type ID

type ID []byte

An ID is a slice of 20 bytes that can be converted into an Address. It must always be exact 20 bytes.

func (ID) Address

func (id ID) Address() Address

Address returns the Address of the ID

func (ID) String

func (id ID) String() string

String returns the ID as a string.

type MultiAddress

type MultiAddress struct {
	Signature []byte
	Nonce     uint64
	// contains filtered or unexported fields
}

MultiAddress is an alias.

func NewMultiAddressFromString

func NewMultiAddressFromString(s string) (MultiAddress, error)

NewMultiAddressFromString parses and validates an input string. It returns a MultiAddress, or an error.

func (MultiAddress) Address

func (multiAddress MultiAddress) Address() Address

Address returns the Republic address of a MultiAddress.

func (MultiAddress) Hash

func (multiAddress MultiAddress) Hash() []byte

Hash returns the Keccak256 hash of a multiAddress. This hash is used to create signatures for a multiaddress.

func (MultiAddress) ID

func (multiAddress MultiAddress) ID() ID

ID returns the Republic ID of a MultiAddress.

func (*MultiAddress) IsNil

func (multiAddress *MultiAddress) IsNil() bool

IsNil checks if the multiAddress is nil.

func (MultiAddress) MarshalJSON

func (multiAddress MultiAddress) MarshalJSON() ([]byte, error)

MarshalJSON implements the json.Marshaler interface.

func (MultiAddress) String

func (multiAddress MultiAddress) String() string

String returns the MultiAddress as a plain string.

func (*MultiAddress) UnmarshalJSON

func (multiAddress *MultiAddress) UnmarshalJSON(data []byte) error

UnmarshalJSON implements the json.Unmarshaler interface.

func (*MultiAddress) UnmarshalStringJSON

func (multiAddress *MultiAddress) UnmarshalStringJSON(data []byte) error

UnmarshalStringJSON will unmarshal multi-addresses that are in string format to a standard multi-address struct.

func (MultiAddress) ValueForProtocol

func (multiAddress MultiAddress) ValueForProtocol(code int) (string, error)

ValueForProtocol returns the value of the specific protocol in the MultiAddress

type MultiAddresses

type MultiAddresses []MultiAddress

MultiAddresses is an alias.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL
⌂ 换网址