2

I am developing a site as college project and I cannot seem to understand how to implement StrKey to get the public key from a transaction.

I can decode the xdr but cannot seem to understand how to retrieve the public key from the xdr. For example Stellar Laboratory can do it no problem but I am unable to do the same.

Current xdr extracted in json form

   {
  "_attributes": {
    "tx": {
      "_attributes": {
        "sourceAccount": {
          "_switch": {
            "name": "publicKeyTypeEd25519",
            "value": 0
          },
          "_arm": "ed25519",
          "_armType": {
            "_length": 32,
            "_padding": 0
          },
          "_value": {
            "type": "Buffer",
            "data": [
              158,
              100,
              0,
              66,
              51,
              202,
              169,
              13,
              252,
              113,
              147,
              154,
              22,
              72,
              191,
              147,
              44,
              51,
              52,
              139,
              186,
              201,
              238,
              207,
              158,
              21,
              238,
              196,
              232,
              12,
              24,
              245
            ]
          }
        },
        "fee": 100,
        "seqNum": {
          "low": 22,
          "high": 14586787,
          "unsigned": true
        },
        "memo": {
          "_switch": {
            "name": "memoNone",
            "value": 0
          },
          "_arm": {},
          "_armType": {}
        },
        "operations": [
          {
            "_attributes": {
              "body": {
                "_switch": {
                  "name": "payment",
                  "value": 1
                },
                "_arm": "paymentOp",
                "_value": {
                  "_attributes": {
                    "destination": {
                      "_switch": {
                        "name": "publicKeyTypeEd25519",
                        "value": 0
                      },
                      "_arm": "ed25519",
                      "_armType": {
                        "_length": 32,
                        "_padding": 0
                      },
                      "_value": {
                        "type": "Buffer",
                        "data": [
                          19,
                          172,
                          96,
                          157,
                          239,
                          173,
                          223,
                          213,
                          79,
                          53,
                          53,
                          74,
                          225,
                          128,
                          93,
                          200,
                          48,
                          132,
                          54,
                          241,
                          50,
                          52,
                          38,
                          8,
                          193,
                          2,
                          69,
                          110,
                          219,
                          39,
                          252,
                          94
                        ]
                      }
                    },
                    "asset": {
                      "_switch": {
                        "name": "assetTypeNative",
                        "value": 0
                      },
                      "_arm": {},
                      "_armType": {}
                    },
                    "amount": {
                      "low": 100000000,
                      "high": 0,
                      "unsigned": false
                    }
                  }
                }
              }
            }
          }
        ],
        "ext": {
          "_switch": 0,
          "_arm": {},
          "_armType": {}
        }
      }
    },
    "signatures": [
      {
        "_attributes": {
          "hint": {
            "type": "Buffer",
            "data": [
              232,
              12,
              24,
              245
            ]
          },
          "signature": {
            "type": "Buffer",
            "data": [
              44,
              124,
              174,
              242,
              133,
              116,
              34,
              80,
              134,
              45,
              23,
              241,
              165,
              87,
              193,
              165,
              157,
              98,
              25,
              65,
              73,
              191,
              148,
              19,
              80,
              30,
              136,
              190,
              113,
              237,
              116,
              79,
              150,
              194,
              58,
              160,
              237,
              188,
              99,
              96,
              140,
              85,
              91,
              24,
              0,
              88,
              245,
              66,
              28,
              26,
              26,
              75,
              243,
              52,
              18,
              6,
              117,
              227,
              92,
              49,
              21,
              60,
              242,
              3
            ]
          }
        }
      }
    ]
  }
}

Edit: I was able to solve this by using

StrKey.encodeEd25519PublicKey()

and going to the locations(in the decoded xdr)

._attributes.tx._attributes.sourceAccount._value and
._attributes.tx._attributes.operations[i]._attributes.body._value._attributes.destination._value

Thank you for your help guys!

3

I'd let the SDK decode the XDR for you, and take it from there.

const tx = new StellarSdk.Transaction(tx_xdr);

From here on you have the source accounts in tx.source and tx.operations[i].source, for 0 < i < tx.operations.length.

Easy peasy!

  • I will implement that once I have the site fully up! Thank you for the suggestion. – KyleMcCann Aug 1 '18 at 23:43
0

I'm not familiar with the JS SDK, but I believe you need to take the array value of sourceAccount/_value/data (or destination/_value/data on the payment operation) and construct the public key from KeyPair.fromRawEd25519Seed(seed)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.