Skip to content
This repository was archived by the owner on Nov 8, 2023. It is now read-only.

Commit 9a8ec6f

Browse files
authored
Merge branch 'main' into client-l1-txHash
2 parents d12465c + 9f31423 commit 9a8ec6f

9 files changed

+63
-47
lines changed

.env

+12-18
Original file line numberDiff line numberDiff line change
@@ -4,42 +4,36 @@ VITE_ALICE_SK=0x2d999770f7b5d49b694080f987b82bbc9fc9ac2b4dcc10b0f8aba7d700f69c6d
44
VITE_ALICE_ADDRESS=0xAAA6628Ec44A8a742987EF3A114dDFE2D4F7aDCE
55

66
VITE_ALICE_CHAIN_URL=https://rpc.public.zkevm-test.net
7+
VITE_ALICE_ENTRYPOINT_ADDRESS=0x65B786ab8b81286f33953aC751b85E2C08D6CFef
8+
VITE_ALICE_SCW_ADDRESS=0xAC5f6d0130Ad82e0677b5ACF788926F5DDA76ca5
79

810
# BOB
911
VITE_BOB_SK=0x0279651921cd800ac560c21ceea27aab0107b67daf436cdd25ce84cad30159b4
1012
VITE_BOB_ADDRESS=0xBBB676f9cFF8D242e9eaC39D063848807d3D1D94
1113

1214
VITE_BOB_CHAIN_URL=https://sepolia-rpc.scroll.io
13-
15+
VITE_BOB_ENTRYPOINT_ADDRESS=0x30CE8e965155dc56bD061C5FBf8fa3a32775fDAD
16+
VITE_BOB_SCW_ADDRESS=0x705DBe0E96E172e95d56eB689dE3Fc43E29b8236
1417

1518
# Charlie
16-
VITE_CHARLIE_SK=0x827d8121978e93d2e9b01be98fd8bdb08eaa0145184442bb6ab0be17f6ab4248,
17-
VITE_CHARLIE_ADDRESS=0xccc182e9C61ABed84B4df353C8a066EB7a2FeE6f,
19+
VITE_CHARLIE_SK=0x827d8121978e93d2e9b01be98fd8bdb08eaa0145184442bb6ab0be17f6ab4248
20+
VITE_CHARLIE_ADDRESS=0xccc182e9C61ABed84B4df353C8a066EB7a2FeE6f
1821

1922
# Debbie,
20-
VITE_DEBBIE_SK=0x07dbfb9cdfa0dcddcc82f86da46ca63db210eaddeab7410d5dda9a18bbf3eb5f,
21-
VITE_DEBBIE_ADDRESS=0xddd63cb783fF61D683e4BED42a03Aa6b5AF7De2B,
23+
VITE_DEBBIE_SK=0x07dbfb9cdfa0dcddcc82f86da46ca63db210eaddeab7410d5dda9a18bbf3eb5f
24+
VITE_DEBBIE_ADDRESS=0xddd63cb783fF61D683e4BED42a03Aa6b5AF7De2B
2225

2326
# Eve,
24-
VITE_EVE_SK=0x22215c87ad66bed27b715e39c3fd105a6616eb139a4106773b1b67233b7796d4,
25-
VITE_EVE_ADDRESS=0xeeef433cD1EF1714d176202D0Aa7680F20aa7F89,
27+
VITE_EVE_SK=0x22215c87ad66bed27b715e39c3fd105a6616eb139a4106773b1b67233b7796d4
28+
VITE_EVE_ADDRESS=0xeeef433cD1EF1714d176202D0Aa7680F20aa7F89
2629

2730
# Fred,
28-
VITE_FRED_SK=0x1234f96c85acb25dff1f20e03ba287a008c99a02541ba5a9ae8b4392dd12d4ff,
29-
VITE_FRED_ADDRESS=0xfff1b9434a6B6402508911028545Aa9CE080ab12,
30-
31-
31+
VITE_FRED_SK=0x1234f96c85acb25dff1f20e03ba287a008c99a02541ba5a9ae8b4392dd12d4ff
32+
VITE_FRED_ADDRESS=0xfff1b9434a6B6402508911028545Aa9CE080ab12
3233

3334
# IRENE
3435
VITE_IRENE_SK=0xfebb3b74b0b52d0976f6571d555f4ac8b91c308dfa25c7b58d1e6a7c3f50c781
3536
VITE_IRENE_ADDRESS=0x111A00868581f73AB42FEEF67D235Ca09ca1E8db
3637

37-
# If Alice and Bob are on seperate chains they could use seperate entrypoints
38-
VITE_ALICE_ENTRYPOINT_ADDRESS=0x65B786ab8b81286f33953aC751b85E2C08D6CFef
39-
VITE_BOB_ENTRYPOINT_ADDRESS=0x30CE8e965155dc56bD061C5FBf8fa3a32775fDAD
40-
41-
VITE_ALICE_SCW_ADDRESS=0xAC5f6d0130Ad82e0677b5ACF788926F5DDA76ca5
42-
VITE_BOB_SCW_ADDRESS=0x705DBe0E96E172e95d56eB689dE3Fc43E29b8236
43-
4438
VITE_SCW_DEPOSIT=10000000000000000 # 0.01 ETH
4539
VITE_INTERMEDIARY_BALANCE=5000000000000000 # 0.005 ETH

README.md

+44-27
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
<h1 align="center">
2-
<div><img src="./SCBridge-Wallet.png"><br>
2+
<div><img src="./docs/SCBridge-Wallet.png"><br>
33
SCBridge-Wallet
44
</h1>
55
<h3 align="center">
6-
An L2 state channel bridge contract which is also an ERC-4337 compliant smart contract wallet.
6+
ERC-4337 compliant smart contract wallet that enables gasless, instant finality cross-chain payments
77
</h3>
88

99
# Quickstart
@@ -14,7 +14,7 @@ Run each of these commands from the repo root directory:
1414
yarn
1515
yarn chain:a
1616
yarn chain:b
17-
yarn deploy-and-start
17+
yarn deploy-and-start-local
1818
```
1919

2020
These commands do the following:
@@ -40,28 +40,21 @@ By having that Intermediary perform the same role in several users' smart contra
4040

4141
This repo also contains Typescript source code for off-chain clients that can be run by a user and by a Intermediary, to enable to orchestrate the functionality described above.
4242

43-
### Sequence Diagram
43+
### Sequence Diagrams
4444

45-
Scenario: Alice wants to (a) pay Bob offchain and (b) execute a trade on Uniswap. Irene is an Intermediary actor.
45+
Scenario: Alice wants to (a) pay Bob offchain and (b) pay Charley, who does not have a state channel bridge. Irene is an Intermediary actor.
4646

47-
In a typical state channel bridge architecture (green) she must deposit funds into the ID of a "ledger channel" in a Singleton adjudicator contract. Her counterparty in that ledger channel (Irene) must have deposited into another ledger channel with Bob. If Alice wants to execute her Uniswap trade with those deposited funds, she needs to close the ledger channel, withdraw the funds (in one L1 tx), and submit the trade (in another L1 tx).
47+
In a typical state channel bridge architecture she must deposit funds into the ID of a "ledger channel" in a Singleton adjudicator contract. Her counterparty in that ledger channel (Irene) must have deposited into another ledger channel with Bob. If Alice wants to send Charley money using those deposited funds, she needs to close the ledger channel, withdraw the funds (in one L1 tx), and submit the transfer to Charley (in another L1 tx).
4848

49-
With the State Channel Bridge Wallet (SCBridge-Wallet) Architecture, she can instead propose the Uniswap trade to Irene, who checks that it doesn't compromise any HTLC payments, and then countersigns it and submits it to Alice's SCBridge-Wallet via en entrypoint contract. The SCBridge-Wallet validates the countersigned transaction and calls into Uniswap to execute the trade.
50-
51-
In either case, executing a multihop payment from Alice to Bob (white) is as easy as executing the well known HTLC (Hash Timelocked Contract) protocol, which is the same technology that powers the Bitcoin Lightning Network.
52-
53-
![Sequence Diagram](./SCBridge-Wallet-sequence.png)
49+
![Sequence Diagram](./docs/seq-typical-sc.png)
5450

5551
<!-- diagram source, edit at sequencediagram.org
5652
fontawesome f182 Alice
5753
fontawesome f233 Irene
5854
fontawesome f183 Bob
5955
6056
fontawesome f0e3 Adjudicator #red
61-
fontawesome f1c9 SCW-Alice #green
62-
fontawesome f1c9 SCW-Bob #green
63-
fontawesome f1c9 Uniswap
64-
57+
fontawesome f183 Charley
6558
6659
group #lightgreen Typical State Channel Bridge
6760
@@ -71,35 +64,59 @@ Alice-#red>Adjudicator: deposit 10
7164
Bob->Irene: ledger 0/10
7265
Bob<-Irene: ack
7366
Irene-#red>Adjudicator: deposit 10
74-
group L1 transaction flow
67+
group On-chain transaction flow
7568
Alice->Irene: close ledger
7669
Alice<-Irene: ack
77-
Alice-#red>Adjudicator: withdraw
78-
Alice-#red>Uniswap: uniswap trade
70+
Alice-#red>Adjudicator: withdraw 10
71+
Adjudicator--#red>Alice: withdraw 10
72+
Alice-#red>Charley: transfer 10
7973
end
8074
end
75+
-->
76+
77+
With the State Channel Bridge Wallet (SCBridge-Wallet) Architecture, she can instead send Irene the proposed transfer to Charley. Irene checks that this new payment doesn't compromise any HTLC payments, and then countersigns it and submits it to Alice's SCBridge-Wallet via en EntryPoint contract. The SCBridge-Wallet validates the countersigned transaction, then transfers some of the wallet funds to Charley.
78+
79+
![Sequence Diagram](./docs/seq-sc-bridge-wallet.png)
80+
81+
<!-- diagram source, edit at sequencediagram.org
82+
fontawesome f182 Alice
83+
fontawesome f233 Irene
84+
fontawesome f183 Bob
85+
86+
fontawesome f1c9 EntryPoint
87+
fontawesome f1c9 SCW-Alice #green
88+
fontawesome f1c9 SCW-Bob #green
89+
fontawesome f183 Charley
8190
8291
group #ff00ff State Channel Bridge Wallet
8392
Alice-#red>SCW-Alice: transfer 10 (direct from exchange, perhaps)
8493
Bob->Irene: ledger Irene:10
8594
Bob<-Irene: ack
8695
Irene-#red>SCW-Bob: transfer 10
87-
group L1 transaction flow
88-
Alice->Irene: proposed uniswap trade
89-
Irene-#red>SCW-Alice: countersigned uniswap trade
90-
91-
SCW-Alice->Uniswap: uniswap trade
96+
group On-chain transaction flow
97+
Alice->Irene: proposed transfer
98+
Irene-#red>EntryPoint: countersigned transfer
99+
EntryPoint-#red>SCW-Alice: transfer 10
100+
SCW-Alice->Charley: transfer 10
101+
end
92102
end
103+
-->
93104

105+
In either case, executing a multihop payment from Alice to Bob is as easy as executing the well known HTLC (Hash Timelocked Contract) protocol, which is the same technology that powers the Bitcoin Lightning Network.
94106

95-
end
107+
![Sequence Diagram](./docs/seq-off-chain-pay.png)
108+
109+
<!-- diagram source, edit at sequencediagram.org
110+
fontawesome f182 Alice
111+
fontawesome f233 Irene
112+
fontawesome f183 Bob
96113
97-
group Multihop L2 payment from Alice to Bob via Irene
114+
group Off-chain payment from Alice to Bob via Irene
98115
Alice->Bob: request an invoice
99116
Bob->Alice: hash
100117
Alice->Irene: add HTLC(hash,timeout,amount, sig, turnNum)
101118
Irene->Bob: add HTLC
102-
Bob->Irene: preimage + updated signed state
119+
Bob->Irene: preimage + updated signed state
103120
Irene->Alice: preimage + updated signed state
104121
Irene->Bob: countersigned updated state
105122
Alice->Irene: countersigned updated state
@@ -108,7 +125,7 @@ end
108125

109126
### Future work
110127

111-
This repo originated as an entry for the ETHGlobal 2023 hackathon. Therefore some shortcuts were taken to enable an end-to-end demo given the time constraints. The following list identifies future areas for improvement if this foundation was used to build a production-ready application.
128+
This repo originated as an entry for the ETHGlobal 2023 hackathon. Therefore the code takes some shortcuts to enable an end-to-end demo given the time constraints. The following list identifies future areas for improvement if this foundation was used to build a production-ready application.
112129

113130
1. `Messaging`: currently in-browser [BroadcastChannels](https://nodejs.org/api/worker_threads.html#class-broadcastchannel-extends-eventtarget) are used to communicate between the different users. A proper messaging service should allow users to exchange messages when they are on different machines with different IP addresses. Possible technologies include: WebSockets, WebRTC, HTTP requests, etc.
114131
2. `Key management`: currently the private signing keys used by each user to sign blockchain txs and state channel messages are stored in a static [.env](./.env) file. These keys are read by the browser code and inserted in the the JavaScript running on the page. Since these keys control the movement of funds, they should be stored more securely, i.e. in Metamask or another crypto wallet.

clients/OwnerClient.ts

+5
Original file line numberDiff line numberDiff line change
@@ -175,6 +175,11 @@ export class OwnerClient extends StateChannelWallet {
175175

176176
// Create L1 payment UserOperation and forward to intermediary
177177
async payL1(payee: string, amount: bigint): Promise<string> {
178+
const balance = await this.getOwnerBalance();
179+
if (amount > balance) {
180+
throw new Error("Requested payment amount exceeds owner balance");
181+
}
182+
178183
// Only need to encode 'to' and 'amount' fields (i.e. no 'data') for basic eth transfer
179184
const callData = IAccount.encodeFunctionData("execute", [
180185
payee,
File renamed without changes.
File renamed without changes.

docs/seq-off-chain-pay.png

62.1 KB
Loading

docs/seq-sc-bridge-wallet.png

96.2 KB
Loading

docs/seq-typical-sc.png

83.7 KB
Loading

src/Wallet.tsx

+2-2
Original file line numberDiff line numberDiff line change
@@ -265,7 +265,7 @@ const Wallet: React.FunctionComponent<{ role: Role }> = (props: {
265265
) : (
266266
<AccessTimeIcon style={{ marginRight: "5px" }} />
267267
)}
268-
L1 Pay
268+
Chain Pay
269269
</Button>
270270
<Button
271271
size="medium"
@@ -280,7 +280,7 @@ const Wallet: React.FunctionComponent<{ role: Role }> = (props: {
280280
});
281281
}}
282282
>
283-
<BoltIcon /> L2 Pay
283+
<BoltIcon /> Bridge Pay
284284
</Button>
285285
</ButtonGroup>
286286
</Stack>

0 commit comments

Comments
 (0)