Skip to content

Commit a1cbd82

Browse files
authored
Update and review conversion guides (#1904)
* light touches and a python example for address conversions Fixes #1858 * remove one unnecessary set of codeblock tags * update the bytes conversion guide Fixes #1859 * update the string conversions guide Fixes #1867 * remove unused imports * update scval conversions guide Fixes #1866 * simplify scval conversions from base64 strings
1 parent 605bbc4 commit a1cbd82

4 files changed

Lines changed: 109 additions & 170 deletions

File tree

docs/build/guides/conversions/address-conversions.mdx

Lines changed: 25 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,11 @@ import { CodeExample } from "@site/src/components/CodeExample";
88

99
The `Address` is an opaque type that represents either a 'default' externally owned account on the Stellar network, or a contract (that may also provide logic for custom externally owned accounts, see [authorization docs](../../../learn/fundamentals/contract-development/authorization.mdx#account-abstraction) for details). For the smart contracts it normally doesn't matter which kind of `Address` is used. However, in some contexts it's useful to convert `Address` to/from different data types, such as string or XDR. The conversions have distinctly different purpose depending on whether they happen in the smart contract itself, or in the client code.
1010

11-
# String conversions
11+
## String conversions
1212

1313
The default string format for `Address` is a so called 'string key' (or 'strkey'), defined fully in [SEP-23](https://github.com/stellar/stellar-protocol/blob/master/ecosystem/sep-0023.md). For the default externally owned accounts strkey always starts with `G` and for all the contracts it starts with `C`.
1414

15-
## Conversions in Client SDKs
15+
### String Conversions in Client SDKs
1616

1717
Outside of the smart contracts, it is convenient to represent `Address` as string most of the time. It can be stored in string serialization formats such as JSON and XML. Storing addresses as strings in databases can simplify database schema design and queries. Strings are easier to manipulate and are more compatible with user interfaces and APIs. Thus
1818

@@ -30,46 +30,49 @@ const address = new StellarSdk.Address(stellarAddress);
3030
const addressToString = address.toString();
3131
```
3232

33+
```python
34+
from stellar_sdk import Address
35+
36+
# Example Stellar address
37+
stellar_address = "GCM5WPR4DDR24FSAX5LIEM4J7AI3KOWJYANSXEPKYXCSZOTAYXE75AFN"
38+
# Create an Address object from string
39+
address = Address(stellar_address)
40+
# Convert the address back to string
41+
address_to_string = address.address
42+
```
43+
3344
</CodeExample>
3445

35-
## Conversions in Smart Contracts
46+
### String Conversions in Smart Contracts
3647

3748
It's generally preferred for contracts to operate directly on the `Address` type. String conversions may be useful for specialized use cases, such as passing the Stellar `Address`es to/from other chains.
3849

39-
<CodeExample>
40-
4150
```rust
4251
use soroban_sdk::{Address, String, Env};
4352

4453
pub fn address_to_string(address: Address) -> String {
4554
address.to_string()
4655
}
4756

48-
pub fn address_from_string(strkey: &String) -> Address {
49-
Address::from_string(strkey)
57+
pub fn address_from_string(strkey: String) -> Address {
58+
Address::from_string(&strkey)
5059
}
5160
```
5261

53-
</CodeExample>
54-
5562
`Address` can also be built from a string literal, which may be useful for testing.
5663

57-
<CodeExample>
58-
5964
```rust
6065
let test_address = Address::from_str(
6166
&env,
6267
"GCM5WPR4DDR24FSAX5LIEM4J7AI3KOWJYANSXEPKYXCSZOTAYXE75AFN",
6368
);
6469
```
6570

66-
</CodeExample>
67-
68-
# XDR conversions
71+
## XDR conversions
6972

7073
XDR is schema-based binary serialization format used by the Stellar network. It is used for all the Stellar blockchain interactions, such as building the transactions, storing data in the ledger, communicating the transaction results etc. Stellar SDKs provide the typed wrappers for all the Stellar XDR data types. Address is represented as `ScAddress` type, which can then be wrapped into `ScVal` which is a type that represents any contract type supported by Stellar contracts.
7174

72-
## Conversions in Client SDKs
75+
### XDR Conversions in Client SDKs
7376

7477
On the client side XDR conversions are useful to build the transactions and process the transaction results.
7578

@@ -90,7 +93,7 @@ const scAddress = address.toScAddress();
9093
```
9194

9295
```python
93-
from stellar_sdk.address import Address
96+
from stellar_sdk import Address
9497

9598
# Example Stellar address
9699
stellar_address = 'GBJCHUKZMTFSLOMNC7P4TS4VJJBTCYL3XKSOLXAUJSD56C4LHND5TWUC'
@@ -102,27 +105,23 @@ sc_address_xdr = address.to_xdr_sc_address()
102105

103106
</CodeExample>
104107

105-
## Conversions in Smart Contracts
108+
### XDR Conversions in Smart Contracts
106109

107110
Smart contracts don't need to explicitly interact with the XDR types, as all the smart contract data types are automatically converted to XDR by the smart contract runtime. Every contract type, including `Address`, can be serialized to XDR bytes. This conversion is useful, for example, for performing hashing in smart contracts. It is also possible to convert the serialized XDR bytes back to contract types, which can be useful in some narrow use cases, such as custom authentication schemes.
108111

109-
Note, that XDR conversions are and advanced feature and are not necessary for most of the Stellar smart contracts.
110-
111-
<CodeExample>
112+
Note, that XDR conversions are an advanced feature and are not necessary for most Stellar smart contracts.
112113

113114
```rust
114115
use soroban_sdk::{
115116
xdr::{FromXdr, ToXdr},
116117
Address, Bytes, Env,
117118
};
118119

119-
pub fn address_to_xdr_bytes(env: &Env, address: Address) -> Bytes {
120-
address.to_xdr(env)
120+
pub fn address_to_xdr_bytes(env: Env, address: Address) -> Bytes {
121+
address.to_xdr(&env)
121122
}
122123

123-
pub fn address_from_xdr_bytes(env: &Env, bytes: &Bytes) -> Address {
124-
Address::from_xdr(env, bytes).unwrap()
124+
pub fn address_from_xdr_bytes(env: Env, bytes: Bytes) -> Address {
125+
Address::from_xdr(&env, &bytes).unwrap()
125126
}
126127
```
127-
128-
</CodeExample>

docs/build/guides/conversions/bytes-conversions.mdx

Lines changed: 13 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ When retrieving data stored on the blockchain, addresses might be stored in byte
1515
<CodeExample>
1616

1717
```rust
18-
use soroban_sdk::{Address, Bytes, Env};
18+
use soroban_sdk::{Address, Bytes};
1919

2020
pub fn bytes_to_address(bytes: Bytes) -> Address {
2121
Address::from_string_bytes(&bytes)
@@ -42,7 +42,7 @@ addressFromBytes.toString();
4242
from stellar_sdk.address import Address
4343

4444
# Example bytes value
45-
raw_bytes = bytes.fromhex('99db3e3c18e3ae1640bf56823389f811b53ac9c01b2b91eac5c52cba60c5c9fe')
45+
raw_bytes = bytes.fromhex("99db3e3c18e3ae1640bf56823389f811b53ac9c01b2b91eac5c52cba60c5c9fe")
4646
bytes_to_address = Address.from_raw_account(raw_bytes)
4747
```
4848

@@ -55,16 +55,14 @@ When dealing with binary data, you may need to convert certain portions of the d
5555
<CodeExample>
5656

5757
```rust
58-
use soroban_sdk::{String, Bytes, Env, FromVal};
58+
use soroban_sdk::{String, Bytes};
5959

60-
pub fn bytes_to_string(env: &Env, bytes: Bytes) -> String {
61-
String::from_val(env, &bytes.to_val())
60+
pub fn bytes_to_string(bytes: Bytes) -> String {
61+
String::from(bytes)
6262
}
6363
```
6464

6565
```js
66-
const StellarSdk = require("@stellar/stellar-sdk");
67-
6866
// Example bytes value
6967
const rawBytes = Buffer.from(
7068
"99db3e3c18e3ae1640bf56823389f811b53ac9c01b2b91eac5c52cba60c5c9fe",
@@ -75,12 +73,10 @@ const bytesToString = rawBytes.toString("hex");
7573
```
7674

7775
```python
78-
from stellar_sdk.address import Address
79-
80-
# Example bytes
81-
raw_bytes = b'99db3e3c18e3ae1640bf56823389f811b53ac9c01b2b91eac5c52cba60c5c9fe'
76+
# Example bytes value
77+
raw_bytes = bytes.fromhex("99db3e3c18e3ae1640bf56823389f811b53ac9c01b2b91eac5c52cba60c5c9fe")
8278
# Convert bytes to string
83-
bytes_to_string = raw_bytes.decode('utf-8')
79+
bytes_to_string = raw_bytes.hex()
8480
```
8581

8682
</CodeExample>
@@ -92,30 +88,26 @@ In a Soroban smart contract that interacts with an external oracle service to pr
9288
<CodeExample>
9389

9490
```rust
95-
use soroban_sdk::{Bytes, Env, FromVal, Val};
91+
use soroban_sdk::{Bytes, Val};
9692

97-
pub fn bytes_to_val(env: &Env, bytes: Bytes) -> Val {
98-
Val::from_val(env, &bytes.to_val())
93+
pub fn bytes_to_val(bytes: Bytes) -> Val {
94+
Val::from(bytes)
9995
}
10096
```
10197

10298
```js
103-
const StellarSdk = require("@stellar/stellar-sdk");
104-
10599
// Example bytes value
106100
const rawBytes = Buffer.from(
107101
"99db3e3c18e3ae1640bf56823389f811b53ac9c01b2b91eac5c52cba60c5c9fe",
108102
"hex",
109103
);
110104
// Convert bytes to xdr.ScVal
111-
const bytesToScVal = StellarSdk.xdr.ScVal.scvBytes(rawBytes);
105+
const bytesToScVal = StellarSdk.nativeToScVal(rawBytes);
112106
```
113107

114108
```python
115-
import stellar_sdk
116-
117109
# Example bytes value
118-
raw_bytes = b'example_bytes_data'
110+
raw_bytes = bytes.fromhex("99db3e3c18e3ae1640bf56823389f811b53ac9c01b2b91eac5c52cba60c5c9fe")
119111
# Convert bytes to ScVal
120112
sc_val = stellar_sdk.scval.to_bytes(raw_bytes)
121113
```

docs/build/guides/conversions/scval-conversions.mdx

Lines changed: 32 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -4,98 +4,66 @@ hide_table_of_contents: true
44
description: Convert a ScVal to other type
55
---
66

7-
import Tabs from "@theme/Tabs";
8-
import TabItem from "@theme/TabItem";
7+
import { CodeExample } from "@site/src/components/CodeExample";
98

109
Soroban Contract Value (`ScVal`) is a custom type defined within the Soroban runtime environment that represents other data types such as strings, bytes, and more complex structures used within smart contracts in a format that that the soroban runtime can process, store and retrieve efficiently.
1110

12-
## ScVal to bytesN
11+
## ScVal to bytes
1312

14-
<Tabs groupId="language">
15-
<TabItem value="js" label="JS">
13+
<CodeExample>
1614

1715
```js
18-
const StellarSdk = require("@stellar/stellar-sdk");
19-
20-
/* Convert ScVal to bytes */
21-
const bytesFromScVal = StellarSdk.scVal.bytes();
16+
// An ScVal bytes value
17+
const myScVal = StellarSdk.xdr.ScVal.fromXDR("AAAADQAAAARQ/8AB", "base64");
18+
// Convert the ScVal to a Buffer
19+
const bytesFromScVal = StellarSdk.scValToNative(myScVal);
2220
```
2321

24-
- `.bytes()` converts an ScVal value to bytes.
25-
- `scVal` is the ScVal value to be converted to bytes.
26-
27-
</TabItem>
28-
<TabItem value="python" label="Python">
29-
3022
```python
31-
import stellar_sdk
32-
33-
# Convert ScVal to bytes
34-
sc_val_to_bytes = stellar_sdk.scval.from_bytes(sc_val)
23+
# Convert the ScVal to bytes
24+
bytes_from_sc_val = stellar_sdk.scval.from_bytes("AAAADQAAAARQ/8AB")
3525
```
3626

37-
- `stellar_sdk.scval.from_bytes()` converts an ScVal value to bytes.
38-
- `sc_val` is the ScVal value to be converted to bytes.
39-
40-
</TabItem>
41-
</Tabs>
27+
</CodeExample>
4228

4329
## ScVal to address
4430

45-
<Tabs groupId="language">
46-
<TabItem value="js" label="JS">
31+
<CodeExample>
4732

4833
```js
49-
const StellarSdk = require("@stellar/stellar-sdk");
50-
51-
const addressFromScVal = StellarSdk.Address.fromScVal(scVal);
52-
addressFromScVal.toString();
34+
// An ScVal Address value
35+
const myScVal = StellarSdk.xdr.ScVal.fromXDR(
36+
"AAAAEgAAAAAAAAAAmds+PBjjrhZAv1aCM4n4EbU6ycAbK5HqxcUsumDFyf4=",
37+
"base64",
38+
);
39+
// Convert the ScVal to an Address
40+
const addressFromScVal = StellarSdk.Address.fromScVal(myScVal);
5341
```
5442

55-
- `StellarSdk.Address.fromScVal()` converts an ScVal value to an address.
56-
- `scVal` is the ScVal value to be converted to an address.
57-
58-
</TabItem>
59-
<TabItem value="python" label="Python">
60-
6143
```python
62-
import stellar_sdk
63-
64-
sc_to_address = Address.from_xdr_sc_address(sc_val)
44+
# Convert the ScVal to address
45+
address_from_sc_val = stellar_sdk.scval.from_address("AAAAEgAAAAAAAAAAmds+PBjjrhZAv1aCM4n4EbU6ycAbK5HqxcUsumDFyf4=")
6546
```
6647

67-
- `stellar_sdk.scval.from_xdr_sc_addres9()` converts an ScVal value to an address.
68-
- `sc_val` represents the ScVal value to be converted to an address.
69-
70-
</TabItem>
71-
</Tabs>
48+
</CodeExample>
7249

7350
## ScVal to String
7451

75-
<Tabs groupId="language">
76-
<TabItem value="js" label="JS">
52+
<CodeExample>
7753

7854
```js
79-
const StellarSdk = require("@stellar/stellar-sdk");
80-
81-
const stringFromScVal = scVal.toString("utf-8");
55+
// An ScVal string value
56+
const myScVal = StellarSdk.xdr.ScVal.fromXDR(
57+
"AAAADgAAAA9IZWxsbywgU3RlbGxhciEA",
58+
"base64",
59+
);
60+
// Convert the ScVal to a string
61+
const stringFromScVal = StellarSdk.scValToNative(myScVal);
8262
```
8363

84-
`scVal.toString()` converts an ScVal value to a string. `scVal` is the ScVal value to be converted to a string. `utf-8` is the encoding format for the string.
85-
86-
</TabItem>
87-
<TabItem value="python" label="Python">
88-
8964
```python
90-
91-
import stellar_sdk
92-
93-
# scval to string
94-
sc_val_to_string = stellar_sdk.scval.from_string(sc_val)
65+
# Convert the ScVal to a string
66+
string_from_sc_val = stellar_sdk.scval.from_string("AAAADgAAAA9IZWxsbywgU3RlbGxhciEA").decode()
9567
```
9668

97-
- `stellar_sdk.scval.from_string()` converts an ScVal value to a string.
98-
- `sc_val` represents the ScVal value to be converted to a string.
99-
100-
</TabItem>
101-
</Tabs>
69+
</CodeExample>

0 commit comments

Comments
 (0)