|
2 | 2 |
|
3 | 3 | from typing import TYPE_CHECKING |
4 | 4 |
|
5 | | -from komet.scval import ( |
6 | | - SCI32, |
7 | | - SCI64, |
8 | | - SCI128, |
9 | | - SCI256, |
10 | | - SCU32, |
11 | | - SCU64, |
12 | | - SCU128, |
13 | | - SCU256, |
14 | | - AccountId, |
15 | | - ContractId, |
16 | | - SCAddress, |
17 | | - SCBool, |
18 | | - SCBytes, |
19 | | - SCMap, |
20 | | - SCSymbol, |
21 | | - SCVec, |
22 | | -) |
23 | 5 | from stellar_sdk import xdr as stellar_xdr |
24 | 6 | from stellar_sdk.xdr.sc_address_type import SCAddressType |
25 | 7 | from stellar_sdk.xdr.sc_val_type import SCValType |
26 | 8 |
|
27 | 9 | _UINT64_MASK = (1 << 64) - 1 |
28 | 10 |
|
29 | 11 | if TYPE_CHECKING: |
30 | | - from komet.scval import SCValue |
31 | | - from stellar_sdk.xdr.sc_address import SCAddress as XDRSCAddress |
32 | 12 | from stellar_sdk.xdr.sc_val import SCVal |
33 | 13 |
|
34 | 14 |
|
@@ -176,106 +156,3 @@ def _map_entry(pair: object) -> tuple[dict, dict]: |
176 | 156 | if isinstance(pair, (list, tuple)) and len(pair) == 2: |
177 | 157 | return pair[0], pair[1] |
178 | 158 | raise NotImplementedError(f'Unsupported SCMap entry in JSON encoding: {pair!r}') |
179 | | - |
180 | | - |
181 | | -def sc_address_from_xdr(xdr: XDRSCAddress) -> SCAddress: |
182 | | - """Convert an XDR SCAddress to a Komet SCAddress.""" |
183 | | - match xdr.type: |
184 | | - case SCAddressType.SC_ADDRESS_TYPE_ACCOUNT: |
185 | | - assert xdr.account_id is not None |
186 | | - # The account_id is a PublicKey XDR — extract the raw 32-byte ed25519 key |
187 | | - assert xdr.account_id.account_id.ed25519 is not None |
188 | | - raw_bytes = xdr.account_id.account_id.ed25519.uint256 |
189 | | - return SCAddress(AccountId(raw_bytes)) |
190 | | - case SCAddressType.SC_ADDRESS_TYPE_CONTRACT: |
191 | | - assert xdr.contract_id is not None |
192 | | - return SCAddress(ContractId(xdr.contract_id.contract_id.hash)) |
193 | | - case _: |
194 | | - raise NotImplementedError(f'Unsupported SCAddress type: {xdr.type}') |
195 | | - |
196 | | - |
197 | | -def scvalue_from_xdr(xdr: SCVal) -> SCValue: |
198 | | - """ |
199 | | - Convert a Stellar XDR SCVal to a Komet SCValue. |
200 | | -
|
201 | | - The XDR SCVal is a large union type — each case maps directly to one of |
202 | | - the Komet SCValue dataclasses. Unsupported types (void, error, timepoint, |
203 | | - duration, contract instance, ledger keys) raise NotImplementedError. |
204 | | - """ |
205 | | - match xdr.type: |
206 | | - |
207 | | - case SCValType.SCV_BOOL: |
208 | | - assert xdr.b is not None |
209 | | - return SCBool(xdr.b) |
210 | | - |
211 | | - case SCValType.SCV_I32: |
212 | | - assert xdr.i32 is not None |
213 | | - return SCI32(xdr.i32.int32) |
214 | | - |
215 | | - case SCValType.SCV_I64: |
216 | | - assert xdr.i64 is not None |
217 | | - return SCI64(xdr.i64.int64) |
218 | | - |
219 | | - case SCValType.SCV_I128: |
220 | | - assert xdr.i128 is not None |
221 | | - # i128 is stored as (hi: int64, lo: uint64) parts |
222 | | - val = (xdr.i128.hi.int64 << 64) | xdr.i128.lo.uint64 |
223 | | - return SCI128(val) |
224 | | - |
225 | | - case SCValType.SCV_I256: |
226 | | - assert xdr.i256 is not None |
227 | | - # i256 is stored as (hi_hi, hi_lo, lo_hi, lo_lo) parts |
228 | | - val = ( |
229 | | - (xdr.i256.hi_hi.int64 << 192) |
230 | | - | (xdr.i256.hi_lo.uint64 << 128) |
231 | | - | (xdr.i256.lo_hi.uint64 << 64) |
232 | | - | xdr.i256.lo_lo.uint64 |
233 | | - ) |
234 | | - return SCI256(val) |
235 | | - |
236 | | - case SCValType.SCV_U32: |
237 | | - assert xdr.u32 is not None |
238 | | - return SCU32(xdr.u32.uint32) |
239 | | - |
240 | | - case SCValType.SCV_U64: |
241 | | - assert xdr.u64 is not None |
242 | | - return SCU64(xdr.u64.uint64) |
243 | | - |
244 | | - case SCValType.SCV_U128: |
245 | | - assert xdr.u128 is not None |
246 | | - val = (xdr.u128.hi.uint64 << 64) | xdr.u128.lo.uint64 |
247 | | - return SCU128(val) |
248 | | - |
249 | | - case SCValType.SCV_U256: |
250 | | - assert xdr.u256 is not None |
251 | | - val = ( |
252 | | - (xdr.u256.hi_hi.uint64 << 192) |
253 | | - | (xdr.u256.hi_lo.uint64 << 128) |
254 | | - | (xdr.u256.lo_hi.uint64 << 64) |
255 | | - | xdr.u256.lo_lo.uint64 |
256 | | - ) |
257 | | - return SCU256(val) |
258 | | - |
259 | | - case SCValType.SCV_SYMBOL: |
260 | | - assert xdr.sym is not None |
261 | | - return SCSymbol(xdr.sym.sc_symbol.decode()) |
262 | | - |
263 | | - case SCValType.SCV_BYTES: |
264 | | - assert xdr.bytes is not None |
265 | | - return SCBytes(xdr.bytes.sc_bytes) |
266 | | - |
267 | | - case SCValType.SCV_ADDRESS: |
268 | | - assert xdr.address is not None |
269 | | - return sc_address_from_xdr(xdr.address) |
270 | | - |
271 | | - case SCValType.SCV_VEC: |
272 | | - assert xdr.vec is not None |
273 | | - # komet annotates SCVec.val as tuple[SCValue] instead of tuple[SCValue, ...] |
274 | | - return SCVec(tuple(scvalue_from_xdr(v) for v in xdr.vec.sc_vec)) # type: ignore[arg-type] |
275 | | - |
276 | | - case SCValType.SCV_MAP: |
277 | | - assert xdr.map is not None |
278 | | - return SCMap({scvalue_from_xdr(entry.key): scvalue_from_xdr(entry.val) for entry in xdr.map.sc_map}) |
279 | | - |
280 | | - case _: |
281 | | - raise NotImplementedError(f'Unsupported SCVal type: {xdr.type}') |
0 commit comments