|
| 1 | +--- |
| 2 | +sidebar_position: 5 |
| 3 | +--- |
| 4 | + |
| 5 | +# keypairs |
| 6 | + |
| 7 | +## Introduction |
| 8 | + |
| 9 | +The keypairs package provides a set of functions for generating and managing cryptographic keypairs. It includes functionality for creating new keypairs, deriving public keys from private keys, and verifying signatures. |
| 10 | + |
| 11 | +This package is used internally by the [`xrpl`](./xrpl.md) package to expose a `Wallet`[Afegir link] interface for easier wallet management. Nevertheless, it can be used independently from the [`xrpl`](./xrpl.md) package for cryptographic operations. |
| 12 | + |
| 13 | +## Key components |
| 14 | + |
| 15 | +This package works with the following key components from the XRP Ledger: |
| 16 | + |
| 17 | +- **Seed**: A base58-encoded string that represents a keypair. |
| 18 | +- **Keypair**: A pair of a private and public key. |
| 19 | +- **Address**: A base58-encoded string that represents an account. |
| 20 | + |
| 21 | +To learn more about these components, you can check the [official documentation](https://xrpl.org/docs/concepts/accounts/cryptographic-keys). |
| 22 | + |
| 23 | +## Supported algorithms |
| 24 | + |
| 25 | +Cryptographic algorithms supported by this package are: |
| 26 | + |
| 27 | +- ed25519 |
| 28 | +- secp256k1 |
| 29 | + |
| 30 | + Every function in the package that requires a cryptographic algorithm will accept any type that satisfies the `KeypairCryptoAlg` interface. So, if desired, you can implement your own algorithm and use it in this package. |
| 31 | + |
| 32 | + However, the library already exports both algorithm getters that satisfy the `KeypairCryptoAlg` and `NodeDerivationCryptoAlg` interfaces. They're available under the package `github.com/Peersyst/xrpl-go/pkg/crypto`, which exports both algorithm getters that satisfy the `KeypairCryptoAlg`, `NodeDerivationCryptoAlg` interfaces. |
| 33 | + |
| 34 | +### crypto package |
| 35 | + |
| 36 | +The `crypto` package exports the following algorithm getters that satisfy the `KeypairCryptoAlg`, `NodeDerivationCryptoAlg` interfaces: |
| 37 | + |
| 38 | +- `ED25519()` |
| 39 | +- `SECP256K1()` |
| 40 | + |
| 41 | +You can use them to generate a seed or derive a keypair as the following example shows: |
| 42 | + |
| 43 | +```go |
| 44 | +seed, err := keypairs.GenerateSeed("", crypto.SECP256K1(), random.NewRandomizer()) |
| 45 | +``` |
| 46 | + |
| 47 | +## API |
| 48 | + |
| 49 | +These are the functions available in this package: |
| 50 | + |
| 51 | +```go |
| 52 | +// Key generation |
| 53 | +func GenerateSeed(entropy string, alg interfaces.KeypairCryptoAlg, r interfaces.Randomizer) (string, error) |
| 54 | +func DeriveKeypair(seed string, validator bool) (private, public string, err error) |
| 55 | +func DeriveClassicAddress(pubKey string) (string, error) |
| 56 | +func DeriveNodeAddress(pubKey string, alg interfaces.NodeDerivationCryptoAlg) (string, error) |
| 57 | + |
| 58 | +// Signing |
| 59 | +func Sign(msg, privKey string) (string, error) |
| 60 | +func Validate(msg, pubKey, sig string) (bool, error) |
| 61 | +``` |
| 62 | + |
| 63 | +They can be split into two groups: |
| 64 | + |
| 65 | +- Key generation: Functions that generate seeds and addresses. |
| 66 | +- Signing: Functions that sign and validate messages. |
| 67 | + |
| 68 | +### Key generation |
| 69 | + |
| 70 | +#### GenerateSeed |
| 71 | + |
| 72 | +```go |
| 73 | +func GenerateSeed(entropy string, alg interfaces.KeypairCryptoAlg, r interfaces.Randomizer) (string, error) |
| 74 | +``` |
| 75 | + |
| 76 | +Generate a seed that can be used to generate keypairs. You can specify the entropy, of the seed or let the function generate a random one (by passing an empty string as entropy and providing a randomizer) and use one of the [supported algorithms][afegir link] the provided algorithm to generate the seed. The result is a base58-encoded seed, which starts with the character `s`. |
| 77 | + |
| 78 | +:::info |
| 79 | + |
| 80 | +A randomizer satisfies the `Randomizer` interface. The `random` package exports a `NewRandomizer` function that returns a new randomizer. |
| 81 | + |
| 82 | +::: |
| 83 | + |
| 84 | +#### DeriveKeypair |
| 85 | + |
| 86 | +```go |
| 87 | +func DeriveKeypair(seed string, validator bool) (private, public string, err error) |
| 88 | +``` |
| 89 | + |
| 90 | +Derives a keypair (private and public keys) from a seed. If the `validator` parameter is `true`, the keypair will be a validator keypair; otherwise, it will be a user keypair. The result for both the private and public keys is a 33-byte hexadecimal string. |
| 91 | + |
| 92 | + |
| 93 | +#### DeriveClassicAddress |
| 94 | + |
| 95 | +```go |
| 96 | +func DeriveClassicAddress(pubKey string) (string, error) |
| 97 | +``` |
| 98 | + |
| 99 | +After deriving a keypair, you can derive the classic address from the public key. The result is a base58 encoded address, which starts with the character `r`. If you're interested in X-Address derivation, [`address-codec`](./address-codec.md) package contains functions to encode and decode X-Addresses from and to classic addresses. |
| 100 | + |
| 101 | +#### DeriveNodeAddress |
| 102 | + |
| 103 | +```go |
| 104 | +func DeriveNodeAddress(pubKey string, alg interfaces.NodeDerivationCryptoAlg) (string, error) |
| 105 | +``` |
| 106 | + |
| 107 | +Derives a node address from a public key. The result is a base58-encoded address, which starts with the character `n`. |
| 108 | + |
| 109 | +### Signing |
| 110 | + |
| 111 | +#### Sign |
| 112 | + |
| 113 | +```go |
| 114 | +func Sign(msg, privKey string) (string, error) |
| 115 | +``` |
| 116 | + |
| 117 | +Signs the provided message with the provided private key. To be able to sign a message, the private key must be a valid keypair and the message must be hex-encoded. The result is a hexadecimal string that represents the signature of the message. To verify the signature, you can use the `Validate` function. |
| 118 | + |
| 119 | +#### Validate |
| 120 | + |
| 121 | +```go |
| 122 | +func Validate(msg, pubKey, sig string) (bool, error) |
| 123 | +``` |
| 124 | + |
| 125 | +Verifies a signature of a message. To be able to verify a signature, the public key must be valid, and the message and the signature must be hex-encoded. The result is a boolean value that indicates if the signature is valid or not. |
| 126 | + |
| 127 | +## Guides |
| 128 | + |
| 129 | +### How to generate a new random keypair |
| 130 | + |
| 131 | +This example generates a new keypair using the `SECP256K1` algorithm and a random entropy. It then derives a keypair from the seed and derives the classic address from the public key. |
| 132 | + |
| 133 | +```go |
| 134 | +package main |
| 135 | + |
| 136 | +import ( |
| 137 | + "fmt" |
| 138 | + "log" |
| 139 | + |
| 140 | + "github.com/Peersyst/xrpl-go/keypairs" |
| 141 | + "github.com/Peersyst/xrpl-go/pkg/crypto" |
| 142 | + "github.com/Peersyst/xrpl-go/pkg/random" |
| 143 | +) |
| 144 | + |
| 145 | +func main() { |
| 146 | + seed, err := keypairs.GenerateSeed("", crypto.SECP256K1(), random.NewRandomizer()) |
| 147 | + if err != nil { |
| 148 | + log.Fatal(err) |
| 149 | + } |
| 150 | + |
| 151 | + privK, pubK, err := keypairs.DeriveKeypair(seed, false) |
| 152 | + if err != nil { |
| 153 | + log.Fatal(err) |
| 154 | + } |
| 155 | + |
| 156 | + addr, err := keypairs.DeriveClassicAddress(pubK) |
| 157 | + if err != nil { |
| 158 | + log.Fatal(err) |
| 159 | + } |
| 160 | + |
| 161 | + fmt.Println("Seed: ", seed) |
| 162 | + fmt.Println("Private Key: ", privK) |
| 163 | + fmt.Println("Public Key: ", pubK) |
| 164 | + fmt.Println("Address: ", addr) |
| 165 | +} |
| 166 | +``` |
| 167 | + |
| 168 | + |
| 169 | +### How to generate a new keypair from entropy |
| 170 | + |
| 171 | +This example generates a new keypair using the `ED25519` algorithm and a provided entropy. Then, it derives the keypair and the address as the previous example. |
| 172 | + |
| 173 | +```go |
| 174 | +package main |
| 175 | + |
| 176 | +import ( |
| 177 | + "fmt" |
| 178 | + "log" |
| 179 | + |
| 180 | + "github.com/Peersyst/xrpl-go/keypairs" |
| 181 | + "github.com/Peersyst/xrpl-go/pkg/crypto" |
| 182 | +) |
| 183 | + |
| 184 | +func main() { |
| 185 | + seed, err := keypairs.GenerateSeed("ThisIsMyCustomEntropy", crypto.ED25519(), nil) |
| 186 | + if err != nil { |
| 187 | + log.Fatal(err) |
| 188 | + } |
| 189 | + |
| 190 | + privK, pubK, err := keypairs.DeriveKeypair(seed, false) |
| 191 | + if err != nil { |
| 192 | + log.Fatal(err) |
| 193 | + } |
| 194 | + |
| 195 | + addr, err := keypairs.DeriveClassicAddress(pubK) |
| 196 | + |
| 197 | + fmt.Println("Seed: ", seed) |
| 198 | + fmt.Println("Private Key: ", privK) |
| 199 | + fmt.Println("Public Key: ", pubK) |
| 200 | + fmt.Println("Address: ", addr) |
| 201 | +} |
| 202 | +``` |
0 commit comments