java-stellar-sdk icon indicating copy to clipboard operation
java-stellar-sdk copied to clipboard

Feature Request: Provide a simple way for decoding the public key as simple String.

Open VicMikhailau opened this issue 3 years ago • 5 comments

Hi, Stellar SDK team

What problem does your feature solve?

Provide a simple way for decoding the public key as simple String

What would you like to see?

StrKey.class is closed. Maybe makes sense to make it opened?

What alternatives are there?

I don't see any other solutions. E.g. I have 'Set Option Operation' with the Signer object. I have used KeyPair.fromXdrSignerKey(operation.signer).accountId for retrieve String representation of Public Key. But in some cases, it returns 'Invalid Public Key' exception -> 'not a valid GroupElement'. Good. But I just need to try to decode the byte array in this case. Can you please suggest any other solution to retrieve a simple String representation of Public Key e.g. from the byte array?

On IOS SDK it retrieved like PublicKey(xdr: x).accountId

On Backend just used:

key = StrKey.decode_ed25519_public_key(public_key)
return Keypair.from_raw_ed25519_public_key(key)

Summary: It would be nice if the SDK had simple methods available for decoding, for example, an array of bytes (e.g for ed25519.uint256). As in the StrKey.class, which is closed.

Thanks.

VicMikhailau avatar Jan 22 '21 09:01 VicMikhailau

@VicMikhailau StrKey.encodeStellarAccountId() will convert an AccountID instance into a string. If that function does not help you can you please provide a specific example with a sample public key

tamirms avatar Jan 22 '21 10:01 tamirms

@VicMikhailau StrKey.encodeStellarAccountId() will convert an AccountID instance into a string. If that function does not help you can you please provide a specific example with a sample public key

Checked. It's worked fine - I need just to receive a simple String public key (from byte array) representation: Here I passed byte array from operation.signer.ed25519.uint256 -> StrKey.encodeStellarAccountId(byte[] data). like StrKey.encodeStellarAccountId(operation.signer.ed25519.uint256) And it returns the right String representation of data for all cases.

Inner method of the KeyPair class use this method StrKey.encodeStellarAccountId(byte[] data) like:

KeyPair -> public String getAccountId() {
        return StrKey.encodeStellarAccountId(this.mPublicKey.getAbyte());
    }

E.g. on Backend just used:

key = StrKey.decode_ed25519_public_key(public_key)
return Keypair.from_raw_ed25519_public_key(key)

But for Java Stellar SDK StrKey.class is closed. I don't see other methods for decode the byte array as a simple string.

VicMikhailau avatar Jan 22 '21 11:01 VicMikhailau

@tamirms Updated issue description. Summary: It would be nice if the SDK had simple methods available for decoding, for example, an array of bytes (e.g for ed25519.uint256). As in the StrKey.class, which is closed. For cases when the Public Key can be not valid for some reason and must be just decoded as String.

Thanks.

VicMikhailau avatar Jan 22 '21 17:01 VicMikhailau

Should we provide some utils for converting addresses, values, and other information in XDR? A few of functions are currently available in the Python SDK, I may add more in the future depending on user demand.

overcat avatar Feb 18 '21 07:02 overcat

@overcat Why not? Sounds good. Additional methods and etc can be always useful. Thanks.

VicMikhailau avatar Feb 18 '21 08:02 VicMikhailau

Please do the StrKey.class public. It can help avoiding the StrKey.class duplication on the app side.

Use case:

org.stellar.sdk.xdr.SC Address Type.SC_ADDRESS_TYPE_ACCOUNT ->
StrKey.encodeStellarAccountId(address.accountId.accountID.ed25519.uint256)

let-it-snow avatar Sep 29 '23 15:09 let-it-snow