-
Notifications
You must be signed in to change notification settings - Fork 0
/
primitives.ts
102 lines (84 loc) · 2.5 KB
/
primitives.ts
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
export enum ChainSupported {
Ethereum = "Ethereum",
Polkadot = "Polkadot"
}
// For status that contains data
interface TxStatusData {
FailedToSubmitTxn: { reason: string };
TxSubmissionPassed: { hash: Uint8Array };
}
export type TxStatus =
| { type: "Genesis" }
| { type: "ReceiverNotRegistered" }
| { type: "RecvAddrConfirmed" }
| { type: "RecvAddrConfirmationPassed" }
| { type: "NetConfirmed" }
| { type: "SenderConfirmed" }
| { type: "SenderConfirmationfailed" }
| { type: "RecvAddrFailed" }
| { type: "FailedToSubmitTxn", data: TxStatusData["FailedToSubmitTxn"] }
| { type: "TxSubmissionPassed", data: TxStatusData["TxSubmissionPassed"] }
export interface TxStateMachine {
senderAddress: string;
receiverAddress: string;
multiId: string; // H256 as hex string
recvSignature?: Uint8Array;
network: ChainSupported;
status: TxStatus;
amount: bigint; // For u128
signedCallPayload?: Uint8Array;
callPayload?: Uint8Array; // Fixed 32 bytes
inboundReqId?: number; // u64
outboundReqId?: number; // u64
txNonce: number
}
export class TxStateMachineManager {
private tx: TxStateMachine;
constructor(tx: TxStateMachine) {
this.tx = tx;
}
setReceiverSignature(signature: Uint8Array): void {
this.tx.recvSignature = signature;
}
setCallPayload(payload: Uint8Array): void {
this.tx.callPayload = payload;
}
setSignedCallPayload(payload: Uint8Array): void {
this.tx.signedCallPayload = payload;
}
updateStatus(status: TxStatus): void {
this.tx.status = status;
}
setRequestIds(inbound?: number, outbound?: number): void {
if (inbound) this.tx.inboundReqId = inbound;
if (outbound) this.tx.outboundReqId = outbound;
}
// Utility methods
isSignedByReceiver(): boolean {
return !!this.tx.recvSignature;
}
hasCallPayload(): boolean {
return !!this.tx.callPayload;
}
// Getters
getTx(): TxStateMachine {
return {...this.tx};
}
// Create new instance
static create(
senderAddress: string,
receiverAddress: string,
network: ChainSupported,
amount: bigint
): TxStateMachineManager {
return new TxStateMachineManager({
senderAddress,
receiverAddress,
multiId: "", // Generate hash of sender+receiver
network,
status: {type: "Genesis"},
amount,
txNonce: 0
});
}
}