@yellow-org/sdk 1.0.1-alpha.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (72) hide show
  1. package/README.md +999 -0
  2. package/dist/abis/generated.d.ts +4300 -0
  3. package/dist/abis/generated.js +1 -0
  4. package/dist/app/index.d.ts +2 -0
  5. package/dist/app/index.js +2 -0
  6. package/dist/app/packing.d.ts +7 -0
  7. package/dist/app/packing.js +111 -0
  8. package/dist/app/types.d.ts +91 -0
  9. package/dist/app/types.js +42 -0
  10. package/dist/asset_store.d.ts +13 -0
  11. package/dist/asset_store.js +121 -0
  12. package/dist/blockchain/evm/channel_hub_abi.d.ts +4284 -0
  13. package/dist/blockchain/evm/channel_hub_abi.js +5475 -0
  14. package/dist/blockchain/evm/client.d.ts +46 -0
  15. package/dist/blockchain/evm/client.js +428 -0
  16. package/dist/blockchain/evm/erc20.d.ts +13 -0
  17. package/dist/blockchain/evm/erc20.js +54 -0
  18. package/dist/blockchain/evm/erc20_abi.d.ts +144 -0
  19. package/dist/blockchain/evm/erc20_abi.js +96 -0
  20. package/dist/blockchain/evm/index.d.ts +6 -0
  21. package/dist/blockchain/evm/index.js +6 -0
  22. package/dist/blockchain/evm/interface.d.ts +10 -0
  23. package/dist/blockchain/evm/interface.js +1 -0
  24. package/dist/blockchain/evm/types.d.ts +27 -0
  25. package/dist/blockchain/evm/types.js +1 -0
  26. package/dist/blockchain/evm/utils.d.ts +9 -0
  27. package/dist/blockchain/evm/utils.js +129 -0
  28. package/dist/blockchain/index.d.ts +1 -0
  29. package/dist/blockchain/index.js +1 -0
  30. package/dist/client.d.ts +99 -0
  31. package/dist/client.js +720 -0
  32. package/dist/config.d.ts +12 -0
  33. package/dist/config.js +28 -0
  34. package/dist/core/event.d.ts +29 -0
  35. package/dist/core/event.js +1 -0
  36. package/dist/core/index.d.ts +6 -0
  37. package/dist/core/index.js +6 -0
  38. package/dist/core/interface.d.ts +53 -0
  39. package/dist/core/interface.js +1 -0
  40. package/dist/core/state.d.ts +21 -0
  41. package/dist/core/state.js +267 -0
  42. package/dist/core/state_packer.d.ts +13 -0
  43. package/dist/core/state_packer.js +98 -0
  44. package/dist/core/types.d.ts +203 -0
  45. package/dist/core/types.js +220 -0
  46. package/dist/core/utils.d.ts +16 -0
  47. package/dist/core/utils.js +181 -0
  48. package/dist/index.d.ts +9 -0
  49. package/dist/index.js +9 -0
  50. package/dist/rpc/api.d.ts +181 -0
  51. package/dist/rpc/api.js +1 -0
  52. package/dist/rpc/client.d.ts +36 -0
  53. package/dist/rpc/client.js +102 -0
  54. package/dist/rpc/dialer.d.ts +30 -0
  55. package/dist/rpc/dialer.js +146 -0
  56. package/dist/rpc/error.d.ts +18 -0
  57. package/dist/rpc/error.js +27 -0
  58. package/dist/rpc/index.d.ts +7 -0
  59. package/dist/rpc/index.js +7 -0
  60. package/dist/rpc/message.d.ts +25 -0
  61. package/dist/rpc/message.js +102 -0
  62. package/dist/rpc/methods.d.ts +30 -0
  63. package/dist/rpc/methods.js +27 -0
  64. package/dist/rpc/types.d.ts +101 -0
  65. package/dist/rpc/types.js +1 -0
  66. package/dist/signers.d.ts +48 -0
  67. package/dist/signers.js +96 -0
  68. package/dist/utils/sign.d.ts +2 -0
  69. package/dist/utils/sign.js +8 -0
  70. package/dist/utils.d.ts +42 -0
  71. package/dist/utils.js +226 -0
  72. package/package.json +89 -0
@@ -0,0 +1,102 @@
1
+ import { newErrorPayload, ERROR_PARAM_KEY } from './error';
2
+ export var MsgType;
3
+ (function (MsgType) {
4
+ MsgType[MsgType["Req"] = 1] = "Req";
5
+ MsgType[MsgType["Resp"] = 2] = "Resp";
6
+ MsgType[MsgType["Event"] = 3] = "Event";
7
+ MsgType[MsgType["RespErr"] = 4] = "RespErr";
8
+ })(MsgType || (MsgType = {}));
9
+ function bigIntReplacer(key, value) {
10
+ if (typeof value === 'bigint') {
11
+ return value.toString();
12
+ }
13
+ if (key === 'blockchain_id' || key === 'epoch' || key === 'version' || key === 'nonce') {
14
+ if (typeof value === 'number' || typeof value === 'bigint') {
15
+ return value.toString();
16
+ }
17
+ }
18
+ return value;
19
+ }
20
+ export function newPayload(v) {
21
+ if (v === null || v === undefined) {
22
+ return {};
23
+ }
24
+ return JSON.parse(JSON.stringify(v, bigIntReplacer));
25
+ }
26
+ export function translatePayload(payload) {
27
+ return payload;
28
+ }
29
+ export function payloadError(payload) {
30
+ const errorMsg = payload[ERROR_PARAM_KEY];
31
+ if (typeof errorMsg === 'string') {
32
+ return new Error(errorMsg);
33
+ }
34
+ return null;
35
+ }
36
+ export function messageError(message) {
37
+ if (message.type !== MsgType.RespErr) {
38
+ return null;
39
+ }
40
+ return payloadError(message.payload);
41
+ }
42
+ export function newMessage(type, requestId, method, payload = {}) {
43
+ return {
44
+ type,
45
+ requestId,
46
+ method,
47
+ payload,
48
+ timestamp: Date.now(),
49
+ };
50
+ }
51
+ export function newRequest(requestId, method, payload = {}) {
52
+ return newMessage(MsgType.Req, requestId, method, payload);
53
+ }
54
+ export function newResponse(requestId, method, payload = {}) {
55
+ return newMessage(MsgType.Resp, requestId, method, payload);
56
+ }
57
+ export function newEvent(requestId, method, payload = {}) {
58
+ return newMessage(MsgType.Event, requestId, method, payload);
59
+ }
60
+ export function newErrorResponse(requestId, method, errMsg) {
61
+ const errPayload = newErrorPayload(errMsg);
62
+ return newMessage(MsgType.RespErr, requestId, method, errPayload);
63
+ }
64
+ export function marshalMessage(message) {
65
+ const arr = [
66
+ message.type,
67
+ message.requestId,
68
+ message.method,
69
+ message.payload,
70
+ message.timestamp,
71
+ ];
72
+ return JSON.stringify(arr, bigIntReplacer);
73
+ }
74
+ export function unmarshalMessage(data) {
75
+ const arr = JSON.parse(data);
76
+ if (!Array.isArray(arr) || arr.length !== 5) {
77
+ throw new Error('invalid RPCData: expected 5 elements in array');
78
+ }
79
+ const [type, requestId, method, payload, timestamp] = arr;
80
+ if (typeof type !== 'number') {
81
+ throw new Error('invalid type: expected number');
82
+ }
83
+ if (typeof requestId !== 'number') {
84
+ throw new Error('invalid requestId: expected number');
85
+ }
86
+ if (typeof method !== 'string') {
87
+ throw new Error('invalid method: expected string');
88
+ }
89
+ if (typeof payload !== 'object' || payload === null) {
90
+ throw new Error('invalid payload: expected object');
91
+ }
92
+ if (typeof timestamp !== 'number') {
93
+ throw new Error('invalid timestamp: expected number');
94
+ }
95
+ return {
96
+ type: type,
97
+ requestId,
98
+ method,
99
+ payload: payload,
100
+ timestamp,
101
+ };
102
+ }
@@ -0,0 +1,30 @@
1
+ export type Group = string;
2
+ export type Method = string;
3
+ export declare const ChannelV1Group: Group;
4
+ export declare const ChannelsV1GetHomeChannelMethod: Method;
5
+ export declare const ChannelsV1GetEscrowChannelMethod: Method;
6
+ export declare const ChannelsV1GetChannelsMethod: Method;
7
+ export declare const ChannelsV1GetLatestStateMethod: Method;
8
+ export declare const ChannelsV1GetStatesMethod: Method;
9
+ export declare const ChannelsV1RequestCreationMethod: Method;
10
+ export declare const ChannelsV1SubmitStateMethod: Method;
11
+ export declare const ChannelsV1SubmitSessionKeyStateMethod: Method;
12
+ export declare const ChannelsV1GetLastKeyStatesMethod: Method;
13
+ export declare const AppSessionsV1Group: Group;
14
+ export declare const AppSessionsV1SubmitDepositStateMethod: Method;
15
+ export declare const AppSessionsV1SubmitAppStateMethod: Method;
16
+ export declare const AppSessionsV1RebalanceAppSessionsMethod: Method;
17
+ export declare const AppSessionsV1GetAppDefinitionMethod: Method;
18
+ export declare const AppSessionsV1GetAppSessionsMethod: Method;
19
+ export declare const AppSessionsV1CreateAppSessionMethod: Method;
20
+ export declare const AppSessionsV1CloseAppSessionMethod: Method;
21
+ export declare const AppSessionsV1SubmitSessionKeyStateMethod: Method;
22
+ export declare const AppSessionsV1GetLastKeyStatesMethod: Method;
23
+ export declare const UserV1Group: Group;
24
+ export declare const UserV1GetBalancesMethod: Method;
25
+ export declare const UserV1GetTransactionsMethod: Method;
26
+ export declare const NodeV1Group: Group;
27
+ export declare const NodeV1PingMethod: Method;
28
+ export declare const NodeV1GetConfigMethod: Method;
29
+ export declare const NodeV1GetAssetsMethod: Method;
30
+ export type Event = string;
@@ -0,0 +1,27 @@
1
+ export const ChannelV1Group = 'channels.v1';
2
+ export const ChannelsV1GetHomeChannelMethod = 'channels.v1.get_home_channel';
3
+ export const ChannelsV1GetEscrowChannelMethod = 'channels.v1.get_escrow_channel';
4
+ export const ChannelsV1GetChannelsMethod = 'channels.v1.get_channels';
5
+ export const ChannelsV1GetLatestStateMethod = 'channels.v1.get_latest_state';
6
+ export const ChannelsV1GetStatesMethod = 'channels.v1.get_states';
7
+ export const ChannelsV1RequestCreationMethod = 'channels.v1.request_creation';
8
+ export const ChannelsV1SubmitStateMethod = 'channels.v1.submit_state';
9
+ export const ChannelsV1SubmitSessionKeyStateMethod = 'channels.v1.submit_session_key_state';
10
+ export const ChannelsV1GetLastKeyStatesMethod = 'channels.v1.get_last_key_states';
11
+ export const AppSessionsV1Group = 'app_sessions.v1';
12
+ export const AppSessionsV1SubmitDepositStateMethod = 'app_sessions.v1.submit_deposit_state';
13
+ export const AppSessionsV1SubmitAppStateMethod = 'app_sessions.v1.submit_app_state';
14
+ export const AppSessionsV1RebalanceAppSessionsMethod = 'app_sessions.v1.rebalance_app_sessions';
15
+ export const AppSessionsV1GetAppDefinitionMethod = 'app_sessions.v1.get_app_definition';
16
+ export const AppSessionsV1GetAppSessionsMethod = 'app_sessions.v1.get_app_sessions';
17
+ export const AppSessionsV1CreateAppSessionMethod = 'app_sessions.v1.create_app_session';
18
+ export const AppSessionsV1CloseAppSessionMethod = 'app_sessions.v1.close_app_session';
19
+ export const AppSessionsV1SubmitSessionKeyStateMethod = 'app_sessions.v1.submit_session_key_state';
20
+ export const AppSessionsV1GetLastKeyStatesMethod = 'app_sessions.v1.get_last_key_states';
21
+ export const UserV1Group = 'user.v1';
22
+ export const UserV1GetBalancesMethod = 'user.v1.get_balances';
23
+ export const UserV1GetTransactionsMethod = 'user.v1.get_transactions';
24
+ export const NodeV1Group = 'node.v1';
25
+ export const NodeV1PingMethod = 'node.v1.ping';
26
+ export const NodeV1GetConfigMethod = 'node.v1.get_config';
27
+ export const NodeV1GetAssetsMethod = 'node.v1.get_assets';
@@ -0,0 +1,101 @@
1
+ import { Address } from 'viem';
2
+ import { TransitionType, TransactionType } from '../core/types';
3
+ export interface ChannelV1 {
4
+ channel_id: string;
5
+ user_wallet: Address;
6
+ asset: string;
7
+ type: string;
8
+ blockchain_id: string;
9
+ token_address: Address;
10
+ challenge_duration: number;
11
+ challenge_expires_at?: string;
12
+ nonce: string;
13
+ approved_sig_validators: string;
14
+ status: string;
15
+ state_version: string;
16
+ }
17
+ export interface ChannelDefinitionV1 {
18
+ nonce: string;
19
+ challenge: number;
20
+ approved_sig_validators: string;
21
+ }
22
+ export interface TransitionV1 {
23
+ type: TransitionType;
24
+ tx_id: string;
25
+ account_id: string;
26
+ amount: string;
27
+ }
28
+ export interface LedgerV1 {
29
+ token_address: Address;
30
+ blockchain_id: string;
31
+ user_balance: string;
32
+ user_net_flow: string;
33
+ node_balance: string;
34
+ node_net_flow: string;
35
+ }
36
+ export interface StateV1 {
37
+ id: string;
38
+ transition: TransitionV1;
39
+ asset: string;
40
+ user_wallet: Address;
41
+ epoch: string;
42
+ version: string;
43
+ home_channel_id?: string;
44
+ escrow_channel_id?: string;
45
+ home_ledger: LedgerV1;
46
+ escrow_ledger?: LedgerV1;
47
+ user_sig?: string;
48
+ node_sig?: string;
49
+ }
50
+ export interface ChannelSessionKeyStateV1 {
51
+ user_address: string;
52
+ session_key: string;
53
+ version: string;
54
+ assets: string[];
55
+ expires_at: string;
56
+ user_sig: string;
57
+ }
58
+ export interface AssetV1 {
59
+ name: string;
60
+ symbol: string;
61
+ decimals: number;
62
+ suggested_blockchain_id: string;
63
+ tokens: TokenV1[];
64
+ }
65
+ export interface TokenV1 {
66
+ name: string;
67
+ symbol: string;
68
+ address: Address;
69
+ blockchain_id: string;
70
+ decimals: number;
71
+ }
72
+ export interface BlockchainInfoV1 {
73
+ name: string;
74
+ blockchain_id: string;
75
+ channel_hub_address: Address;
76
+ }
77
+ export interface BalanceEntryV1 {
78
+ asset: string;
79
+ amount: string;
80
+ }
81
+ export interface TransactionV1 {
82
+ id: string;
83
+ asset: string;
84
+ tx_type: TransactionType;
85
+ from_account: Address;
86
+ to_account: Address;
87
+ sender_new_state_id?: string;
88
+ receiver_new_state_id?: string;
89
+ amount: string;
90
+ created_at: string;
91
+ }
92
+ export interface PaginationParamsV1 {
93
+ offset?: number;
94
+ limit?: number;
95
+ }
96
+ export interface PaginationMetadataV1 {
97
+ page: number;
98
+ per_page: number;
99
+ total_count: number;
100
+ page_count: number;
101
+ }
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,48 @@
1
+ import { Address, Hex } from 'viem';
2
+ import { privateKeyToAccount } from 'viem/accounts';
3
+ export interface StateSigner {
4
+ getAddress(): Address;
5
+ signMessage(hash: Hex): Promise<Hex>;
6
+ }
7
+ export interface TransactionSigner {
8
+ getAddress(): Address;
9
+ sendTransaction(tx: any): Promise<Hex>;
10
+ signMessage(message: {
11
+ raw: Hex;
12
+ }): Promise<Hex>;
13
+ }
14
+ export declare class EthereumMsgSigner implements StateSigner {
15
+ private account;
16
+ constructor(privateKeyOrAccount: Hex | ReturnType<typeof privateKeyToAccount>);
17
+ getAddress(): Address;
18
+ signMessage(hash: Hex): Promise<Hex>;
19
+ }
20
+ export declare class EthereumRawSigner implements TransactionSigner {
21
+ private account;
22
+ constructor(privateKeyOrAccount: Hex | ReturnType<typeof privateKeyToAccount>);
23
+ getAddress(): Address;
24
+ sendTransaction(tx: any): Promise<Hex>;
25
+ signMessage(message: {
26
+ raw: Hex;
27
+ }): Promise<Hex>;
28
+ }
29
+ export declare class ChannelDefaultSigner implements StateSigner {
30
+ private inner;
31
+ constructor(inner: StateSigner);
32
+ getAddress(): Address;
33
+ signMessage(hash: Hex): Promise<Hex>;
34
+ }
35
+ export declare class ChannelSessionKeyStateSigner implements StateSigner {
36
+ private account;
37
+ private walletAddress;
38
+ private metadataHash;
39
+ private authSignature;
40
+ constructor(sessionKeyPrivateKey: Hex, walletAddress: Address, metadataHash: Hex, authSignature: Hex);
41
+ getAddress(): Address;
42
+ getSessionKeyAddress(): Address;
43
+ signMessage(hash: Hex): Promise<Hex>;
44
+ }
45
+ export declare function createSigners(privateKey: Hex): {
46
+ stateSigner: StateSigner;
47
+ txSigner: TransactionSigner;
48
+ };
@@ -0,0 +1,96 @@
1
+ import { encodeAbiParameters } from 'viem';
2
+ import { privateKeyToAccount } from 'viem/accounts';
3
+ export class EthereumMsgSigner {
4
+ constructor(privateKeyOrAccount) {
5
+ if (typeof privateKeyOrAccount === 'string') {
6
+ this.account = privateKeyToAccount(privateKeyOrAccount);
7
+ }
8
+ else {
9
+ this.account = privateKeyOrAccount;
10
+ }
11
+ }
12
+ getAddress() {
13
+ return this.account.address;
14
+ }
15
+ async signMessage(hash) {
16
+ return await this.account.signMessage({
17
+ message: { raw: hash },
18
+ });
19
+ }
20
+ }
21
+ export class EthereumRawSigner {
22
+ constructor(privateKeyOrAccount) {
23
+ if (typeof privateKeyOrAccount === 'string') {
24
+ this.account = privateKeyToAccount(privateKeyOrAccount);
25
+ }
26
+ else {
27
+ this.account = privateKeyOrAccount;
28
+ }
29
+ }
30
+ getAddress() {
31
+ return this.account.address;
32
+ }
33
+ async sendTransaction(tx) {
34
+ throw new Error('sendTransaction requires a wallet client - use the blockchain client instead');
35
+ }
36
+ async signMessage(message) {
37
+ return await this.account.sign({ hash: message.raw });
38
+ }
39
+ }
40
+ export class ChannelDefaultSigner {
41
+ constructor(inner) {
42
+ this.inner = inner;
43
+ }
44
+ getAddress() {
45
+ return this.inner.getAddress();
46
+ }
47
+ async signMessage(hash) {
48
+ const sig = await this.inner.signMessage(hash);
49
+ return `0x00${sig.slice(2)}`;
50
+ }
51
+ }
52
+ export class ChannelSessionKeyStateSigner {
53
+ constructor(sessionKeyPrivateKey, walletAddress, metadataHash, authSignature) {
54
+ this.account = privateKeyToAccount(sessionKeyPrivateKey);
55
+ this.walletAddress = walletAddress;
56
+ this.metadataHash = metadataHash;
57
+ this.authSignature = authSignature;
58
+ }
59
+ getAddress() {
60
+ return this.walletAddress;
61
+ }
62
+ getSessionKeyAddress() {
63
+ return this.account.address;
64
+ }
65
+ async signMessage(hash) {
66
+ const sessionKeySig = await this.account.signMessage({
67
+ message: { raw: hash },
68
+ });
69
+ const encoded = encodeAbiParameters([
70
+ {
71
+ type: 'tuple',
72
+ components: [
73
+ { name: 'sessionKey', type: 'address' },
74
+ { name: 'metadataHash', type: 'bytes32' },
75
+ { name: 'authSignature', type: 'bytes' },
76
+ ],
77
+ },
78
+ { type: 'bytes' },
79
+ ], [
80
+ {
81
+ sessionKey: this.account.address,
82
+ metadataHash: this.metadataHash,
83
+ authSignature: this.authSignature,
84
+ },
85
+ sessionKeySig,
86
+ ]);
87
+ return `0x01${encoded.slice(2)}`;
88
+ }
89
+ }
90
+ export function createSigners(privateKey) {
91
+ const account = privateKeyToAccount(privateKey);
92
+ return {
93
+ stateSigner: new ChannelDefaultSigner(new EthereumMsgSigner(account)),
94
+ txSigner: new EthereumRawSigner(account),
95
+ };
96
+ }
@@ -0,0 +1,2 @@
1
+ import { Hex } from 'viem';
2
+ export declare function signRawECDSAMessage(message: Hex, privateKey: Hex): Promise<Hex>;
@@ -0,0 +1,8 @@
1
+ import { privateKeyToAccount } from 'viem/accounts';
2
+ export async function signRawECDSAMessage(message, privateKey) {
3
+ const account = privateKeyToAccount(privateKey);
4
+ const signature = await account.signMessage({
5
+ message: { raw: message },
6
+ });
7
+ return signature;
8
+ }
@@ -0,0 +1,42 @@
1
+ import * as core from './core/types';
2
+ import * as API from './rpc/api';
3
+ import { AssetV1, BalanceEntryV1, ChannelV1, LedgerV1, TransitionV1, StateV1, TransactionV1, PaginationMetadataV1 } from './rpc/types';
4
+ export declare function generateNonce(): bigint;
5
+ export declare function transformNodeConfig(resp: API.NodeV1GetConfigResponse): core.NodeConfig;
6
+ export declare function transformAssets(assets: AssetV1[]): core.Asset[];
7
+ export declare function transformBalances(balances: BalanceEntryV1[]): core.BalanceEntry[];
8
+ export declare function transformChannel(channel: ChannelV1): core.Channel;
9
+ export declare function transformLedger(ledger: LedgerV1): core.Ledger;
10
+ export declare function transformTransition(transition: TransitionV1): core.Transition;
11
+ export declare function transformState(state: StateV1): core.State;
12
+ export declare function transformTransaction(tx: TransactionV1): core.Transaction;
13
+ export declare function transformPaginationMetadata(metadata: PaginationMetadataV1): core.PaginationMetadata;
14
+ import { AppDefinitionV1, AppStateUpdateV1, SignedAppStateUpdateV1, AppSessionInfoV1 } from './app/types';
15
+ export declare function transformAppDefinitionToRPC(def: AppDefinitionV1): any;
16
+ export declare function transformAppStateUpdateToRPC(update: AppStateUpdateV1): {
17
+ app_session_id: string;
18
+ intent: import("./app").AppStateUpdateIntent;
19
+ version: string;
20
+ allocations: {
21
+ participant: `0x${string}`;
22
+ asset: string;
23
+ amount: string;
24
+ }[];
25
+ session_data: string;
26
+ };
27
+ export declare function transformSignedAppStateUpdateToRPC(signed: SignedAppStateUpdateV1): {
28
+ app_state_update: {
29
+ app_session_id: string;
30
+ intent: import("./app").AppStateUpdateIntent;
31
+ version: string;
32
+ allocations: {
33
+ participant: `0x${string}`;
34
+ asset: string;
35
+ amount: string;
36
+ }[];
37
+ session_data: string;
38
+ };
39
+ quorum_sigs: string[];
40
+ };
41
+ export declare function transformAppSessionInfo(raw: any): AppSessionInfoV1;
42
+ export declare function transformAppDefinitionFromRPC(raw: any): AppDefinitionV1;
package/dist/utils.js ADDED
@@ -0,0 +1,226 @@
1
+ import * as core from './core/types';
2
+ import Decimal from 'decimal.js';
3
+ export function generateNonce() {
4
+ return BigInt(Date.now()) * 1000000n + BigInt(Math.floor(Math.random() * 1000000));
5
+ }
6
+ function decodeSigValidators(raw) {
7
+ if (Array.isArray(raw))
8
+ return raw;
9
+ if (typeof raw === 'string' && raw.length > 0) {
10
+ const binary = atob(raw);
11
+ return Array.from(binary, (c) => c.charCodeAt(0));
12
+ }
13
+ return [];
14
+ }
15
+ export function transformNodeConfig(resp) {
16
+ const blockchains = resp.blockchains.map((info) => ({
17
+ name: info.name,
18
+ id: BigInt(info.blockchain_id),
19
+ channelHubAddress: info.channel_hub_address,
20
+ blockStep: 0n,
21
+ }));
22
+ return {
23
+ nodeAddress: resp.node_address,
24
+ nodeVersion: resp.node_version,
25
+ supportedSigValidators: decodeSigValidators(resp.supported_sig_validators),
26
+ blockchains,
27
+ };
28
+ }
29
+ export function transformAssets(assets) {
30
+ return assets.map((asset) => ({
31
+ name: asset.name,
32
+ symbol: asset.symbol,
33
+ decimals: asset.decimals,
34
+ suggestedBlockchainId: BigInt(asset.suggested_blockchain_id),
35
+ tokens: asset.tokens.map((token) => ({
36
+ name: token.name,
37
+ symbol: token.symbol,
38
+ address: token.address,
39
+ blockchainId: BigInt(token.blockchain_id),
40
+ decimals: token.decimals,
41
+ })),
42
+ }));
43
+ }
44
+ export function transformBalances(balances) {
45
+ return balances.map((balance) => ({
46
+ asset: balance.asset,
47
+ balance: new Decimal(balance.amount),
48
+ }));
49
+ }
50
+ function parseChannelType(type) {
51
+ switch (type.toLowerCase()) {
52
+ case 'home':
53
+ return core.ChannelType.Home;
54
+ case 'escrow':
55
+ return core.ChannelType.Escrow;
56
+ default:
57
+ return core.ChannelType.Home;
58
+ }
59
+ }
60
+ function parseChannelStatus(status) {
61
+ switch (status.toLowerCase()) {
62
+ case 'void':
63
+ return core.ChannelStatus.Void;
64
+ case 'open':
65
+ return core.ChannelStatus.Open;
66
+ case 'challenged':
67
+ return core.ChannelStatus.Challenged;
68
+ case 'closed':
69
+ return core.ChannelStatus.Closed;
70
+ default:
71
+ return core.ChannelStatus.Void;
72
+ }
73
+ }
74
+ export function transformChannel(channel) {
75
+ const result = {
76
+ channelId: channel.channel_id,
77
+ userWallet: channel.user_wallet,
78
+ asset: channel.asset,
79
+ type: parseChannelType(channel.type),
80
+ blockchainId: BigInt(channel.blockchain_id),
81
+ tokenAddress: channel.token_address,
82
+ challengeDuration: channel.challenge_duration,
83
+ nonce: BigInt(channel.nonce),
84
+ approvedSigValidators: channel.approved_sig_validators,
85
+ status: parseChannelStatus(channel.status),
86
+ stateVersion: BigInt(channel.state_version),
87
+ };
88
+ if (channel.challenge_expires_at) {
89
+ result.challengeExpiresAt = new Date(channel.challenge_expires_at);
90
+ }
91
+ return result;
92
+ }
93
+ export function transformLedger(ledger) {
94
+ return {
95
+ tokenAddress: ledger.token_address,
96
+ blockchainId: BigInt(ledger.blockchain_id),
97
+ userBalance: new Decimal(ledger.user_balance),
98
+ userNetFlow: new Decimal(ledger.user_net_flow),
99
+ nodeBalance: new Decimal(ledger.node_balance),
100
+ nodeNetFlow: new Decimal(ledger.node_net_flow),
101
+ };
102
+ }
103
+ export function transformTransition(transition) {
104
+ const result = {
105
+ type: transition.type,
106
+ txId: transition.tx_id,
107
+ amount: new Decimal(transition.amount),
108
+ };
109
+ if (transition.account_id) {
110
+ result.accountId = transition.account_id;
111
+ }
112
+ return result;
113
+ }
114
+ export function transformState(state) {
115
+ const result = {
116
+ id: state.id,
117
+ transition: transformTransition(state.transition),
118
+ asset: state.asset,
119
+ userWallet: state.user_wallet,
120
+ epoch: BigInt(state.epoch),
121
+ version: BigInt(state.version),
122
+ homeLedger: transformLedger(state.home_ledger),
123
+ };
124
+ if (state.home_channel_id) {
125
+ result.homeChannelId = state.home_channel_id;
126
+ }
127
+ if (state.escrow_channel_id) {
128
+ result.escrowChannelId = state.escrow_channel_id;
129
+ }
130
+ if (state.escrow_ledger) {
131
+ result.escrowLedger = transformLedger(state.escrow_ledger);
132
+ }
133
+ if (state.user_sig) {
134
+ result.userSig = state.user_sig;
135
+ }
136
+ if (state.node_sig) {
137
+ result.nodeSig = state.node_sig;
138
+ }
139
+ return result;
140
+ }
141
+ export function transformTransaction(tx) {
142
+ const result = {
143
+ id: tx.id,
144
+ asset: tx.asset,
145
+ txType: tx.tx_type,
146
+ fromAccount: tx.from_account,
147
+ toAccount: tx.to_account,
148
+ amount: new Decimal(tx.amount),
149
+ createdAt: new Date(tx.created_at),
150
+ };
151
+ if (tx.sender_new_state_id) {
152
+ result.senderNewStateId = tx.sender_new_state_id;
153
+ }
154
+ if (tx.receiver_new_state_id) {
155
+ result.receiverNewStateId = tx.receiver_new_state_id;
156
+ }
157
+ return result;
158
+ }
159
+ export function transformPaginationMetadata(metadata) {
160
+ return {
161
+ page: metadata.page,
162
+ perPage: metadata.per_page,
163
+ totalCount: metadata.total_count,
164
+ pageCount: metadata.page_count,
165
+ };
166
+ }
167
+ export function transformAppDefinitionToRPC(def) {
168
+ return {
169
+ application: def.application,
170
+ participants: def.participants.map(p => ({
171
+ wallet_address: p.walletAddress,
172
+ signature_weight: p.signatureWeight,
173
+ })),
174
+ quorum: def.quorum,
175
+ nonce: def.nonce.toString(),
176
+ };
177
+ }
178
+ export function transformAppStateUpdateToRPC(update) {
179
+ return {
180
+ app_session_id: update.appSessionId,
181
+ intent: update.intent,
182
+ version: update.version.toString(),
183
+ allocations: update.allocations.map(a => ({
184
+ participant: a.participant,
185
+ asset: a.asset,
186
+ amount: a.amount.toString(),
187
+ })),
188
+ session_data: update.sessionData,
189
+ };
190
+ }
191
+ export function transformSignedAppStateUpdateToRPC(signed) {
192
+ return {
193
+ app_state_update: transformAppStateUpdateToRPC(signed.appStateUpdate),
194
+ quorum_sigs: signed.quorumSigs,
195
+ };
196
+ }
197
+ export function transformAppSessionInfo(raw) {
198
+ return {
199
+ appSessionId: raw.app_session_id,
200
+ isClosed: raw.status === 'closed',
201
+ participants: (raw.participants || []).map((p) => ({
202
+ walletAddress: p.wallet_address,
203
+ signatureWeight: p.signature_weight,
204
+ })),
205
+ sessionData: raw.session_data || '',
206
+ quorum: raw.quorum,
207
+ version: BigInt(raw.version),
208
+ nonce: BigInt(raw.nonce),
209
+ allocations: (raw.allocations || []).map((a) => ({
210
+ participant: a.participant,
211
+ asset: a.asset,
212
+ amount: new Decimal(a.amount),
213
+ })),
214
+ };
215
+ }
216
+ export function transformAppDefinitionFromRPC(raw) {
217
+ return {
218
+ application: raw.application,
219
+ participants: (raw.participants || []).map((p) => ({
220
+ walletAddress: p.wallet_address,
221
+ signatureWeight: p.signature_weight,
222
+ })),
223
+ quorum: raw.quorum,
224
+ nonce: BigInt(raw.nonce),
225
+ };
226
+ }