Skip to content

HOWTO Atomic Swaps

Harris Brakmić edited this page Feb 11, 2019 · 19 revisions

Atomic Swaps with ACM & BTC


Important

  • Depending on your configuration you might have to use additional parameters: --rpcuser and --rpcpass to access the wallet.

  • The commands from this tutorial are applicable to other supported cryptocurrencies as well.

  • To be able to execute atomic swaps you must use the legacy variant of Actinium wallet.

  • Only use P2PKH addresses. Use getnewaddress "" legacy in the console to generate them.

  • Your Actinium.conf must activate certain deprecated methods which are currently being used by the atomic swap tools. Future versions may be using newer variants. Here is an example configuration.

testnet=1
daemon=1
debug=0
shrinkdebugfile=1
server=1
listen=1
port=4335
rpcport=2301
rpcuser=your_user_name
rpcpassword=your_password
torenabled=0
zmqpubrawblock=tcp://127.0.0.1:28332
zmqpubrawtx=tcp://127.0.0.1:28333
addnode=goldmine.actinium.org
addnode=api.actinium.org
addnode=electrum1.actinium.org
deprecatedrpc=signrawtransaction

The commands used in this tutorial need certain parameters that will be produced by successful execution of other commands.

initiate <participant address> <amount>
participate <initiator address> <amount> <secret hash>
redeem <contract> <contract transaction> <secret>
refund <contract> <contract transaction>
extractsecret <redemption transaction> <secret hash>
auditcontract <contract> <contract transaction>

The participating sides (A & B) in the atomic swap described below use the same set of commands via their respective tools (btcatomicswap & acmatomicswap).


Create initiating transaction

Here the party B, initiates an atomic swap by promising to pay 0.00001 BTC to address miAZVuFy2cfS1U6V1Bqpbf3opWvV6EvLyw

$ btcatomicswap --testnet initiate miAZVuFy2cfS1U6V1Bqpbf3opWvV6EvLyw 0.00001
warning: falling back to mempool relay fee policy
Secret:      b731f4129255bb0e9303dd3114cbb82fd38e7732da9c7eba4367e4ba7b5c6bcc
Secret hash: 6d957766ba77b2b2b77f8d0c1455ee359aafbd87edce839c574c8b7d11a1f3e0

Contract fee: 0.00000166 BTC (0.00000672 BTC/kB)
Refund fee:   0.00000297 BTC (0.00001017 BTC/kB)

Contract (2N9jhV2cuBmbvrjSyAu5SXPZ7bM6veg1sv6):
6382012088a8206d957766ba77b2b2b77f8d0c1455ee359aafbd87edce839c574c8b7d11a1f3e08876a9141d0d0779ad4f9dcff308630a4a284c1aa6efbc7b67046026635cb17576a914a56407e615b3957442b47e6d4c07cfb0041404ac6888ac

Contract transaction (970876d6bbafc6bcb6e2af76c48af390bdf7dc0ef3fd73e40e4303d923c6c305):
020000000001017739f97422720f33c37eb9a27271dd8236eebb731e7b40e3214cd8f3aa7d9ad80100000017160014cff1d9fb1652b58ef1cabd0dc12bc19a64ebb9f0feffffff02e80300000000000017a914b4e49c9485198f12720f2a278229e167cd9ad066870409ee000000000017a914351ad3b31033373673429bd431df41fa514df0c787024730440220241990ed39ba3fb1e26446b092ce577177601beb4b0d3bfd7d0ca89103e61eba02202b71d142938312eb1e4ebdeb4443f3c9b1e0df478e730aff4988273ac2e0ea510121029099aa3862997c2b6e2c19b641ab3463c9be458619f1a594294b58b35690217900000000

Refund transaction (699d714f9241093c30bacf1a922f64449034ce27686354b61bc5a663d25571ec):
020000000105c3c623d903430ee473fdf30edcf7bd90f38ac476afe2b6bcc6afbbd676089700000000cf483045022100a8adea5193da3b9a25e7eb1f6ad23686740fdb4711c7602045d5b0d7e977c42902207d67d67ca4880f5a9c761c0d95c93fcf9a288135e54fb37527ac3afe894c451401210261cdbd2bbc59ea36d6ffe6283945be45f065958aa3551514b58be7284e480bc2004c616382012088a8206d957766ba77b2b2b77f8d0c1455ee359aafbd87edce839c574c8b7d11a1f3e08876a9141d0d0779ad4f9dcff308630a4a284c1aa6efbc7b67046026635cb17576a914a56407e615b3957442b47e6d4c07cfb0041404ac6888ac0000000001bf020000000000001976a914aac2e1ca55934f05883916e15f690634a282240a88ac6026635c

Publish contract transaction? [y/N] y
Published contract transaction (970876d6bbafc6bcb6e2af76c48af390bdf7dc0ef3fd73e40e4303d923c6c305) 

The most important part is of course the Secret which B will keep for its until it has redeemed the funds from party A.

The other party, A, now audits B's initiating contract

$ btcatomicswap --testnet auditcontract 6382012088a8206d957766ba77b2b2b77f8d0c1455ee359aafbd87edce839c574c8b7d11a1f3e08876a9141d0d0779ad4f9dcff308630a4a284c1aa6efbc7b67046026635cb17576a914a56407e615b3957442b47e6d4c07cfb0041404ac6888ac 020000000001017739f97422720f33c37eb9a27271dd8236eebb731e7b40e3214cd8f3aa7d9ad80100000017160014cff1d9fb1652b58ef1cabd0dc12bc19a64ebb9f0feffffff02e80300000000000017a914b4e49c9485198f12720f2a278229e167cd9ad066870409ee000000000017a914351ad3b31033373673429bd431df41fa514df0c787024730440220241990ed39ba3fb1e26446b092ce577177601beb4b0d3bfd7d0ca89103e61eba02202b71d142938312eb1e4ebdeb4443f3c9b1e0df478e730aff4988273ac2e0ea510121029099aa3862997c2b6e2c19b641ab3463c9be458619f1a594294b58b35690217900000000
Contract address:        2N9jhV2cuBmbvrjSyAu5SXPZ7bM6veg1sv6
Contract value:          0.00001 BTC
Recipient address:       miAZVuFy2cfS1U6V1Bqpbf3opWvV6EvLyw
Author's refund address: mvbTcyJvXyrHMwKCUU5PVgtUnvUaFukKQ9

Secret hash: 6d957766ba77b2b2b77f8d0c1455ee359aafbd87edce839c574c8b7d11a1f3e0

Locktime: 2019-02-12 20:02:40 +0000 UTC
Locktime reached in 47h57m18s

A only gets the Secret Hash but not the Secret. That will be released only after B has successfully redeemed the funds provided by A.

In this case the audit seems to be ok. The recipient address belongs to A so it's obvious that only A has the corresponding private key.

A now declares its participation in this atomic swap

As the audit from above showed the address controlled by A (miAZVuFy2cfS1U6V1Bqpbf3opWvV6EvLyw) it has decided to continue with the agreement by invoking the command participate.

A now promises to pay B 10.0ACM for the 0.00001BTC it funded above in the initiating transaction. A uses B's Secret Hash to bind its promise to B's contract. The goal is clear: if you take my funds I will take yours because they're connected together.

$ acmatomicswap -testnet participate JTrVLmhVfpFGSMUknXGsZJjvoPQT7DGk4P 10.0 6d957766ba77b2b2b77f8d0c1455ee359aafbd87edce839c574c8b7d11a1f3e0
warning: falling back to mempool relay fee policy
Contract fee: 0.00000222 BTC (0.00001005 ACM/kB)
Refund fee:   0.00000297 BTC (0.00001021 ACM/kB)

Contract (Qh3Ky7n5zPWV4ZptMRQVNhCrzjL3JK7a6o):
6382012088a8206d957766ba77b2b2b77f8d0c1455ee359aafbd87edce839c574c8b7d11a1f3e08876a914663b5544c11783c6742c5caa612c2e54a771b26367048ad6615cb17576a914722b7c937b2c788756bda0c4c5bc691792d968116888ac

Contract transaction (8b5af3b2e6bc7c2f7d8635384ca9d7afc16659a7378b8c76ab80a5c8802be21d):
02000000011936ac89e57c792b3c8aa2405c61aa501b45a9e5bbb496f79d3eff627f02ff03000000006a4730440220199e3fc57039ab38c923ad418d61804b0ebdcfd8e8134736ac13201de47da77202200a7e34f919488a346174e0560b92656bf81f368d9a6bd5d8c7dcacb2f13323c7012102df656a76f5cebb5f48ad5c94a608dcc391c44b9c883963766543a54b9e668020feffffff0200ca9a3b0000000017a914e02d7dfc62001c4fe39036e9552a81a96698cbdf87a2bff0050000000017a91424019fe10bfd1712eab0cc505d75e9f4669276cd8700000000

Refund transaction (463d063c7acc9a31ae495ad03e46381a5492904128e99e0ad4abdb61c224babe):
02000000011de22b80c8a580ab768c8b37a75966c1afd7a94c3835867d2f7cbce6b2f35a8b00000000ce47304402201ac803b1e58ed1cdcb33ce6f9ab53b455f56d1bc7bf03267990b76232ef64de5022074a7b9d3face90d03f7b87a0810310c22a3c78fe14499174ac9541faf2709ed10121034cea53043c23bfe7cd48bff87de970f9d556b1b0350cfd4410712af255ddf372004c616382012088a8206d957766ba77b2b2b77f8d0c1455ee359aafbd87edce839c574c8b7d11a1f3e08876a914663b5544c11783c6742c5caa612c2e54a771b26367048ad6615cb17576a914722b7c937b2c788756bda0c4c5bc691792d968116888ac0000000001d7c89a3b000000001976a914e670544527db697484e5da4919be9fa5c1be250188ac8ad6615c

Publish contract transaction? [y/N] y
Published contract transaction (8b5af3b2e6bc7c2f7d8635384ca9d7afc16659a7378b8c76ab80a5c8802be21d)

The party B will audit A's transaction as well:

$ acmatomicswap -testnet auditcontract 6382012088a8206d957766ba77b2b2b77f8d0c1455ee359aafbd87edce839c574c8b7d11a1f3e08876a914663b5544c11783c6742c5caa612c2e54a771b26367048ad6615cb17576a914722b7c937b2c788756bda0c4c5bc691792d968116888ac 02000000011936ac89e57c792b3c8aa2405c61aa501b45a9e5bbb496f79d3eff627f02ff03000000006a4730440220199e3fc57039ab38c923ad418d61804b0ebdcfd8e8134736ac13201de47da77202200a7e34f919488a346174e0560b92656bf81f368d9a6bd5d8c7dcacb2f13323c7012102df656a76f5cebb5f48ad5c94a608dcc391c44b9c883963766543a54b9e668020feffffff0200ca9a3b0000000017a914e02d7dfc62001c4fe39036e9552a81a96698cbdf87a2bff0050000000017a91424019fe10bfd1712eab0cc505d75e9f4669276cd8700000000

Contract address:        Qh3Ky7n5zPWV4ZptMRQVNhCrzjL3JK7a6o
Contract value:          10.00000000 ACM
Recipient address:       JTrVLmhVfpFGSMUknXGsZJjvoPQT7DGk4P
Author's refund address: JUwcU3R3PZzBDK9bJv1PEPuJt71cU5QeVb

Secret hash: 6d957766ba77b2b2b77f8d0c1455ee359aafbd87edce839c574c8b7d11a1f3e0

Locktime: 2019-02-11 20:09:46 +0000 UTC
Locktime reached in 23h44m56s

B sees proper lock times (24h), its address (JTrVLmhVfpFGSMUknXGsZJjvoPQT7DGk4P), and the Secret Hash that it recognizes as its own. The result is clear: because the Secret Hash corresponds to the Secret only B knows, B can be sure that it will be able to redeem funds provided by A.

The party A now redeems the BTC funds provided by B.

$ btcatomicswap --testnet redeem 6382012088a8206d957766ba77b2b2b77f8d0c1455ee359aafbd87edce839c574c8b7d11a1f3e08876a9141d0d0779ad4f9dcff308630a4a284c1aa6efbc7b67046026635cb17576a914a56407e615b3957442b47e6d4c07cfb0041404ac6888ac 020000000001017739f97422720f33c37eb9a27271dd8236eebb731e7b40e3214cd8f3aa7d9ad80100000017160014cff1d9fb1652b58ef1cabd0dc12bc19a64ebb9f0feffffff02e80300000000000017a914b4e49c9485198f12720f2a278229e167cd9ad066870409ee000000000017a914351ad3b31033373673429bd431df41fa514df0c787024730440220241990ed39ba3fb1e26446b092ce577177601beb4b0d3bfd7d0ca89103e61eba02202b71d142938312eb1e4ebdeb4443f3c9b1e0df478e730aff4988273ac2e0ea510121029099aa3862997c2b6e2c19b641ab3463c9be458619f1a594294b58b35690217900000000 b731f4129255bb0e9303dd3114cbb82fd38e7732da9c7eba4367e4ba7b5c6bcc
warning: falling back to mempool relay fee policy
Redeem fee: 0.0000033 BTC (0.00001019 BTC/kB)

Redeem transaction (f0be32fb27d5ff8b49936337d18b9321d735ecc03b5db4fc6417abcbffb34ede):
020000000105c3c623d903430ee473fdf30edcf7bd90f38ac476afe2b6bcc6afbbd676089700000000ef47304402202df3c0ce602e5618bcdc9e5d599a1104ffb7cab1197e8c8d252625c8ac74380e02202c703e996fe39e452da21dc5a0c32471ebc2e5fe0b819d3f0292ea0d672dc9f1012102b3d5c42665e3dfbeb467e848eb66150f4847617aaa161510633297db35c27d1120b731f4129255bb0e9303dd3114cbb82fd38e7732da9c7eba4367e4ba7b5c6bcc514c616382012088a8206d957766ba77b2b2b77f8d0c1455ee359aafbd87edce839c574c8b7d11a1f3e08876a9141d0d0779ad4f9dcff308630a4a284c1aa6efbc7b67046026635cb17576a914a56407e615b3957442b47e6d4c07cfb0041404ac6888acffffffff019e020000000000001976a9142a496b870c735e5e7000f64f6b154ab2d0af259a88ac6026635c

Publish redeem transaction? [y/N] y
Published redeem transaction (f0be32fb27d5ff8b49936337d18b9321d735ecc03b5db4fc6417abcbffb34ede)

After the transaction has been published B will be able to redeem the ACMs in two steps:

  • B extracts the Secret packed inside A's BTC-redeem transaction
  • B uses this Secret to unlock the ACM-funds
B extracts the Secret from A's transaction
$ acmatomicswap -testnet extractsecret 020000000105c3c623d903430ee473fdf30edcf7bd90f38ac476afe2b6bcc6afbbd676089700000000ef47304402202df3c0ce602e5618bcdc9e5d599a1104ffb7cab1197e8c8d252625c8ac74380e02202c703e996fe39e452da21dc5a0c32471ebc2e5fe0b819d3f0292ea0d672dc9f1012102b3d5c42665e3dfbeb467e848eb66150f4847617aaa161510633297db35c27d1120b731f4129255bb0e9303dd3114cbb82fd38e7732da9c7eba4367e4ba7b5c6bcc514c616382012088a8206d957766ba77b2b2b77f8d0c1455ee359aafbd87edce839c574c8b7d11a1f3e08876a9141d0d0779ad4f9dcff308630a4a284c1aa6efbc7b67046026635cb17576a914a56407e615b3957442b47e6d4c07cfb0041404ac6888acffffffff019e020000000000001976a9142a496b870c735e5e7000f64f6b154ab2d0af259a88ac6026635c 6d957766ba77b2b2b77f8d0c1455ee359aafbd87edce839c574c8b7d11a1f3e0
Secret: b731f4129255bb0e9303dd3114cbb82fd38e7732da9c7eba4367e4ba7b5c6bcc
$ Secret: b731f4129255bb0e9303dd3114cbb82fd38e7732da9c7eba4367e4ba7b5c6bcc
B redeems ACMs by using the Secret as one of the parameters
acmatomicswap --testnet redeem 6382012088a8206d957766ba77b2b2b77f8d0c1455ee359aafbd87edce839c574c8b7d11a1f3e08876a914663b5544c11783c6742c5caa612c2e54a771b26367048ad6615cb17576a914722b7c937b2c788756bda0c4c5bc691792d968116888ac 02000000011936ac89e57c792b3c8aa2405c61aa501b45a9e5bbb496f79d3eff627f02ff03000000006a4730440220199e3fc57039ab38c923ad418d61804b0ebdcfd8e8134736ac13201de47da77202200a7e34f919488a346174e0560b92656bf81f368d9a6bd5d8c7dcacb2f13323c7012102df656a76f5cebb5f48ad5c94a608dcc391c44b9c883963766543a54b9e668020feffffff0200ca9a3b0000000017a914e02d7dfc62001c4fe39036e9552a81a96698cbdf87a2bff0050000000017a91424019fe10bfd1712eab0cc505d75e9f4669276cd8700000000 b731f4129255bb0e9303dd3114cbb82fd38e7732da9c7eba4367e4ba7b5c6bcc
warning: falling back to mempool relay fee policy
Redeem fee: 0.0000033 BTC (0.00001019 ACM/kB)

Redeem transaction (e0a6a53069aaef3bd7c4577bf11e4d9ca7b39e85524e65cd61453bac16ede4a8):
02000000011de22b80c8a580ab768c8b37a75966c1afd7a94c3835867d2f7cbce6b2f35a8b00000000ef47304402205f51f51f385aa70757ec3db7370a233dad1b8b638b40a2c391e8096c67ac91b90220011061ab24d664cf1f6b30b1be2018791f1552cf7316118edae0c03a627e4dbc0121023acbcfa5f52328bf8bed3e614401516386441ead780f660ddb307b2ea9ba4c8f20b731f4129255bb0e9303dd3114cbb82fd38e7732da9c7eba4367e4ba7b5c6bcc514c616382012088a8206d957766ba77b2b2b77f8d0c1455ee359aafbd87edce839c574c8b7d11a1f3e08876a914663b5544c11783c6742c5caa612c2e54a771b26367048ad6615cb17576a914722b7c937b2c788756bda0c4c5bc691792d968116888acffffffff01b6c89a3b000000001976a914dfb539c843549a132dbebd047ddc5e0c6655610788ac8ad6615c

Publish redeem transaction? [y/N] y
Published redeem transaction (e0a6a53069aaef3bd7c4577bf11e4d9ca7b39e85524e65cd61453bac16ede4a8)

The atomic swap is now complete.