GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — Building-Apps / Wallet: add Recovery SEP-30 instructions (#261) · stellar/stellar-docs@3146713

File tree

Expand file treeCollapse file tree
Open diff view settings
Filter options
  • docs/building-apps/wallet
    • sep30.mdx
Expand file treeCollapse file tree
Open diff view settings
Collapse file

‎docs/building-apps/wallet/sep30.mdx‎

Copy file name to clipboard
+315Lines changed: 315 additions & 0 deletions
  • Display the source diff
  • Display the rich diff
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,315 @@
1+
---
2+
title: Recovery
3+
sidebar_position: 60
4+
---
5+
6+
import Header from "./component/header.mdx";
7+
import { WalletCodeExample as CodeExample } from "@site/src/components/WalletCodeExample";
8+
9+
<Header WIPLangs={["dart"]} />
10+
11+
The [Sep-30] standard defines the standard way for an individual (e.g., a user or wallet) to regain access to their Stellar account after losing its private key without providing any third party control of the account. During this flow the wallet communicates with one or more recovery signer servers to register the wallet for a later recovery if it's needed.
12+
13+
## Create Recoverable Account
14+
15+
First, let's create an account key, a device key, and a recovery key that will be attached to the account.
16+
17+
<CodeExample>
18+
19+
```typescript
20+
const accountKp = wallet.stellar().account().createKeypair();
21+
const deviceKp = wallet.stellar().account().createKeypair();
22+
const recoveryKp = wallet.stellar().account().createKeypair();
23+
```
24+
25+
```kotlin
26+
val accountKp = wallet.stellar().account().createKeyPair()
27+
val deviceKp = wallet.stellar().account().createKeyPair()
28+
val recoveryKp = wallet.stellar().account().createKeyPair()
29+
```
30+
31+
</CodeExample>
32+
33+
The `accountKp` is the wallet's main account. The `deviceKp` we will be adding to the wallet as a signer so a device (eg. a mobile device a wallet is hosted on) can take control of the account. And the `recoveryKp` will be used to identify the key with the recovery servers.
34+
35+
Next, let's identify the recovery servers and create our recovery object:
36+
37+
<CodeExample>
38+
39+
```typescript
40+
const server1Key = "server1";
41+
const server1 = {
42+
endpoint: "recovery-example.com",
43+
authEndpoint: "auth-example.com",
44+
homeDomain: "test-domain",
45+
};
46+
47+
const server2Key = "server2";
48+
const server2 = {
49+
endpoint: "recovery-example2.com",
50+
authEndpoint: "auth-example2.com",
51+
homeDomain: "test-domain2",
52+
};
53+
54+
const recovery = wallet.recovery({
55+
servers: { [server1Key]: server1, [server2Key]: server2 },
56+
});
57+
```
58+
59+
```kotlin
60+
val first = RecoveryServerKey("first")
61+
val second = RecoveryServerKey("second")
62+
val firstServer = RecoveryServer("recovery.example.com", "auth.example.com", "example.com")
63+
val secondServer = RecoveryServer("recovery2.example.com", "auth2.example.com", "example.com")
64+
val servers = mapOf(first to firstServer, second to secondServer)
65+
val recovery = wallet.recovery(servers)
66+
```
67+
68+
</CodeExample>
69+
70+
Next, we need to define SEP-30 identities. In this example we are going to create an identity for both servers.
71+
72+
<CodeExample>
73+
74+
```typescript
75+
const identity1 = {
76+
role: RecoveryRole.OWNER,
77+
authMethods: [
78+
{
79+
type: RecoveryType.STELLAR_ADDRESS,
80+
value: recoveryKp.publicKey,
81+
},
82+
],
83+
};
84+
85+
const identity2 = {
86+
role: RecoveryRole.OWNER,
87+
authMethods: [
88+
{
89+
type: RecoveryType.STELLAR_ADDRESS,
90+
value: recoveryKp.publicKey,
91+
},
92+
{
93+
type: RecoveryType.EMAIL,
94+
value: "[email protected]",
95+
},
96+
],
97+
};
98+
```
99+
100+
```kotlin
101+
val identities =
102+
listOf(
103+
RecoveryAccountIdentity(
104+
RecoveryRole.OWNER,
105+
listOf(RecoveryAccountAuthMethod(RecoveryType.STELLAR_ADDRESS, recoveryKp.address))
106+
)
107+
)
108+
```
109+
110+
</CodeExample>
111+
112+
Here, stellar key will be used as a recovery method. Other recovery servers may support email or phone as a recovery methods.
113+
114+
You can read more about SEP-30 identities [here](https://github.com/stellar/stellar-protocol/blob/master/ecosystem/sep-0030.md#common-request-fields)
115+
116+
Next, let's create a recoverable account:
117+
118+
<CodeExample>
119+
120+
```typescript
121+
const config = {
122+
accountAddress: accountKp,
123+
deviceAddress: deviceKp,
124+
accountThreshold: { low: 10, medium: 10, high: 10 },
125+
accountIdentity: { [server1Key]: [identity1], [server2Key]: [identity2] },
126+
signerWeight: { device: 10, recoveryServer: 5 },
127+
};
128+
const recoverableWallet = await recovery.createRecoverableWallet(config);
129+
```
130+
131+
```kotlin
132+
val recoverableWallet =
133+
recovery.createRecoverableWallet(
134+
RecoverableWalletConfig(
135+
accountKp,
136+
deviceKp,
137+
AccountThreshold(10, 10, 10),
138+
mapOf(first to identities, second to identities),
139+
SignerWeight(10, 5)
140+
)
141+
)
142+
```
143+
144+
</CodeExample>
145+
146+
With the given parameters, this function will create a transaction that will:
147+
148+
1. Set `deviceKp` as the primary account key. Please note that the master key belonging to `accountKp` will be locked. `deviceKp` should be used as a primary signer instead.
149+
2. Set all operation thresholds to 10. You can read more about threshold in the [documentation](https://developers.stellar.org/docs/encyclopedia/signatures-multisig#thresholds)
150+
3. Use identities that were defined earlier on both servers. (That means, both server will accept SEP-10 authentication via `recoveryKp` as an auth method)
151+
4. Set device key weight to 10, and recovery server weight to 5. Given these account thresholds, both servers must be used to recover the account, as transaction signed by one will only have weight of 5, which is not sufficient to change account key.
152+
153+
Finally, sign and submit transaction to the network:
154+
155+
<CodeExample>
156+
157+
```typescript
158+
recoverableWallet.transaction.sign(accountKp.keypair);
159+
160+
await stellar.submitTransaction(recoverableWallet.transaction);
161+
```
162+
163+
```kotlin
164+
val tx = recoverableWallet.transaction.sign(accountKp)
165+
166+
wallet.stellar().submitTransaction(tx)
167+
```
168+
169+
</CodeExample>
170+
171+
## Get Account Info
172+
173+
You can fetch account info from one or more servers. To do so, first we need to authenticate with a recovery server using the SEP-10 authentication method:
174+
175+
<CodeExample>
176+
177+
```typescript
178+
const authToken = await recovery
179+
.sep10Auth(server1Key)
180+
.authenticate({ accountKp: recoveryKp });
181+
```
182+
183+
```kotlin
184+
val auth1 = recovery.sep10Auth(first).authenticate(recoveryKp)
185+
```
186+
187+
</CodeExample>
188+
189+
Next, get account info using auth tokens:
190+
191+
<CodeExample>
192+
193+
```typescript
194+
const accountResp = await recovery.getAccountInfo(accountKp, {
195+
[server1Key]: authToken,
196+
});
197+
```
198+
199+
```kotlin
200+
val accountInfo = recovery.getAccountInfo(account, mapOf(first to auth1))
201+
202+
println("Recoverable info: $accountInfo")
203+
```
204+
205+
</CodeExample>
206+
207+
## Recover Wallet
208+
209+
Let's say we've lost our device key and need to recover our wallet.
210+
211+
First, we need to authenticate with both recovery servers:
212+
213+
<CodeExample>
214+
215+
```typescript
216+
const authToken1 = await recovery
217+
.sep10Auth(server1Key)
218+
.authenticate({ accountKp: recoveryKp });
219+
220+
const authToken2 = await recovery
221+
.sep10Auth(server1Key)
222+
.authenticate({ accountKp: recoveryKp });
223+
```
224+
225+
```kotlin
226+
val auth1 = recovery.sep10Auth(first).authenticate(recoveryKp)
227+
val auth2 = recovery.sep10Auth(second).authenticate(recoveryKp)
228+
```
229+
230+
</CodeExample>
231+
232+
We need to know the recovery signer addresses that will be used to sign the transaction. You can get them from either the recoverable wallet object we created earlier (`recoverableWallet.signers`), or via fetching account info from recovery servers.
233+
234+
<CodeExample>
235+
236+
```typescript
237+
const recoverySignerAddress1 = recoverableWallet.signers[0];
238+
const recoverySignerAddress2 = recoverableWallet.signers[1];
239+
```
240+
241+
```kotlin
242+
val recoverySigners = recoverableWallet.signers
243+
```
244+
245+
</CodeExample>
246+
247+
Next, create a new device key and retrieve a signed transaction that replaces the device key:
248+
249+
<CodeExample>
250+
251+
```typescript
252+
const newDeviceKp = accountService.createKeypair();
253+
254+
const serverAuth = {
255+
[server1Key]: {
256+
signerAddress: recoverySignerAddress1,
257+
authToken1,
258+
},
259+
[server2Key]: {
260+
signerAddress: recoverySignerAddress2,
261+
authToken2,
262+
},
263+
};
264+
265+
const recoverTxn = await recovery.replaceDeviceKey(
266+
accountKp,
267+
newDeviceKp,
268+
serverAuth,
269+
);
270+
```
271+
272+
```kotlin
273+
val newKey = wallet.stellar().account().createKeyPair()
274+
275+
val serverAuth = mapOf(
276+
first to RecoveryServerSigning(recoverySigners[0], auth1),
277+
second to RecoveryServerSigning(recoverySigners[1], auth2)
278+
)
279+
280+
val signedReplaceKeyTransaction =
281+
recovery.replaceDeviceKey(
282+
accountKp,
283+
newKey,
284+
serverAuth
285+
)
286+
```
287+
288+
</CodeExample>
289+
290+
Calling this function will create a transaction that locks the previous device key and replaces it with your new key (having the same weight as the old one). Both recovery signers will have signed the transaction.
291+
292+
The lost device key is deduced automatically if not given. A signer will be considered a device key, if one of these conditions matches:
293+
294+
1. It's the only signer that's not in `serverAuth`.
295+
2. All signers in `serverAuth` have the same weight, and the potential signer is the only one with a different weight.
296+
297+
Note that the account created above will match the first criteria. If 2-3 schema were used, then second criteria would match. (In 2-3 schema, 3 serves are used and 2 of them is enough to recover key. This is a recommended approach.)
298+
299+
Note: you can also use more low-level `signWithRecoveryServers` functions to sign arbitrary transaction.
300+
301+
Finally, it's time to submit the transaction:
302+
303+
<CodeExample>
304+
305+
```typescript
306+
await stellar.submitTransaction(recoverTxn);
307+
```
308+
309+
```kotlin
310+
wallet.stellar().submitTransaction(signedReplaceKeyTransaction)
311+
```
312+
313+
</CodeExample>
314+
315+
[sep-30]: https://github.com/stellar/stellar-protocol/blob/master/ecosystem/sep-0030.md

0 commit comments

Comments
0 (0)
You are visitor 01402792 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.