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.
ScVal to bytes
// An ScVal bytes value
const myScVal = StellarSdk.xdr.ScVal.fromXDR("AAAADQAAAARQ/8AB", "base64");
// Convert the ScVal to a Buffer
const bytesFromScVal = StellarSdk.scValToNative(myScVal);
# Convert the ScVal to bytes
bytes_from_sc_val = stellar_sdk.scval.from_bytes("AAAADQAAAARQ/8AB")
// An ScVal bytes value
var myScVal xdr.ScVal
if err := xdr.SafeUnmarshalBase64("AAAADQAAAARQ/8AB", &myScVal); err != nil {
log.Fatal(err)
}
// Convert the ScVal to a byte slice
bytesFromScVal, ok := myScVal.GetBytes()
if !ok {
log.Fatal("not a bytes value")
}
// import org.stellar.sdk.scval.Scv;
// import org.stellar.sdk.xdr.SCVal;
// An ScVal bytes value (fromXdrBase64 throws IOException on invalid XDR)
SCVal myScVal = SCVal.fromXdrBase64("AAAADQAAAARQ/8AB");
// Convert the ScVal to a byte array (throws if it is not a bytes value)
byte[] bytesFromScVal = Scv.fromBytes(myScVal);
ScVal to address
// An ScVal Address value
const myScVal = StellarSdk.xdr.ScVal.fromXDR(
"AAAAEgAAAAAAAAAAmds+PBjjrhZAv1aCM4n4EbU6ycAbK5HqxcUsumDFyf4=",
"base64",
);
// Convert the ScVal to an Address
const addressFromScVal = StellarSdk.Address.fromScVal(myScVal);
# Convert the ScVal to address
address_from_sc_val = stellar_sdk.scval.from_address("AAAAEgAAAAAAAAAAmds+PBjjrhZAv1aCM4n4EbU6ycAbK5HqxcUsumDFyf4=")
// An ScVal Address value
var myScVal xdr.ScVal
err := xdr.SafeUnmarshalBase64("AAAAEgAAAAAAAAAAmds+PBjjrhZAv1aCM4n4EbU6ycAbK5HqxcUsumDFyf4=", &myScVal)
if err != nil {
log.Fatal(err)
}
// Convert the ScVal to an xdr.ScAddress, then to its string form
address, ok := myScVal.GetAddress()
if !ok {
log.Fatal("not an address value")
}
addressFromScVal, err := address.String()
if err != nil {
log.Fatal(err)
}
// import org.stellar.sdk.Address;
// import org.stellar.sdk.xdr.SCVal;
// An ScVal Address value
SCVal myScVal = SCVal.fromXdrBase64("AAAAEgAAAAAAAAAAmds+PBjjrhZAv1aCM4n4EbU6ycAbK5HqxcUsumDFyf4=");
// Convert the ScVal to an Address (throws if it is not an address value)
Address addressFromScVal = Address.fromSCVal(myScVal);
ScVal to String
// An ScVal string value
const myScVal = StellarSdk.xdr.ScVal.fromXDR(
"AAAADgAAAA9IZWxsbywgU3RlbGxhciEA",
"base64",
);
// Convert the ScVal to a string
const stringFromScVal = StellarSdk.scValToNative(myScVal);
# Convert the ScVal to a string
string_from_sc_val = stellar_sdk.scval.from_string("AAAADgAAAA9IZWxsbywgU3RlbGxhciEA").decode()
// An ScVal string value
var myScVal xdr.ScVal
if err := xdr.SafeUnmarshalBase64("AAAADgAAAA9IZWxsbywgU3RlbGxhciEA", &myScVal); err != nil {
log.Fatal(err)
}
// Convert the ScVal to a string
scString, ok := myScVal.GetStr()
if !ok {
log.Fatal("not a string value")
}
stringFromScVal := string(scString)
// import java.nio.charset.StandardCharsets;
// import org.stellar.sdk.scval.Scv;
// import org.stellar.sdk.xdr.SCVal;
// An ScVal string value
SCVal myScVal = SCVal.fromXdrBase64("AAAADgAAAA9IZWxsbywgU3RlbGxhciEA");
// Convert the ScVal to a string (Scv.fromString returns the raw bytes)
String stringFromScVal = new String(Scv.fromString(myScVal), StandardCharsets.UTF_8);