Skip to content

Commit

Permalink
Merge pull request #175 from andreabonel/update_getnewaddress_result_…
Browse files Browse the repository at this point in the history
…example

Fixes for elements 23
  • Loading branch information
wintercooled authored Nov 16, 2023
2 parents 8a99454 + 4e235d7 commit 26f60be
Show file tree
Hide file tree
Showing 7 changed files with 44 additions and 159 deletions.
123 changes: 3 additions & 120 deletions elements-code-tutorial/advanced-examples.md
Original file line number Diff line number Diff line change
Expand Up @@ -763,13 +763,9 @@ echo "Completed without error"
<a id="registry"></a>
### Example 3: Issuing an asset and using the contract hash argument to enable registration with the Blockstream Liquid Asset Registry.

Save the code below in a file named **advancedexamplesregistry.sh** and place it in your home directory and run:
Please refer to the <a href="https://docs.liquid.net/docs/blockstream-liquid-asset-registry" target="_blank">Blockstream docs site</a> to see how to create an asset, register it and prove ownership against a domain name.

~~~~
bash advancedexamplesregistry.sh
~~~~

Update the following fields within the script before running it and check the path to your elementsd and elements-cli:
You will need to update the following fields within the script before running it and check the path to your elementsd and elements-cli:

~~~
NAME
Expand All @@ -780,120 +776,7 @@ TOKEN_AMOUNT
PRECISION
~~~

For reference as to how the values should be set please refer to the <a href="https://docs.blockstream.com/liquid/developer-guide/developer-guide-index.html#proof-of-issuance-blockstream-s-liquid-asset-registry" target="_blank">Blockstream docs site</a>.

##### Note: The following will use the live Liquid network by default as the `datadir` is set to `.elements`. Assets issued on elementsregtest cannot be registered.

~~~~
#!/bin/bash
set -x
shopt -s expand_aliases
# We don't need to validate pegin for this example so no bitcoin node is used
alias e1-dae="$HOME/elements/src/elementsd -datadir=$HOME/.elements -validatepegin=0"
alias e1-cli="$HOME/elements/src/elements-cli -datadir=$HOME/.elements"
# We will hash using sha256sum if available, openssl otherwise (other options are available)
which sha256sum >/dev/null 2>&1 && alias sha256hash="sha256sum | sed 's/ .*//g'" || alias sha256hash="openssl dgst -sha256 | sed 's/.*= //g'"
# Ignore error
set +o errexit
# The following may error without issue if the daemon is not already running
e1-cli stop
sleep 15
# Start the daemon
e1-dae
sleep 15
# Make sure the node has finished startup and is responding to commands
until e1-cli getwalletinfo
do
echo "Waiting for e1 to finish loading..."
sleep 2
done
# Exit on error
set -o errexit
# We will be using the issueasset command and the contract_hash argument:
# issueasset <assetamount> <tokenamount> <blind> <contract_hash>
NAME="your asset name here"
TICKER="ticker here"
DOMAIN="domain.here"
ASSET_AMOUNT=100
TOKEN_AMOUNT=1
PRECISION=0
# Don't change the following:
VERSION=0
# As we need to sign the deletion request message later we need
# a legacy address. If you prefer to generate a pubkey and sign
# outside of Elements you can use a regular address instead.
NEWADDR=$(e1-cli getnewaddress "" legacy)
VALIDATEADDR=$(e1-cli getaddressinfo $NEWADDR)
PUBKEY=$(echo $VALIDATEADDR | jq -r '.pubkey')
ASSET_ADDR=$NEWADDR
NEWADDR=$(e1-cli getnewaddress "" legacy)
TOKEN_ADDR=$NEWADDR
# Create the contract and calculate the contract hash
# The contract is formatted for use in the Blockstream Asset Registry:
CONTRACT='{"entity":{"domain":"'$DOMAIN'"},"issuer_pubkey":"'$PUBKEY'","name":"'$NAME'","precision":'$PRECISION',"ticker":"'$TICKER'","version":'$VERSION'}'
CONTRACT_HASH=$(echo -n "${CONTRACT}" | sha256hash)
# Reverse the hash
TEMP=$CONTRACT_HASH
LEN=${#TEMP}
until [ $LEN -eq "0" ]; do
END=${TEMP:(-2)}
CONTRACT_HASH_REV="$CONTRACT_HASH_REV$END"
TEMP=${TEMP::$((${#TEMP} - 2))}
LEN=$((LEN-2))
done
# Wait for peers to connect (optional)
sleep 180
# Issue the asset and pass in the contract hash
IA=$(e1-cli issueasset $ASSET_AMOUNT $TOKEN_AMOUNT false $CONTRACT_HASH_REV)
# Details of the issuance...
ASSET=$(echo $IA | jq -r '.asset')
TOKEN=$(echo $IA | jq -r '.token')
ISSUETX=$(echo $IA | jq -r '.txid')
# Output the proof file - you need to place this on your domain.
# See the Blockstream docs link above for reference as to where.
echo "Authorize linking the domain name $DOMAIN to the Liquid asset $ASSET" > liquid-asset-proof-$ASSET
# Create the bash script to run after you have placed the proof file on your domain
# that will call the registry and request the asset is registered.
echo "curl https://assets.blockstream.info/ --data-raw '{\"asset_id\":\"$ASSET\",\"contract\":$CONTRACT}'" > register_asset-$ASSET.sh
# Create the bash script to delete the asset from the registry (if needed later)
PRIV=$(e1-cli dumpprivkey $ASSET_ADDR)
SIGNED=$(e1-cli signmessagewithprivkey $PRIV "remove $ASSET from registry")
echo "curl -X DELETE https://assets.blockstream.info/$ASSET -H 'Content-Type: application/json' -d '{\"signature\":\"$SIGNED\"}'" > delete_asset_$ASSET.sh
# Stop the daemon
e1-cli stop
sleep 10
echo "Completed without error"
~~~~
* * *

<a id="verify"></a>
### Example 4: Verify an output's asset and amount values using the blinding factors.
Expand Down
13 changes: 7 additions & 6 deletions elements-code-tutorial/confidential-transactions.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,9 @@ echo $ADDR

After running the echo command above you should see something similar to this:

<div class="console-output">Azpr7BwzjwdiB1pNZKcLkk6Esn5NWAE7wtrC4UzEsshpKe3eUZzPQBvfJ7q9wzJLbt9yn8hYZmZDayGG</div>
<div class="console-output">el1qqds73pp5sflsnrfstyf5y0n39esy3wfcca0e7u8q7kap2eg3z630pmgmljx9tlmv8urr0gtd842e9hfqta0ezke8p5jpfyx3e</div>

##### NOTE: As of Elements v0.17, getnewaddress defaults to creating P2SH-P2WPKH addresses. You can create 'CTE' prefixed addresses (the Elements pre-0.17 default) by calling getnewaddress like this: `e1-cli getnewaddress "" legacy`. You can also set the addresstype=legacy argument on node startup, or set it in your config file to always get legacy addresses from 'getnewaddress'. Some commands require a 'legacy' style address in order to work, such as message signing, as shown in the [Advanced examples]({{ site.url }}/elements-code-tutorial/advanced-examples) section.
##### NOTE: As of Elements v0.18, getnewaddress defaults to creating blinded bech32 addresses. You can create 'CTE' prefixed addresses (the Elements pre-0.17 default) by calling getnewaddress like this: `e1-cli getnewaddress "" legacy`. You can also set the addresstype=legacy argument on node startup, or set it in your config file to always get legacy addresses from 'getnewaddress'. Some commands require a 'legacy' style address in order to work, such as message signing, as shown in the [Advanced examples]({{ site.url }}/elements-code-tutorial/advanced-examples) section.

Let's look at the address in more detail to check that it is indeed a confidential one. To do this we can use the "getaddressinfo" command, passing in the address that we stored in the ADDR variable as a parameter:

Expand Down Expand Up @@ -111,7 +111,7 @@ Looking in the "details" section near the top, you will see that there are two a
...
},
{
....
...
"category": "receive",
"amount": 1.00000000,
...
Expand All @@ -136,9 +136,10 @@ e1-cli getrawtransaction $TXID 1
That returns raw transaction details. If you look within the "vout" section you can see that there are three instances. The first two instances are the receiving and change amounts and the third is the transaction fee. Of these three amounts, the fee is the only one in which you can see a value, as the fee itself is unblinded. For the first two instances you will see (amongst others) properties with values similar to this:

<div class="console-output">"value-minimum": 0.00000001,
"value-maximum": 11258999.06842624,
"amountcommitment": "0881c61d8a15ad26e6ef621ca99a188ccebbdb348d5285012393459b7e5b1e6113",
"assetcommitment": "0b1b7a1a4a604f4a68b3277e3a8926d74e86adce7b92e8e6ba67f9c5a8ad2cbcf4",
"value-maximum": 45035996.27370496,
...
"valuecommitment": "089dac0ee066b8c126ea69e2a9274cff2bc35d0bcd048e1e36baf4ffa74a80188d",
"assetcommitment": "0a80020aeca516f22abb6c952bd4e4d1eaa013fdde6f98289a2fb9f0b6dcd03142",
</div>

What this shows are the "blinded ranges" of the value amounts and the commitment data that acts as proof of the actual amount and type of asset transacted.
Expand Down
9 changes: 3 additions & 6 deletions elements-code-tutorial/easy-run-code.md
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ echo "The following 3 'rm' commands may error - that is fine."
rm -r ~/bitcoindir ; rm -r ~/elementsdir1 ; rm -r ~/elementsdir2
mkdir ~/bitcoindir ; mkdir ~/elementsdir1 ; mkdir ~/elementsdir2
echo "regtest=1
echo -n "regtest=1
txindex=1
daemon=1
rpcuser=user3
Expand All @@ -102,10 +102,9 @@ fallbackfee=0.0002
[regtest]
rpcport=18888
port=18889
" > ~/bitcoindir/bitcoin.conf
echo "chain=elementsregtest
echo -n "chain=elementsregtest
rpcuser=user1
rpcpassword=password1
daemon=1
Expand All @@ -123,10 +122,9 @@ rpcport=18884
port=18886
anyonecanspendaremine=1
connect=localhost:18887
" > ~/elementsdir1/elements.conf
echo "chain=elementsregtest
echo -n "chain=elementsregtest
rpcuser=user2
rpcpassword=password2
daemon=1
Expand All @@ -143,7 +141,6 @@ rpcport=18885
port=18887
anyonecanspendaremine=1
connect=localhost:18886
" > ~/elementsdir2/elements.conf
b-dae
Expand Down
4 changes: 2 additions & 2 deletions elements-code-tutorial/issuing-assets.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ e1-cli getwalletinfo
We see that Alice holds a lot of the "bitcoin" asset and nothing else:

<div class="console-output">"balance": {
"bitcoin": 10500001.00000000
"bitcoin": 10500000.00000000
</div>

Every asset you issue within Elements (including the "bitcoin" default) will be assigned its own hex value. This is used to uniquely identify it on the network. Notice how "bitcoin" is displayed with a readable asset name however. This is because Elements automatically associates the label "bitcoin" with the asset hex for that default asset. To find out its hex value we can run:
Expand Down Expand Up @@ -141,7 +141,7 @@ IADDR=$(e1-cli gettransaction $ITXID | jq -r '.details[0].address')
e2-cli importaddress $IADDR
~~~~

Another way to make Bob's node aware of the issuance is for Bob to get the issuance transaction ID and use that to import any output address from the transaction into his wallet. This is useful if Bob is not able to get the adress from Alice, but knows the transaction in which the asset was issued... perhaps by using the [Blockstream Explorer's assets list](https://blockstream.info/liquid/assets/) to look it up. From that page he can either use the TXID or select one of the addresses from the outputs. Using the TXID requires that Bob's node has ``index=1`` set in the elements.conf file.
Another way to make Bob's node aware of the issuance is for Bob to get the issuance transaction ID and use that to import any output address from the transaction into his wallet. This is useful if Bob is not able to get the address from Alice, but knows the transaction in which the asset was issued... perhaps by using the [Blockstream Explorer's assets list](https://blockstream.info/liquid/assets/) to look it up. From that page he can either use the TXID or select one of the addresses from the outputs. Using the TXID requires that Bob's node has ``index=1`` set in the elements.conf file.

Bob's already imported the address above but for reference the code to import using TXID is shown below. It doesn't matter which address is used, so we will use the first instance:

Expand Down
2 changes: 1 addition & 1 deletion elements-code-tutorial/reissuing-assets.md
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ e2-cli decoderawtransaction $RAWRTRANS
We can see that the amounts and asset types are indeed blinded with results like this:

<div class="console-output">"value-minimum": 0.00000001,
"value-maximum": 42.94967296,
"value-maximum": 45035996.27370496,
</div>

You could unblind these using the techniques we used for the initial issuance should you want to.
Expand Down
43 changes: 25 additions & 18 deletions elements-code-tutorial/sidechain.md
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,21 @@ CLAIMTXID=$(e1-cli claimpegin $RAW $PROOF $CLAIMSCRIPT)

##### NOTE: **The time between generating a peg-in address with "getpeginaddress" and claiming it with "claimpegin" should be kept as small as possible.**

Remember that fees will also have been deducted on the sidechain from the amount received above.

As the wallet started with 21 million bitcoin it should have nearly 1 more now (1 minus sidechain fees). To check:

~~~~
e1-cli getwalletinfo
~~~~

Which returns the claimed amount in the "unconfirmed_balance" value (minus fee)::

<div class="console-output">"unconfirmed_balance": {
"bitcoin": 0.99992800
},
</div>

Bob's node (as well as Alice's of course) should accept the claim transaction as valid and add it to its mempool. Create a block containing the transaction:

~~~~
Expand All @@ -174,21 +189,6 @@ The output of which can be seen below. Note that the asset hex may differ from t
"asset": "b7c9431837115ba3b8a1753dc227311ab4480c14d97484234f984d361a00c966",
</div>

Remember that fees will also have been deducted on the sidechain from the amount received above.

As the wallet started with 21 million bitcoin it should have nearly 1 more now (1 minus sidechain fees). To check:

~~~~
e1-cli getwalletinfo
~~~~

Which returns the claimed amount in the "unconfirmed_balance" value (minus fee)::

<div class="console-output">"unconfirmed_balance": {
"bitcoin": 0.99992800
},
</div>

Now that we have sent assets into our sidechain (peg-in) we will now peg-out and send assets representing bitcoin back from our sidechain to the main chain:

~~~~
Expand All @@ -204,16 +204,23 @@ The results of which show:
},
</div>

Which is the amount before peg-out plus the (now confirmed) "unconfirmed_balance" balance we saw above, minus the 1 we pegged out. Remember that sidechain fees have also been deducted so we return to just under the original 21 million. These fees actually show in the "immature_balance" for Alice's wallet, as she mined the block and collected her own fees:
Which is the amount before peg-in plus the (now confirmed) "unconfirmed_balance" balance we saw above, minus the 1 we pegged out. Remember that sidechain fees have also been deducted so we return to just under the original 21 million. These fees actually show in the "immature_balance" for Alice's wallet, as she mined the block and collected her own fees:

<div class="console-output">”immature_balance”: {
“bitcoin”: 0.00005760
},
</div>

Similarly, for Bob's wallet:

<div class="console-output">”immature_balance”: {
“bitcoin”: 0.00012540
“bitcoin”: 0.00006780
},
</div>

Checking this manually:

<div class="console-output">20999999.99987460 + 0.00012540 = 21000000
<div class="console-output">20999999.99987460 + 0.00005760 + 0.00006780 = 21000000
</div>


Expand Down
9 changes: 3 additions & 6 deletions elements-code-tutorial/working-environment.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ We need to set up our config files now. We'll do that by writing some settings t

The Bitcoin config file:
~~~~
echo "regtest=1
echo -n "regtest=1
txindex=1
daemon=1
rpcuser=user3
Expand All @@ -38,13 +38,12 @@ fallbackfee=0.0002
[regtest]
rpcport=18888
port=18889
" > ~/bitcoindir/bitcoin.conf
~~~~

Elements 1 config file:
~~~
echo "chain=elementsregtest
echo -n "chain=elementsregtest
rpcuser=user1
rpcpassword=password1
daemon=1
Expand All @@ -62,13 +61,12 @@ rpcport=18884
port=18886
anyonecanspendaremine=1
connect=localhost:18887
" > ~/elementsdir1/elements.conf
~~~

Elements 2 config file:
~~~
echo "chain=elementsregtest
echo -n "chain=elementsregtest
rpcuser=user2
rpcpassword=password2
daemon=1
Expand All @@ -85,7 +83,6 @@ rpcport=18885
port=18887
anyonecanspendaremine=1
connect=localhost:18886
" > ~/elementsdir2/elements.conf
~~~

Expand Down

0 comments on commit 26f60be

Please sign in to comment.