stellar / js-soroban-client

Main Soroban client library for the Javascript language
https://stellar.github.io/js-soroban-client/
Apache License 2.0
22 stars 17 forks source link

scValToNative not working #148

Closed quankori closed 1 year ago

quankori commented 1 year ago

Currently, I am trying to decode scVal to its native format, but it is not working. I followed these steps:

  1. I have a value scVal encoded as AAAADwAAAAdkZXBvc2l0AA== of type Sym.
  2. I attempted to change it to type SorobanClient.xdr.ScVal using the following code:
const svcValue = SorobanClient.xdr.ScVal.scvSymbol(
      "AAAADwAAAAdkZXBvc2l0AA==",
);

The result is as follows:

ChildUnion {
  _switch: ChildEnum { name: 'scvString', value: 14 },
  _arm: 'str',
  _armType: String { _maxLength: 4294967295 },
  _value: 'AAAADwAAAAdkZXBvc2l0AA=='
}
  1. However, when I use it with scValToNative, it returns the same old variable:
const scValDecode = await SorobanClient.scValToNative(svcValue);
console.log(scValDecode);

The result is AAAADwAAAAdkZXBvc2l0AA==. I attempted to decode it on website laboratory stellar, and the result was different: ZGVwb3NpdA==

Shaptic commented 1 year ago

that's because the scvValue itself is not correct: you need to specify the decoding

const svcValue = SorobanClient.xdr.ScVal.fromXDR(
    "AAAADwAAAAdkZXBvc2l0AA==",
    "base64"
);

then it should be correct