You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: docs/examples/get_public_key/README.md
+57-38Lines changed: 57 additions & 38 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -1,26 +1,37 @@
1
1
# Get Public Key Example
2
2
3
-
This example demonstrates how to retrieve a public key from a wallet. It covers scenarios for retrieving the user's own public key and a counterparty's public key.
3
+
This example demonstrates how to use the `wallet` package to retrieve various types of public keys associated with a wallet.
4
4
5
-
## Running the Example
5
+
## Overview
6
6
7
-
To run this example, navigate to the `go-sdk/docs/examples/get_public_key` directory and execute the following command:
7
+
The `wallet.Wallet` can manage multiple cryptographic keys. The `GetPublicKey` method allows you to retrieve:
8
+
1.**Identity Public Key**: The root public key of the wallet itself.
9
+
2.**Derived Public Key (Self)**: A public key derived for a specific protocol and key ID, intended for the wallet's own use (e.g., for self-encryption or signing).
10
+
3.**Derived Public Key (for Counterparty)**: A public key derived by the user's wallet that is specifically for interacting with a known counterparty. This is often used in shared secret generation schemes like ECIES.
8
11
9
-
```bash
10
-
go run get_public_key.go
11
-
```
12
+
## Example Overview
13
+
14
+
This example demonstrates:
15
+
16
+
1. Creating a user's wallet.
17
+
2. Retrieving the user's identity public key.
18
+
3. Retrieving a public key derived by the user's wallet for its own use under a specific protocol and key ID.
19
+
4. Simulating a counterparty and retrieving a public key derived by the user's wallet for interacting with that counterparty under a specific protocol and key ID.
log.Println("Successfully retrieved public keys.")
121
140
}
122
-
123
141
```
142
+
To get a public key derived by the user's wallet for interaction with a specific counterparty, we set `Counterparty.Type` to `wallet.CounterpartyTypeOther` and provide the `Counterparty.Counterparty` public key. The `ForSelf` field in `GetPublicKeyArgs` defaults to `false`, indicating the derived key is for the counterparty.
124
143
125
-
### Explanation
144
+
## Running the Example
145
+
146
+
To run this example:
126
147
127
-
1.**Setup**:
128
-
* We import necessary packages: `context`, `fmt` for printing, `log` for error handling, `ec` for elliptic curve cryptography (key generation), and `wallet` for wallet operations.
129
-
* A `context.Background()` is created.
148
+
```bash
149
+
cd go-sdk/docs/examples/get_public_key
150
+
go mod tidy
151
+
go run get_public_key.go
152
+
```
130
153
131
-
2.**User Wallet and Identity Key**:
132
-
* A new private key (`userKey`) is generated using `ec.NewPrivateKey()`.
133
-
* A `wallet.NewWallet` is initialized using this `userKey`.
134
-
* To get the user's own root identity public key, `userWallet.GetPublicKey` is called with `wallet.GetPublicKeyArgs{IdentityKey: true}`. The `PublicKey` field in the result contains the public key.
154
+
## Key Concepts
135
155
136
-
3.**User's Derived Public Key (Self)**:
137
-
* We define a `wallet.Protocol` and a `keyID`. These are used to derive specific keys for different applications or purposes.
138
-
*`userWallet.GetPublicKey` is called with `EncryptionArgs` specifying the `ProtocolID`, `KeyID`, and `Counterparty` set to `wallet.CounterpartyTypeSelf`. This tells the wallet to derive a public key for the user themselves under that protocol and key ID.
156
+
-**`wallet.GetPublicKeyArgs`**: Struct used to specify parameters for retrieving public keys.
157
+
-**`IdentityKey`**: Boolean flag in `GetPublicKeyArgs`. If true, retrieves the wallet's root identity public key.
158
+
-**`EncryptionArgs`**: Embedded in `GetPublicKeyArgs`, used for deriving keys based on protocol, key ID, and counterparty.
159
+
-**`CounterpartyTypeSelf`**: Used in `EncryptionArgs.Counterparty.Type` to derive a key for the wallet's own use.
160
+
-**`CounterpartyTypeOther`**: Used in `EncryptionArgs.Counterparty.Type` along with the counterparty's public key to derive a key for interacting with that counterparty.
161
+
-**`ForSelf`**: Field in `GetPublicKeyArgs` (defaults to `false`). When `false` and `CounterpartyTypeOther` is used, it means the public key derived is the one the user's wallet would use for the specified counterparty (e.g., as a component in an ECIES shared secret calculation).
139
162
140
-
4.**Counterparty Public Key**:
141
-
* A new private key (`counterpartyKey`) is generated for a simulated counterparty. In a real application, you would typically receive the counterparty's public key through other means.
142
-
* We define another `wallet.Protocol` and `keyID` that might be used for shared interactions.
143
-
*`userWallet.GetPublicKey` is called again. This time, `EncryptionArgs.Counterparty` is set to `wallet.CounterpartyTypeOther` and `Counterparty: counterpartyKey.PubKey()`.
144
-
* The `ForSelf` field in `GetPublicKeyArgs` is `false` by default. This means the call retrieves the public key that `userWallet` would use when interacting *with* the specified counterparty under the given protocol and key ID. This is typically part of an ECDH key agreement process.
163
+
## Additional Resources
145
164
146
-
The example prints the WIF private keys (for demonstration; handle private keys securely in real applications) and the compressed public keys obtained in each scenario.
0 commit comments