-
Notifications
You must be signed in to change notification settings - Fork 1
/
SmartVault.Demo.Setup.sh
384 lines (293 loc) · 10.6 KB
/
SmartVault.Demo.Setup.sh
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
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
#!/bin/bash
#####################################
bitcoin-cli -named createwallet wallet_name="POC" descriptors=false >/dev/null 2>&1
#User - Keys
#UserPriv="cUrRAGYGV9Lj7yk7qFMZxxVeFTFqgt6BuJheb4EgVMafHef8f9p9"
#UserPub="023320c921fb86d276cf996c97a3f3893e5da2c03926acd1d5160d0ccdb582f416"
echo "-------------"
echo "Generating User's Priv-Pub-Addr"
echo "-------------"
UserAdrs=$(bitcoin-cli getnewaddress)
UserPriv=$(bitcoin-cli dumpprivkey "$UserAdrs")
UserPub=$(bitcoin-cli getaddressinfo "$UserAdrs" | jq -r .pubkey)
echo "Private Key: $UserPriv"
echo "Public Key: $UserPub"
echo "Address: $UserAdrs"
#Ledger - Keys
#LedgerPriv="cS71P5KPZbgGYhkXfTomFNYxq2NRccQb8Zkw3XEQkMVnQdSvAYQn"
#LedgerPub="03cb7ef39e4bf4e487f73dd8c0ac6f0ef112a6ac7b3fa09546007121605bfa7c7b"
echo "-------------"
echo "Generating Ledger's Priv-Pub-Addr"
echo "-------------"
LedgerAdrs=$(bitcoin-cli getnewaddress)
LedgerPriv=$(bitcoin-cli dumpprivkey "$LedgerAdrs")
LedgerPub=$(bitcoin-cli getaddressinfo "$LedgerAdrs" | jq -r .pubkey)
echo "Private Key: $LedgerPriv"
echo "Public Key: $LedgerPub"
echo "Address: $LedgerAdrs"
#####################################
echo "-------------"
echo "Generating blocks to bootstrap Bitcoin RegTest Blockchain"
echo "-------------"
bitcoin-cli generatetoaddress 101 "$UserAdrs" >/dev/null 2>&1
#bitcoin-cli importaddress "$UserAdrs"
echo "Done"
utxo_txid_1=$(bitcoin-cli listunspent | jq -r '.[0] | .txid')
utxo_vout_1=$(bitcoin-cli listunspent | jq -r '.[0] | .vout')
# Create Deposit Transaction
echo "-------------"
echo "Deposit Tx Redeem Script"
echo "-------------"
#####################################################################
# Deposit Transaction - Script / Smart Contract - Ivy Lang
#
# contract Deposit(
# user: PublicKey,
# ledger: PublicKey,
# val: Value
# )
# {
# clause spend(userSig: Signature, ledgerSig: Signature) {
# verify checkMultiSig([user, ledger], [userSig, ledgerSig])
# unlock val
# }
# }
#####################################################################
#DepositTxRedeemScript="2103cb7ef39e4bf4e487f73dd8c0ac6f0ef112a6ac7b3fa09546007121605bfa7c7b21023320c921fb86d276cf996c97a3f3893e5da2c03926acd1d5160d0ccdb582f41600547a547a527152ae"
DepositTxRedeemScript="21${LedgerPub}21${UserPub}00547a547a527152ae"
echo $DepositTxRedeemScript
echo "-------------"
echo "Checking Deposit Tx Redeem Script"
echo "-------------"
bitcoin-cli decodescript "$DepositTxRedeemScript"
# bitcoin-cli decodescript $DepositTxRedeemScript
# {
# "asm": "03cb7ef39e4bf4e487f73dd8c0ac6f0ef112a6ac7b3fa09546007121605bfa7c7b 023320c921fb86d276cf996c97a3f3893e5da2c03926acd1d5160d0ccdb582f416 0 4 OP_ROLL 4 OP_ROLL 2 OP_2ROT 2 OP_CHECKMULTISIG",
# "type": "nonstandard",
# "p2sh": "2N67fe2umeVqEvKP9pDho7U44WFEB6qwSkf",
# "segwit": {
# "asm": "0 60ab4558df4445fdcae68eba8810c6febcae467e88e2a1ac0f1c2169d449a759",
# "hex": "002060ab4558df4445fdcae68eba8810c6febcae467e88e2a1ac0f1c2169d449a759",
# "reqSigs": 1,
# "type": "witness_v0_scripthash",
# "addresses": [
# "bcrt1qvz452kxlg3zlmjhx36agsyxxl672u3n73r32rtq0rsskn4zf5avs0ye2zj"
# ],
# "p2sh-segwit": "2NEe9XKk3mDrc6FEZvoa19KGmE55bVpKb2L"
# }
# }
DepositTxOutputAddress=$(bitcoin-cli decodescript "$DepositTxRedeemScript" | jq -r .segwit.address)
echo "-------------"
echo "Deposit Tx - Script to Address"
echo "-------------"
echo $DepositTxOutputAddress
read -r -d '' DepositTxInputs <<-EOM
[
{
"txid": "$utxo_txid_1",
"vout": $utxo_vout_1
}
]
EOM
read -r -d '' DepositTxOutputs <<-EOM
[
{
"$DepositTxOutputAddress": 49.999
}
]
EOM
echo "-------------"
echo "Creating Unsigned Deposit Tx"
echo "-------------"
DepositTx=$(bitcoin-cli createrawtransaction "$DepositTxInputs" "$DepositTxOutputs")
echo "Done"
echo "-------------"
echo "Unsigned Deposit Tx"
echo "-------------"
echo $DepositTx
echo "-------------"
echo "Checking Unsigned Deposit Tx"
echo "-------------"
bitcoin-cli decoderawtransaction "$DepositTx"
echo "-------------"
echo "Signing the Deposit Tx"
echo "-------------"
DepositTxSigned=$(bitcoin-cli signrawtransactionwithkey "$DepositTx" "[\"$UserPriv\"]" | jq -r '.hex')
echo "Done"
echo "-------------"
echo "Signed Deposit Tx"
echo "-------------"
echo $DepositTxSigned
echo "-------------"
echo "Checking Signed Deposit Tx"
echo "-------------"
bitcoin-cli decoderawtransaction "$DepositTxSigned"
DepositTxID=$(bitcoin-cli decoderawtransaction "$DepositTxSigned" | jq -r '.txid')
DepositTxScriptPubKey=$(bitcoin-cli decoderawtransaction "$DepositTxSigned" | jq '.vout[0] | .scriptPubKey.hex')
DepositTxAmount=$(bitcoin-cli decoderawtransaction "$DepositTxSigned" | jq '.vout[0] | .value')
echo "-------------"
echo "Provsional Tx Redeem Script"
echo "-------------"
#####################################################################
# Provisional Transaction - Script / Smart Contract - Ivy Lang
#
# contract SmartVault(
# user: PublicKey,
# ledger: PublicKey,
# userDelay: Duration,
# ledgerDelay: Duration,
# val: Value
# )
# {
# /* Option : 1 [1000] */
# clause User(userSig: Signature) {
# verify checkSig(user, userSig)
# verify older(userDelay)
# unlock val
# }
# /* Option : 2 [5000] */
# clause Ledger(ledgerSig: Signature) {
# verify checkSig(ledger, ledgerSig)
# verify older(ledgerDelay)
# unlock val
# }
# /* Option : 3 */
# clause MultiSig(userSig: Signature, ledgerSig: Signature) {
# verify checkMultiSig([user, ledger], [userSig, ledgerSig])
# unlock val
# }
# }
#####################################################################
UserDelay=1000
echo "Setting Option 1 (User Private-Key Only) Delay to $UserDelay"
UserDelayCoded=$(python3 helpers/number_coding.py --encode $UserDelay) #500=f401
LedgerDelay=5000
echo "Setting Option 2 (Ledger Private-Key Only) Delay to $LedgerDelay"
LedgerDelayCoded=$(python3 helpers/number_coding.py --encode $LedgerDelay) #2500=c409
echo "Hex:"
ProvTxRedeemScript="02${LedgerDelayCoded}02${UserDelayCoded}21${LedgerPub}21${UserPub}54795287637b757b757b7500547a547a527152ae67547a6375777b7cadb2755167777b757b7cadb275516868"
echo $ProvTxRedeemScript
echo "-------------"
echo "Checking Provisional Tx Redeem Script"
echo "-------------"
bitcoin-cli decodescript "$ProvTxRedeemScript"
# Ref:
# bitcoin-cli decodescript $ProvTxRedeemScript
# {
# "asm": "2500 500 03cb7ef39e4bf4e487f73dd8c0ac6f0ef112a6ac7b3fa09546007121605bfa7c7b 023320c921fb86d276cf996c97a3f3893e5da2c03926acd1d5160d0ccdb582f416 4 OP_PICK 2 OP_EQUAL OP_IF OP_ROT OP_DROP OP_ROT OP_DROP OP_ROT OP_DROP 0 4 OP_ROLL 4 OP_ROLL 2 OP_2ROT 2 OP_CHECKMULTISIG OP_ELSE 4 OP_ROLL OP_IF OP_DROP OP_NIP OP_ROT OP_SWAP OP_CHECKSIGVERIFY OP_CHECKSEQUENCEVERIFY OP_DROP 1 OP_ELSE OP_NIP OP_ROT OP_DROP OP_ROT OP_SWAP OP_CHECKSIGVERIFY OP_CHECKSEQUENCEVERIFY OP_DROP 1 OP_ENDIF OP_ENDIF",
# "type": "nonstandard",
# "p2sh": "2NALhW1somHP9ifDFc6YjzbTPfUB8dbgNuL",
# "segwit": {
# "asm": "0 20d29d4c1268eef833fd1d9a25fc2881dd36ab55c92e56f10a0e81b557e8ef95",
# "hex": "002020d29d4c1268eef833fd1d9a25fc2881dd36ab55c92e56f10a0e81b557e8ef95",
# "reqSigs": 1,
# "type": "witness_v0_scripthash",
# "addresses": [
# "bcrt1qyrff6nqjdrh0svlarkdztlpgs8wnd264eyh9dug2p6qm24lga72srtvven"
# ],
# "p2sh-segwit": "2N63Rp9WJEvkVviV6Hbx1qmV7JaKv2jZAyU"
# }
# }
echo "-------------"
echo "Provisional Tx - Script to Address"
echo "-------------"
ProvTxOutputAddress=$(bitcoin-cli decodescript "$ProvTxRedeemScript" | jq -r .segwit.address)
echo $ProvTxOutputAddress
###########
read -r -d '' ProvTxInputs <<-EOM
[
{
"txid": "$DepositTxID",
"vout": 0
}
]
EOM
read -r -d '' ProvTxOutputs <<-EOM
[
{
"$ProvTxOutputAddress": 49.998
}
]
EOM
echo "-------------"
echo "Creating Unsigned Provisional Tx"
echo "-------------"
ProvTx=$(bitcoin-cli createrawtransaction "$ProvTxInputs" "$ProvTxOutputs")
echo $ProvTx
echo "-------------"
echo "Checking Unsigned Prov Tx Created by User"
echo "-------------"
bitcoin-cli decoderawtransaction "$ProvTx"
echo "-------------"
echo "Simulating the transfer of Unsigned Provisional Tx copy"
echo "to Ledger!"
echo "-------------"
echo "[U] --> Unsigned Provisional Tx --> [L]"
echo "-------------"
echo "Ledger signs the Unsigned Provisional Tx"
echo "with his Private Key"
echo "-------------"
LedgerSignatureProv=$(python3 helpers/sign_tx.py $ProvTx 0 $DepositTxAmount $DepositTxRedeemScript $LedgerPriv SIGHASH_ALL True)
echo "-------------"
echo "ProvTx - Ledger's Signature"
echo "-------------"
echo $LedgerSignatureProv
echo "-------------"
echo "Simulating the transfer of Partially Signed Provisional Tx"
echo "to User!"
echo "-------------"
echo "[L] --> Partially Signed Provisional Tx --> [U]"
####################################
echo "-------------"
echo "User signs the Unsigned Provisional Tx"
echo "with his Private Key"
echo "-------------"
UserSignatureProv=$(python3 helpers/sign_tx.py $ProvTx 0 $DepositTxAmount $DepositTxRedeemScript $UserPriv SIGHASH_ALL True)
echo "-------------"
echo "ProvTx - User Signature"
echo "-------------"
echo $UserSignatureProv
echo "-------------"
echo "Simulating the transfer of Partially Signed Provisional Tx copy"
echo "to Ledger!"
echo "-------------"
echo "[U] --> Partially Signed Provisional Tx --> [L]"
#######################################
#
# Deositor will broadcast DepositTx
# after receiving the
# Partially Signed Prov. Tx with
# Ledger's signature already added to it
#
#######################################
echo "-------------"
echo "User signs & broadcasts the Deposit Tx"
echo "after receiving the Partially signed Provisional Tx"
echo "-------------"
#Broadcast DepositTx
bitcoin-cli sendrawtransaction "$DepositTxSigned" >/dev/null 2>&1
echo "Done"
echo "-------------"
echo "Generating Block to confirm the Deposit Tx"
echo "-------------"
#Confirm the transaction in a block
bitcoin-cli generatetoaddress 1 "$UserAdrs" >/dev/null 2>&1
DepositTxBlock=$(bitcoin-cli getbestblockhash)
echo "Deposit Tx Block: $DepositTxBlock"
echo "-------------"
echo "Confirmed Deposit Tx"
echo "-------------"
bitcoin-cli getrawtransaction "$DepositTxID" true "$DepositTxBlock"
echo "-------------"
echo "***Smart Vault Setup Complete!***"
echo "-------------"
#####################################
echo "User is now assured of his"
echo "Bitcoin's safety and security"
echo "as his Bitcoin is now in Joint Custody and"
echo "rest everything is managed using the"
echo "Partially Signed Provisional Transactions"
echo "with User and Ledger after the Smart Vault setup!"
echo "-------------"
read -n 1 -s -r -p "Press any key to continue..."
echo ""