Skip to content

Commit 985f555

Browse files
committed
Use snippets
1 parent aa127d0 commit 985f555

File tree

1 file changed

+57
-38
lines changed

1 file changed

+57
-38
lines changed

docs/examples/get_public_key/README.md

Lines changed: 57 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,37 @@
11
# Get Public Key Example
22

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.
44

5-
## Running the Example
5+
## Overview
66

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.
811

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.
1220

13-
## Code
21+
## Code Walkthrough
22+
23+
### 1. Setup User Wallet
1424

1525
```go
1626
package main
1727

1828
import (
1929
"context"
30+
"encoding/hex"
2031
"fmt"
32+
ec "github.com/bsv-blockchain/go-sdk/primitives/ec"
2133
"log"
2234

23-
"github.com/bsv-blockchain/go-sdk/primitives/ec"
2435
"github.com/bsv-blockchain/go-sdk/wallet"
2536
)
2637

@@ -32,15 +43,20 @@ func main() {
3243
if err != nil {
3344
log.Fatalf("Failed to generate user private key: %v", err)
3445
}
35-
log.Printf("User private key: %s", userKey.SerialiseWIF())
46+
log.Printf("User private key: %s", userKey.Wif())
3647

3748
// Create a new wallet for the user
3849
userWallet, err := wallet.NewWallet(userKey)
3950
if err != nil {
4051
log.Fatalf("Failed to create user wallet: %v", err)
4152
}
4253
log.Println("User wallet created successfully.")
54+
```
55+
First, we initialize a new wallet for the user.
56+
57+
### 2. Get User's Identity Public Key
4358
59+
```go
4460
// --- Get User's Own Public Key (Identity Key) ---
4561
identityPubKeyArgs := wallet.GetPublicKeyArgs{
4662
IdentityKey: true, // Indicates we want the root identity public key of the wallet
@@ -49,8 +65,13 @@ func main() {
4965
if err != nil {
5066
log.Fatalf("Failed to get user's identity public key: %v", err)
5167
}
52-
fmt.Printf("User's Identity Public Key: %s\n", identityPubKeyResult.PublicKey.SerialiseCompressed())
68+
fmt.Printf("User's Identity Public Key: %s\n", hex.EncodeToString(identityPubKeyResult.PublicKey.Compressed()))
69+
```
70+
To get the wallet's own root identity public key, we set `IdentityKey: true` in `GetPublicKeyArgs`.
71+
72+
### 3. Get User's Derived Public Key for Self
5373
74+
```go
5475
// --- Get User's Derived Public Key for a Protocol/KeyID (Self) ---
5576
// Define a protocol and key ID for deriving a key
5677
selfProtocol := wallet.Protocol{
@@ -73,22 +94,20 @@ func main() {
7394
log.Fatalf("Failed to get user's derived public key (self): %v", err)
7495
}
7596
fmt.Printf("User's Derived Public Key (Self - Protocol: %s, KeyID: %s): %s\n",
76-
selfProtocol.Protocol, selfKeyID, selfPubKeyResult.PublicKey.SerialiseCompressed())
97+
selfProtocol.Protocol, selfKeyID, hex.EncodeToString(selfPubKeyResult.PublicKey.Compressed()))
98+
```
99+
To derive a public key for the wallet's own use under a specific protocol and key ID, we set `Counterparty.Type` to `wallet.CounterpartyTypeSelf`.
100+
101+
### 4. Get Derived Public Key for a Counterparty
77102
103+
```go
78104
// --- Get Counterparty's Public Key ---
79105
// Generate a new private key for the counterparty
80106
counterpartyKey, err := ec.NewPrivateKey()
81107
if err != nil {
82108
log.Fatalf("Failed to generate counterparty private key: %v", err)
83109
}
84-
log.Printf("Counterparty private key: %s", counterpartyKey.SerialiseWIF())
85-
86-
// (Optional) Create a wallet for the counterparty - not strictly needed for this example part,
87-
// but useful if the counterparty wallet needs to perform actions.
88-
// counterpartyWallet, err := wallet.NewWallet(counterpartyKey)
89-
// if err != nil {
90-
// log.Fatalf("Failed to create counterparty wallet: %v", err)
91-
// }
110+
log.Printf("Counterparty private key: %s", counterpartyKey.Wif())
92111

93112
// Define a protocol and key ID for deriving a key with a counterparty
94113
counterpartyProtocol := wallet.Protocol{
@@ -115,32 +134,32 @@ func main() {
115134
log.Fatalf("Failed to get derived public key for counterparty: %v", err)
116135
}
117136
fmt.Printf("User's Derived Public Key (for Counterparty - Protocol: %s, KeyID: %s): %s\n",
118-
counterpartyProtocol.Protocol, counterpartyKeyID, derivedForCounterpartyPubKeyResult.PublicKey.SerialiseCompressed())
137+
counterpartyProtocol.Protocol, counterpartyKeyID, hex.EncodeToString(derivedForCounterpartyPubKeyResult.PublicKey.Compressed()))
119138

120139
log.Println("Successfully retrieved public keys.")
121140
}
122-
123141
```
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.
124143

125-
### Explanation
144+
## Running the Example
145+
146+
To run this example:
126147

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+
```
130153

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
135155

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).
139162

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
145164

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.
165+
- [go-sdk `wallet` package documentation](https://pkg.go.dev/github.com/bsv-blockchain/go-sdk/wallet)

0 commit comments

Comments
 (0)