-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcontract.scala
259 lines (241 loc) · 9.76 KB
/
contract.scala
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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
package adastream
import scalus.*
import scalus.builtin.Builtins
import scalus.builtin.Builtins.*
import scalus.builtin.ByteString
import scalus.builtin.Data
import scalus.builtin.Data.FromData
import scalus.builtin.Data.ToData
import scalus.builtin.FromData
import scalus.builtin.List
import scalus.builtin.ToData
import scalus.builtin.ToDataInstances.given
import scalus.builtin.given
import scalus.ledger.api.v1.FromDataInstances.given
import scalus.ledger.api.v3.*
import scalus.prelude.?
import scalus.prelude.Maybe
import scalus.prelude.Prelude.log
import scalus.utils.Utils
extension (a: Array[Byte]) def toHex: String = Utils.bytesToHex(a)
/** Bond contract
*
* - Alice wants to buy a file from Bob.
* - Bob encrypts the file with a random key (`password`) and sends encrypted file to Alice.
* - Bob creates a bond contract on Cardano with a collateral and a commitment to the `password`
* and the file.
* - Alice pays Bob for the file via a HTLC (Hash Time Lock Contract), using Cardano or Bitcoin
* Lightning Network.
* - Alice decrypts the file with the key from the HTLC or takes the money back after the
* timeout.
* - If Bob cheats, Alice can prove it and get the collateral from the bond contract.
* - Bob can withdraw the collateral by revealing the `password`.
*/
@Compile
object BondContract {
case class BondConfig(
passwordHash: ByteString,
encryptedId: ByteString,
serverPubKey: ByteString,
serverPubKeyHash: ByteString
)
enum BondAction:
case Withdraw(preimage: ByteString)
case FraudProof(
signature: ByteString,
password: ByteString,
encryptedChunk: ByteString,
chunkHash: ByteString,
chunkIndex: BigInt,
merkleProof: Data // List of ByteString
)
import scalus.builtin.FromDataInstances.given
given FromData[BondConfig] = FromData.deriveCaseClass
given ToData[BondConfig] = ToData.deriveCaseClass[BondConfig](0)
given FromData[BondAction] = FromData.deriveEnum
given ToData[BondAction] = ToData.deriveEnum
/** Convert BigInt to ByteString */
def integerToByteString(num: BigInt): ByteString =
if num <= BigInt(0) then throw new Exception(s"Number must be positive")
else Builtins.integerToByteString(true, 0, num)
def xorBytes(a: BigInt, b: BigInt): BigInt = {
def xorHelper(a: BigInt, b: BigInt, pow: BigInt, result: BigInt): BigInt = {
if pow == BigInt(256) then result
else xorHelper(a / 2, b / 2, pow * 2, result + ((a + b) % 2) * pow)
}
xorHelper(a, b, 1, 0)
}
// a and b are of the same length
def xor(a: ByteString, b: ByteString): ByteString = {
val l1 = lengthOfByteString(a)
val l2 = lengthOfByteString(b)
def xorHelper(a: ByteString, b: ByteString, i: BigInt, result: ByteString): ByteString = {
if i < 0 then result
else {
val byteA = indexByteString(a, i)
val byteB = indexByteString(b, i)
val xorByte = xorBytes(byteA, byteB)
xorHelper(a, b, i - 1, consByteString(xorByte, result))
}
}
if l1 == l2 then xorHelper(a, b, l1 - 1, ByteString.empty)
else throw new Exception("X")
}
/** Verifies Merkle inclusion proof */
inline def verifyMerkleInclusionProof(
merkleProof: Data,
encryptedChunk: ByteString,
chunkHash: ByteString,
chunkIndex: BigInt,
encId: ByteString
): Boolean =
val encryptedChunkAndChunkHashHash = sha2_256(appendByteString(encryptedChunk, chunkHash))
def loop(index: BigInt, curHash: ByteString, siblings: List[Data]): ByteString =
if siblings.isEmpty then curHash
else
val sibling = unBData(siblings.head)
val nextHash =
if index % 2 == BigInt(0)
then sha2_256(appendByteString(curHash, sibling))
else sha2_256(appendByteString(sibling, curHash))
loop(index / 2, nextHash, siblings.tail)
val merkleRoot = loop(chunkIndex, encryptedChunkAndChunkHashHash, unListData(merkleProof))
merkleRoot == encId
def verifyPreimage(preimage: ByteString, preimageHash: ByteString): Boolean =
preimageHash == sha2_256(preimage)
inline def verifyFraudProof(
chunkHash: ByteString,
chunkIndex: BigInt,
encId: ByteString,
encryptedChunk: ByteString,
merkleProof: Data,
preimage: ByteString,
preimageHash: ByteString,
serverPubKey: ByteString,
signature: ByteString
): Boolean =
val verifyWrongChunkHash =
val preimageAndIndex = appendByteString(preimage, integerToByteString(chunkIndex + 1))
// hash( Ei ⊕ hash( preimage || i) ) ≠ Hi
val expectedChunkHash = sha2_256(xor(encryptedChunk, sha2_256(preimageAndIndex)))
expectedChunkHash != chunkHash
log("verifyWrongChunkHash")
val verifyValidClaimSignature = {
val claim = appendByteString(encId, preimageHash)
verifyEd25519Signature(serverPubKey, claim, signature)
}
log("verifyValidClaimSignature")
val verifyValidPreimage = verifyPreimage(preimage, preimageHash)
log("verifyValidPreimage")
val merkleInclusionProofValid = verifyMerkleInclusionProof(
merkleProof,
encryptedChunk,
chunkHash,
chunkIndex,
encId
)
verifyWrongChunkHash.?
&& verifyValidClaimSignature.?
&& verifyValidPreimage.?
&& merkleInclusionProofValid.?
/** Bond contract validator
*
* @param datum
* BondConfig
* @param redeemer
* BondAction
* - Withdraw: Bob withdraws the collateral by revealing the `password`.
* - FraudProof: Alice proves that Bob cheated and gets the collateral.
* @param ctxData
* ScriptContext
*/
def bondContractValidator(ctxData: Data): Unit = {
val bondAction = ctxData.field[ScriptContext](_.redeemer).to[BondAction]
val info = ctxData.field[ScriptContext](_.scriptInfo)
val infoPair = info.toConstr
if infoPair.fst == BigInt(1) then // SpendingScript
val datum = info.field[ScriptInfo.SpendingScript](_.datum).to[Maybe[BondConfig]]
datum match
case Maybe.Just(
BondConfig(passwordHash, encryptedId, serverPubKey, serverPubKeyHash)
) =>
if bondContractCheck(
bondAction,
passwordHash,
encryptedId,
serverPubKey,
serverPubKeyHash,
// get PubKeyHash as a ByteString from the first signatory
// NOTE: we assume that the first signatory is the server
ctxData.field[ScriptContext](_.txInfo.signatories).toList
)
then ()
else throw new Exception()
case _ => throw new Exception("No datum")
}
private inline def bondContractCheck(
bondAction: BondAction,
passwordHash: ByteString,
encryptedId: ByteString,
serverPubKey: ByteString,
serverPubKeyHash: ByteString,
signatures: builtin.List[Data]
): Boolean = {
bondAction match {
case BondAction.Withdraw(password) =>
log("BondAction.Withdraw(preimage)")
// verify that the signatory is the server PubKeyHash from the BondConfig
val verifySignature = signatures.head.toByteString == serverPubKeyHash
// verify that the password is the correct preimage of the passwordHash
val verifyValidPreimage = verifyPreimage(password, passwordHash)
// return true if both conditions are met
verifySignature.? && verifyValidPreimage.?
case BondAction.FraudProof(
signature,
preimage,
encryptedChunk,
chunkHash,
chunkIndex,
merkleProof
) =>
log("BondAction.FraudProof")
verifyFraudProof(
chunkHash,
chunkIndex,
encryptedId,
encryptedChunk,
merkleProof,
preimage,
passwordHash,
serverPubKey,
signature
)
}
}
/** Hash-Time Locked Contract validator
*/
def makeHtlcContractValidator(
clientPubKeyHash: Data,
expiration: PosixTime,
hash: ByteString
)(ctxData: Data): Unit = {
val redeemer = ctxData.field[ScriptContext](_.redeemer)
val validPreimage = hash == sha2_256(redeemer.toByteString)
val expired = {
val txInfoData = ctxData.field[ScriptContext](_.txInfo)
val txtime =
txInfoData.field[TxInfo](_.validRange.from.boundType).to[IntervalBoundType]
txtime match
case IntervalBoundType.Finite(txtime) =>
val expired = expiration < txtime
val signedByClient = {
val signaturePubKeyHashData =
txInfoData.field[TxInfo](_.signatories).toList.head
signaturePubKeyHashData == clientPubKeyHash
}
expired.? && signedByClient.?
case _ => false
}
if validPreimage.? || expired.? then () else throw new Exception()
}
}