-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy patheth.ak
188 lines (166 loc) · 5.87 KB
/
eth.ak
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
use aiken/builtin
use aiken/cbor
use aiken/collection/list
use aiken/collection/pairs
use aiken/crypto.{keccak_256, sha2_256}
use aiken/interval.{Finite}
use aiken/primitive/bytearray.{length}
use aiken/primitive/int.{to_string}
use aiken/primitive/string
use cardano/address.{Credential, Script}
use cardano/assets.{
from_asset, from_lovelace, lovelace_of, merge, negate, quantity_of,
}
use cardano/certificate.{
Certificate, DelegateBlockProduction, DelegateCredential,
}
use cardano/transaction.{OutputReference, Transaction, find_input}
pub type Redeemer {
Send {
signature: ByteArray,
utxo: OutputReference,
policy: ByteArray,
assetname: ByteArray,
amount: Int,
pubkey: ByteArray,
stakekey: ByteArray,
}
Merge
Stake { signature: ByteArray, deadline: Int }
}
const magic =
@"Ethereum Signed Message:\n"
validator btc(pubKey: ByteArray) {
spend(
_datum: Option<Data>,
redeemer: Redeemer,
own_ref: OutputReference,
self: Transaction,
) {
//we'll build the message like this,txid+index+unit+amount+pubkey+stakekeys
when redeemer is {
Send(signature, utxo, policy, assetname, amount, pubkey, stakekey) -> {
let message_array: List<ByteArray> =
list.push([], stakekey)
|> list.push(pubkey)
|> list.push(bytearray.from_int_big_endian(amount, 20))
|> list.push(assetname)
|> list.push(policy)
|> list.push(bytearray.from_int_big_endian(utxo.output_index, 1))
|> list.push(utxo.transaction_id)
let concat_array = concat_array(message_array)
let message_hash = get_hash_from_bytearray(concat_array)
//TODO da checkare
//verify I am sending the right amount to the right user from the data i have in the first output
//verify the second output goes to the starting one - the amount it had - 1 ADA
//address validation receiver is missing
let sending_value = from_asset(policy, assetname, amount)
expect Some(contract_input) = find_input(self.inputs, own_ref)
expect Some(payment_output) = list.at(self.outputs, 0)
expect Some(returning_output) = list.at(self.outputs, 1)
let starting_ada = lovelace_of(contract_input.output.value)
let basic_validation = and {
own_ref == utxo,
validate_signature_key(pubKey, message_hash, signature),
}
if starting_ada > 4000000 {
and {
quantity_of(payment_output.value, policy, assetname) == quantity_of(
sending_value,
policy,
assetname,
),
merge(
merge(contract_input.output.value, negate(sending_value)),
negate(from_lovelace(1000000)),
) == returning_output.value,
basic_validation,
}
} else {
and {
quantity_of(payment_output.value, policy, assetname) == quantity_of(
sending_value,
policy,
assetname,
),
merge(contract_input.output.value, negate(sending_value)) == returning_output.value,
basic_validation,
}
}
}
//We need to give back some ADA to the ones able to merge the utxos of the smart accounts, 2 ADA ideally
Merge -> False
_ -> False
}
}
//anyone can withdraw stake rewards, but only the user can do get them
withdraw(_redeemer: Redeemer, account: Credential, self: Transaction) {
when account is {
Script(hash) -> {
expect Some(withdrawAmount) = pairs.get_first(self.withdrawals, account)
expect Some(first_output) = list.at(self.outputs, 0)
and {
first_output.value == from_lovelace(withdrawAmount - 1000000),
first_output.address.payment_credential == Script(hash),
withdrawAmount > 10000000,
}
}
_ -> False
}
}
publish(redeemer: Redeemer, certificate: Certificate, self: Transaction) {
expect Stake(signature, deadline) = redeemer
when certificate is {
DelegateCredential(_, delegateCredential) -> {
expect DelegateBlockProduction(poolId) = delegateCredential
let message_array: List<ByteArray> =
list.push([], poolId)
|> list.push(bytearray.from_int_big_endian(deadline, 20))
let concat_message = concat_array(message_array)
let message_hash = get_hash_from_bytearray(concat_message)
//let message_hash = get_hash_from_bytearray(poolId)
//TODO missing end in the hash
trace cbor.diagnostic(message_hash)
and {
validate_signature_key(pubKey, message_hash, signature),
validate_tx_end(self, deadline),
}
}
_ -> {
trace @"finisco qua"
False
}
}
}
else(_) {
fail
}
}
fn get_hash_from_bytearray(byte: ByteArray) {
//this is what I sign
let concat_message =
string.to_bytearray(bytearray.to_hex(sha2_256(sha2_256(byte))))
let size = string.to_bytearray(to_string(length(concat_message)))
//il doppio sha poi lo rendo stringa e maiuscolo, è quello che firmo
let final_message = bytearray.concat(size, concat_message)
let magic_array = bytearray.from_string(magic)
let final_magic = bytearray.concat(#"19", magic_array)
let finalized = bytearray.concat(final_magic, final_message)
keccak_256(finalized)
}
fn validate_signature_key(publicKey: ByteArray, msg: ByteArray, sig: ByteArray) {
trace cbor.diagnostic(
builtin.verify_ecdsa_secp256k1_signature(publicKey, msg, sig),
)
builtin.verify_ecdsa_secp256k1_signature(publicKey, msg, sig)
}
fn concat_array(list_array: List<ByteArray>) {
list.reduce(list_array, #[], bytearray.concat)
}
fn validate_tx_end(tx: Transaction, deadline: Int) {
trace cbor.diagnostic(deadline)
when tx.validity_range.upper_bound.bound_type is {
Finite(end) -> end < deadline
_ -> False
}
}