-
Notifications
You must be signed in to change notification settings - Fork 122
Interchain Exchange Tutorial
Performing an interchain exchange between DeFiChain and Bitcoin requires several steps. Please see the pinkpaper for an overview to understand the flow required: (Interchain Exchange Pinkpaper)[https://github.com/DeFiCh/pinkpaper/tree/main/interchain-exchange]
This tutorial will teach you how to use the Interchain Exchange through the command line interface.
- Firstly, the DeFiChain HTLC creator will need to generate a seed/hash pair, here is some Javascript code to generate a seed/hash pair:
import { createHash } from "https://deno.land/std@0.74.0/hash/mod.ts";
import { cryptoRandomString } from "https://deno.land/x/crypto_random_string@1.0.0/mod.ts"
import { decodeString } from "https://deno.land/std/encoding/hex.ts"
export const createSeedHashPair = () => {
const seed = cryptoRandomString({length: 64});
let hash = createHash('sha256');
hash.update(decodeString(seed));
return { seed: seed, hash: hash.toString('hex')};
}
Alternatively, you may skip this step, and omit the hash in step 4, the node will generate it for you.
-
Generate an SPV address to receive your Bitcoin using
spv_getnewaddress
and get the pub key usingspv_getaddresspubkey ADDRESS
-
Create the order using
icx_createorder {"tokenFrom": 0, "chainTo":"BTC","ownerAddress": YOUR_DEFICHAIN_ADDRESS,"amountFrom":30,"orderPrice":0.1,"expiry":1000, "receivePubkey": YOUR_SPV_PUBKEY}
-
Run
icx_listorders
and observe if you have an offer. Once you have an offer, create the DeFiChain HTLC using:icx_submitdfchtlc {"offerTx": offerTxId, "amount": 10, "hash": YOUR_HASH, "receiveAddress": newAddress, "receivePubkey": newAddressPubKey, "timeout": 500}
-
Run
icx_listorders
again until you see an external HTLC posted for your order. Once it's posted claim it using the seed by doingspv_claimhtlc EXT_HTLC_ADDRESS YOUR_SPV_ADDRESS YOUR_SEED 2000
- Firstly, the Bitcoin HTLC creator will need to generate a seed/hash pair, here is some Javascript code to generate a seed/hash pair:
import { createHash } from "https://deno.land/std@0.74.0/hash/mod.ts";
import { cryptoRandomString } from "https://deno.land/x/crypto_random_string@1.0.0/mod.ts"
import { decodeString } from "https://deno.land/std/encoding/hex.ts"
export const createSeedHashPair = () => {
const seed = cryptoRandomString({length: 64});
let hash = createHash('sha256');
hash.update(decodeString(seed));
return { seed: seed, hash: hash.toString('hex')};
}
Alternatively, you may skip this step, and omit the hash in step 4, the node will generate it for you.
-
Create the order using
icx_createorder {"tokenTo": 0, "chainFrom":"BTC","ownerAddress": YOUR_DEFICHAIN_ADDRESS,"amountFrom":30,"orderPrice":0.1,"expiry": 1000}
-
Obtain your SPV pubkey using
spv_getaddresspubkey ADDRESS
-
Run
icx_listorders
and observe if you have an offer. Once you have an offer, create the Bitcoin HTLC using:spv_createhtlc OTHER_PUBKEY YOUR_PUBKEY 30 YOUR_HASH
-
Run
icx_listorders
again until you see a DeFiChain HTLC posted for your order. Once it's posted claim it using the seed by doingicx_claimdfchtlc {"dfchtlcTx": HTLC_TXID, "amount": 0.00000001, "seed": YOUR_SEED}