Bridge swap pBTC <=> BTC

How to swap pCoin <=> Coin. Tutorial how to make app using Golang SDK.

we using Bridge BTC to pBTC as example

First : We need 2 master wallets: Incognito master account and Bitcoin master account.

1. Swap Bitcoin (Bitcoin network) to pBTC (incognito network).

  • Step1: generate a new Bitcoin temp wallet (show qr-code) on the swap page.

  • Step2: user send Bitcoin to the Bitcoin temp address from step1 (users use btc wallet to send).

  • Step3: check Bitcoin balance of temp wallet.

  • Step4: send pBTC (from Incognito master wallet) to the user’s incognito payment address.

  • Step5: send BTC from temp address to master Bitcoin wallet.

// Generate a new btc address (step1):
func (bs *BlockcypherService) GenerateAddress() (privKey, pubKey, address string, err error) {
secret, err := btcec.NewPrivateKey(btcec.S256())
if err != nil {
err = errors.Wrap(err, "c.GenerateAddress")
return
}
wif, err := btcutil.NewWIF(secret, bs.getNetworkParams(), true)
if err != nil {
err = errors.Wrap(err, "c.GenerateAddress")
return
}
privKey = wif.String()
addr, err := btcutil.NewAddressPubKey(wif.PrivKey.PubKey().SerializeCompressed(), bs.getNetworkParams())
if err != nil {
err = errors.Wrap(err, "btcutil.NewAddressPubKey")
return
}
address = addr.EncodeAddress()
return
}
// get btc balance (step2):
func (bs *BlockcypherService) GetBalance(address string) (*big.Int, error) {
btcAddrInfo, err := bs.BTCGetAddrInfo(address)
if err != nil {
return nil, errors.Wrap(err, "c.btc.BalanceAt")
}
b := new(big.Int)
b.SetUint64(btcAddrInfo.Balance)
return b, nil
}
// send pBTC (step3):
func (b *Blockchain) SendToken(privateKey string, receiverAddress string, tokenId string, amount uint64, fee uint64, feeTokenId string) (string, error) {
if tokenId == common.PRV {
var listPaymentAddresses = make(map[string]uint64)
listPaymentAddresses[receiverAddress] = amount
return b.CreateAndSendConstantPrivacyTransaction(privateKey, serializers.WalletSend{
Type: 0,
PaymentAddresses: listPaymentAddresses,
})
}
param := serializers.WalletSend{
TokenID: tokenId,
Type: 1,
PaymentAddresses: map[string]uint64{
receiverAddress: amount,
},
}
if feeTokenId == tokenId {
param.TokenFee = fee
}
tx, err := b.SendPrivacyCustomTokenTransaction(privateKey, param)
if err != nil {
return "", errors.Wrap(err, "b.SendPrivacyCustomTokenTransaction")
}
txID, ok := tx["TxID"].(string)
if !ok {
return "", errors.Errorf("couldn't get txID: result: %+v", tx)
}
return txID, nil
}
// send btc (step4):
func (bs *BlockcypherService) SendTransaction(secret string, from string, destination string, amount int, fee int) (string, error) {
wif, err := btcutil.DecodeWIF(secret)
if err != nil {
return "", err
}
pkHex := hex.EncodeToString(wif.PrivKey.Serialize())
tx := gobcy.TempNewTX(from, destination, amount)
tx.Fees = fee
skel, err := bs.chain.NewTX(tx, false)
if err != nil {
return "", err
}
prikHexs := []string{}
for i := 0; i < len(skel.ToSign); i++ {
prikHexs = append(prikHexs, pkHex)
}
err = skel.Sign(prikHexs)
if err != nil {
log.Println(err)
return "", err
}
skel, err = bs.chain.SendTX(skel)
if err != nil {
log.Println(err)
return "", err
}
return skel.Trans.Hash, nil
}

2. Swap pBTC (Incognito network) to Bitcoin (Bitcoin network).

  • Step1: generate a new Incognito temp wallet (show qr-code) on the swap page.

  • Step2: user send pBTC to the incognito temp address from step1.

  • Step3: check pBTC balance of temp wallet.

  • Step4: send BTC(from Bitcoin master wallet) to the user’s Bitcoin payment address.

  • Step5: send pBTC to master Incognito wallet.