-
Notifications
You must be signed in to change notification settings - Fork 160
Expand file tree
/
Copy pathMuxedAccount.java
More file actions
136 lines (127 loc) · 4.88 KB
/
MuxedAccount.java
File metadata and controls
136 lines (127 loc) · 4.88 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
package org.stellar.sdk;
import java.math.BigInteger;
import lombok.EqualsAndHashCode;
import lombok.Getter;
import lombok.NonNull;
import lombok.ToString;
import org.jetbrains.annotations.Nullable;
import org.stellar.sdk.xdr.CryptoKeyType;
import org.stellar.sdk.xdr.Uint256;
import org.stellar.sdk.xdr.Uint64;
import org.stellar.sdk.xdr.XdrUnsignedHyperInteger;
/**
* Represents a multiplexed account on Stellar's network.
*
* <p>A muxed account is an extension of the regular account that allows multiple entities to share
* the same ed25519 key pair as their account ID while providing a unique identifier for each
* entity.
*
* <p>A muxed account consists of two parts:
*
* <ul>
* <li>The ed25519 account ID, which starts with the letter "G".
* <li>An optional account multiplexing ID, which is a 64-bit unsigned integer.
* </ul>
*
* @see <a
* href="https://developers.stellar.org/docs/build/guides/transactions/pooled-accounts-muxed-accounts-memos#muxed-accounts"
* target="_blank">Muxed Accounts</a>
*/
@Getter
@ToString
@EqualsAndHashCode
public class MuxedAccount {
/** The ed25519 account ID. It starts with the letter "G". */
@NonNull private final String accountId;
/** The optional account multiplexing ID. It is a 64-bit unsigned integer. */
@Nullable private final BigInteger muxedId;
/**
* Creates a new muxed account from the given ed25519 account ID and optional multiplexing ID.
*
* @param accountId The ed25519 account ID. It must be a valid account ID starting with "G".
* @param muxedId The optional account multiplexing ID. It can be null if not set.
* @throws IllegalArgumentException If the provided account ID is invalid.
*/
public MuxedAccount(@NonNull String accountId, @Nullable BigInteger muxedId) {
if (!StrKey.isValidEd25519PublicKey(accountId)) {
throw new IllegalArgumentException("accountId is invalid");
}
this.accountId = accountId;
this.muxedId = muxedId;
}
/**
* Creates a new muxed account from the given muxed account address.
*
* @param address The muxed account address. It can be either a regular account ID (starting with
* "G") or a muxed account address (starting with "M").
* @throws IllegalArgumentException If the provided address is invalid.
*/
public MuxedAccount(@NonNull String address) {
if (StrKey.isValidEd25519PublicKey(address)) {
this.accountId = address;
this.muxedId = null;
} else if (StrKey.isValidMed25519PublicKey(address)) {
byte[] rawMed25519 = StrKey.decodeMed25519PublicKey(address);
StrKey.RawMuxedAccountStrKeyParameter parameter =
StrKey.fromRawMuxedAccountStrKey(rawMed25519);
this.accountId = StrKey.encodeEd25519PublicKey(parameter.getEd25519());
this.muxedId = parameter.getId();
} else {
throw new IllegalArgumentException("Invalid address");
}
}
/**
* Returns the account address representation of this muxed account.
*
* @return The account address. It starts with "M" if the multiplexing ID is set, or with "G" if
* the multiplexing ID is not set.
*/
public String getAddress() {
if (muxedId == null) {
return accountId;
}
return StrKey.encodeMed25519PublicKey(
StrKey.toRawMuxedAccountStrKey(
new StrKey.RawMuxedAccountStrKeyParameter(
StrKey.decodeEd25519PublicKey(accountId), muxedId)));
}
/**
* Creates a new muxed account from the given XDR representation.
*
* @param xdr The XDR representation of the muxed account.
* @return A new muxed account instance.
* @throws IllegalArgumentException If the provided XDR is invalid.
*/
public static MuxedAccount fromXdr(org.stellar.sdk.xdr.MuxedAccount xdr) {
switch (xdr.getDiscriminant()) {
case KEY_TYPE_ED25519:
return new MuxedAccount(StrKey.encodeEd25519PublicKey(xdr.getEd25519().getUint256()), null);
case KEY_TYPE_MUXED_ED25519:
return new MuxedAccount(
StrKey.encodeEd25519PublicKey(xdr.getMed25519().getEd25519().getUint256()),
xdr.getMed25519().getId().getUint64().getNumber());
default:
throw new IllegalArgumentException("Invalid address");
}
}
/**
* Returns the XDR representation of this muxed account.
*
* @return The XDR representation of the muxed account.
*/
public org.stellar.sdk.xdr.MuxedAccount toXdr() {
if (muxedId == null) {
return new org.stellar.sdk.xdr.MuxedAccount(
CryptoKeyType.KEY_TYPE_ED25519,
new Uint256(StrKey.decodeEd25519PublicKey(this.accountId)),
null);
} else {
return new org.stellar.sdk.xdr.MuxedAccount(
CryptoKeyType.KEY_TYPE_MUXED_ED25519,
null,
new org.stellar.sdk.xdr.MuxedAccount.MuxedAccountMed25519(
new Uint64(new XdrUnsignedHyperInteger(this.muxedId)),
new Uint256(StrKey.decodeEd25519PublicKey(this.accountId))));
}
}
}