Skip to content

Gateway

Nadimpalli Susruth edited this page Jul 23, 2019 · 1 revision

Gateway is a bitcoin script, that allows users to deposit funds to unique addresses and allow a single private key to spend the funds. Gateways are used instead of HD Wallets because new gateway addresses can be generated by just knowing the public key instead of knowing the master private key.

Bitcoin Script

<g_hash>
OP_DROP
OP_DUP
OP_HASH160
<spender_pubkey_hash>
OP_EQUALVERIFY
OP_CHECKSIG

To generate a gateway you would need the public key corresponding to the private key that can spend the funds and a random slice of bytes (gateway hash) that makes the script address unique.

Interacting with a Gateway

Depositing funds

To deposit funds, we need to build the gateway bitcoin script and calculate the script address.

// Instantiating a new btc/zec client
client, err := btcclient.New(logger, network)
if err != nil {
    // Handle error
}

// Instantiating a new Gateway
gateway := btcgateway.New(client, spenderPubKey, gHash)

// Calculating the Gateway address
gatewayAddress := gateway.Address()

// Funds should be deposited to the gatewayAddress

Spending funds

To spend funds from a gateway the spender needs to know the OutPoints of the Gateway address and have the spender private key.

// Instantiating a new btc/zec client
client, err := btcclient.New(logger, network)
if err != nil {
    // Handle error
}

// Instantiating the Gateway
gateway := btcgateway.New(client, spenderPubKey, gHash)

// `ops` are the OutPoints of the gateway the spender wants to spend
utxos := make(btctypes.UTXOs, len(ops))
var err error
for i, op := range ops {
    utxos[i], err = gateway.UTXO(op)
    if err != nil {
         // Handle error
    }
}

// `recipients` are the recipients of this tx, the change address of this transaction is sent to `refundAddress`.
// And the `txFee` is the fee used when submitting the tx.
tx, err := client.BuildUnsignedTx(utxos, recipients, refundAddress, txFee)
if err != nil {
     // Handle error
}

// Sign the tx
if err := tx.Sign(spenderPrivKey); err != nil {
    // Handle error
}

txHash, err := client.SubmitSignedTx(tx)
if err != nil {
     // Handle error
}
Clone this wiki locally