Skip to content

Commit 9e1ee1f

Browse files
committed
Pass through G-addresses when extracting
1 parent d51c286 commit 9e1ee1f

File tree

2 files changed

+9
-4
lines changed

2 files changed

+9
-4
lines changed

src/util/decode_encode_muxed_account.js

+8-4
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@ import { StrKey } from '../strkey';
77
* Converts a Stellar address (in G... or M... form) to an `xdr.MuxedAccount`
88
* structure, using the ed25519 representation when possible.
99
*
10-
* This supports full muxed accounts by default, where an M... address will
11-
* resolve to both its underlying G... address and an ID.
10+
* This supports full muxed accounts, where an `M...` address will resolve to
11+
* both its underlying `G...` address and an integer ID.
1212
*
1313
* @param {string} address G... or M... address to encode into XDR
1414
* @returns {xdr.MuxedAccount} a muxed account object for this address string
@@ -72,12 +72,16 @@ export function encodeMuxedAccount(address, id) {
7272

7373
/**
7474
* Extracts the underlying base (G...) address from an M-address.
75-
* @param {string} address a muxed account address (M...)
75+
* @param {string} address an account address (either M... or G...)
7676
* @return {string} a Stellar public key address (G...)
7777
*/
7878
export function extractBaseAddress(address) {
79+
if (StrKey.isValidEd25519PublicKey(address)) {
80+
return address;
81+
}
82+
7983
if (!StrKey.isValidMed25519PublicKey(address)) {
80-
throw new TypeError('address should be a muxed account (M...)');
84+
throw new TypeError(`expected muxed account (M...), got ${address}`);
8185
}
8286

8387
const muxedAccount = decodeAddressToMuxedAccount(address);

test/unit/strkey_test.js

+1
Original file line numberDiff line numberDiff line change
@@ -300,6 +300,7 @@ describe('StrKey', function() {
300300

301301
it('decodes underlying G... address correctly', function() {
302302
expect(StellarBase.extractBaseAddress(MPUBKEY)).to.equal(PUBKEY);
303+
expect(StellarBase.extractBaseAddress(PUBKEY)).to.equal(PUBKEY);
303304
});
304305

305306
const RAW_PUBKEY = StellarBase.StrKey.decodeEd25519PublicKey(PUBKEY);

0 commit comments

Comments
 (0)