Skip to content

Commit

Permalink
first pass at Create Addresses
Browse files Browse the repository at this point in the history
  • Loading branch information
nothingalike committed Aug 5, 2022
1 parent 0386b4a commit ab6afb8
Showing 1 changed file with 357 additions and 0 deletions.
357 changes: 357 additions & 0 deletions 03-Create-Addresses.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,357 @@
{
"cells": [
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"dotnet_interactive": {
"language": "csharp"
},
"vscode": {
"languageId": "dotnet-interactive.csharp"
}
},
"outputs": [
{
"data": {
"text/html": [
"<div><div></div><div></div><div><strong>Installed Packages</strong><ul><li><span>CardanoSharp.Wallet, 2.14.0</span></li></ul></div></div>"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"#r \"nuget:CardanoSharp.Wallet,2.14.0\""
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Create Addresses\n",
"\n",
"Once you have your keys, you will want to generate addresses. There are a few different addresses you may want to create.\n",
"\n",
"Supported Address Types: \n",
" - Delegation/Base\n",
" - Enterprise\n",
" - Rewards/Stake\n",
" - Deletegation/Base Shared\n",
" - Enterprised Shared"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Delegation/Base Address\n",
"\n",
"This address type is the most common. Since this address type requires the Stake keys along side the External or Internal keys, funds sent to these addresses will automatically contribute to the wallets staking."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"dotnet_interactive": {
"language": "csharp"
},
"vscode": {
"languageId": "dotnet-interactive.csharp"
}
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Delegation Address: addr_test1qrjyfzy6p8n42zmz4hke3e5gg6nc70cts20qqn8dthtknpx8qwa3numg7kppuecp90xe32r5n9cxrpqg05uknjasl98ssnlc27\r\n"
]
}
],
"source": [
"using CardanoSharp.Wallet;\n",
"using CardanoSharp.Wallet.Enums;\n",
"using CardanoSharp.Wallet.Models.Keys;\n",
"using CardanoSharp.Wallet.Models.Addresses;\n",
"using CardanoSharp.Wallet.Extensions.Models;\n",
"using CardanoSharp.Wallet.Models.Derivations;\n",
"\n",
"IAccountNodeDerivation accountNode = new MnemonicService()\n",
" .Generate(24)\n",
" .GetMasterNode()\n",
" .Derive(PurposeType.Shelley)\n",
" .Derive(CoinType.Ada)\n",
" .Derive(0);\n",
"\n",
"IIndexNodeDerivation paymentNode = accountNode\n",
" .Derive(RoleType.ExternalChain)\n",
" .Derive(0);\n",
"paymentNode.SetPublicKey();\n",
"\n",
"IIndexNodeDerivation stakingNode = accountNode\n",
" .Derive(RoleType.Staking)\n",
" .Derive(0);\n",
"stakingNode.SetPublicKey();\n",
"\n",
"Address delegationAddress = new AddressService()\n",
" .GetBaseAddress(\n",
" paymentNode.PublicKey,\n",
" stakingNode.PublicKey,\n",
" NetworkType.Testnet);\n",
"Console.WriteLine($\"Delegation Address: {delegationAddress.ToString()}\");"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Enterprise Address\n",
"\n",
"The enterprise address utilizes only the External or Internal keys. Any funds sent here will not be able to be staked."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"dotnet_interactive": {
"language": "csharp"
},
"vscode": {
"languageId": "dotnet-interactive.csharp"
}
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Enterprise Address: addr_test1vrjyfzy6p8n42zmz4hke3e5gg6nc70cts20qqn8dthtknpq06mzkw\r\n"
]
}
],
"source": [
"// We are going to utilize the wallet created above\n",
"\n",
"Address enterpriseAddress = new AddressService()\n",
" .GetEnterpriseAddress(\n",
" paymentNode.PublicKey,\n",
" NetworkType.Testnet);\n",
"Console.WriteLine($\"Enterprise Address: {enterpriseAddress.ToString()}\");"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Staking Address\n",
"\n",
"The staking address is also known as the reward address. "
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"dotnet_interactive": {
"language": "csharp"
},
"vscode": {
"languageId": "dotnet-interactive.csharp"
}
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Staking/Reward Address: stake_test1urrs8wce7d50tqs7vuqjhnvc4p6fjurpssy86wtfewc0jncjkrm3k\r\n"
]
}
],
"source": [
"// Again utilizely the wallet created above\n",
"\n",
"Address stakingAddress = new AddressService()\n",
" .GetRewardAddress(\n",
" stakingNode.PublicKey,\n",
" NetworkType.Testnet);\n",
"Console.WriteLine($\"Staking/Reward Address: {stakingAddress.ToString()}\");"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Shared/Script Addresses\n",
"\n",
"Shared/Script Addresses come out of CIP1845 (Multi-signature HD Wallets). These are the type of wallets that can have different rules among a collection of keys in order to spend funds. A good example would be a Treasury.\n",
"\n",
"### Delegation Shared/Script Address\n",
"\n",
"Lets start by creating a simple delegation script address that can be signed by one of two keys."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"dotnet_interactive": {
"language": "csharp"
},
"vscode": {
"languageId": "dotnet-interactive.csharp"
}
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Payment Policy Id: script1cpqa5wphg30n29lfptpm7959hnthm2lfkta0rp2g4cxu7l7tk6q\r\n",
"Stake Policy Id: script12nvu737l7s579tza8wllumf8jtfd25qfx2ngsj5j65kyg36gnem\r\n",
"Shared/Script Address: addr_test1xrqyrk3cxaz97dghay9v80cksk7dwldtaxe04uv9fzhqmn65m8850hl59832chfmhllx6fuj6t24qzfj56yy4yk493zqrcg27f\r\n"
]
}
],
"source": [
"using CardanoSharp.Wallet.Encoding;\n",
"using CardanoSharp.Wallet.Utilities;\n",
"using CardanoSharp.Wallet.Extensions;\n",
"using CardanoSharp.Wallet.Models.Transactions.Scripts;\n",
"using CardanoSharp.Wallet.TransactionBuilding;\n",
"\n",
"// Lets setup some wallets\n",
"Mnemonic mnemonic1 =\n",
"new MnemonicService().Restore(\"scale fiction sadness render fun system hunt skull awake neither quick uncle grab grid credit\");\n",
"\n",
"Mnemonic mnemonic2 =\n",
"new MnemonicService().Restore(\"harsh absorb lazy resist elephant social carry roof remember picture merry enlist regret major practice\");\n",
"\n",
"// Derive all the needed keys\n",
"// First lets derive all the Payment Keys for both wallets\n",
"IIndexNodeDerivation paymentNode1 = mnemonic1.GetMasterNode()\n",
" .Derive(PurposeType.MultiSig)\n",
" .Derive(CoinType.Ada)\n",
" .Derive(0)\n",
" .Derive(RoleType.ExternalChain)\n",
" .Derive(0);\n",
"paymentNode1.SetPublicKey();\n",
"IIndexNodeDerivation paymentNode2 = mnemonic2.GetMasterNode()\n",
" .Derive(PurposeType.MultiSig)\n",
" .Derive(CoinType.Ada)\n",
" .Derive(0)\n",
" .Derive(RoleType.ExternalChain)\n",
" .Derive(0);\n",
"paymentNode2.SetPublicKey();\n",
"\n",
"PublicKey paymentPub1 = paymentNode1.PublicKey;\n",
"PublicKey paymentPub2 = paymentNode2.PublicKey;\n",
" \n",
"// Next lets derive all the Staking Keys for both wallets\n",
"IIndexNodeDerivation stakeNode1 = mnemonic1.GetMasterNode()\n",
" .Derive(PurposeType.MultiSig)\n",
" .Derive(CoinType.Ada)\n",
" .Derive(0)\n",
" .Derive(RoleType.Staking)\n",
" .Derive(0);\n",
"stakeNode1.SetPublicKey();\n",
"IIndexNodeDerivation stakeNode2 = mnemonic2.GetMasterNode()\n",
" .Derive(PurposeType.MultiSig)\n",
" .Derive(CoinType.Ada)\n",
" .Derive(0)\n",
" .Derive(RoleType.Staking)\n",
" .Derive(0);\n",
"stakeNode2.SetPublicKey();\n",
"\n",
"PublicKey stakePub1 = stakeNode1.PublicKey;\n",
"PublicKey stakePub2 = stakeNode2.PublicKey;\n",
"\n",
"// Generate payment hashes\n",
"byte[] paymentHash1 = HashUtility.Blake2b224(paymentPub1.Key);\n",
"byte[] paymentHash2 = HashUtility.Blake2b224(paymentPub2.Key);\n",
"\n",
"// Generate stake hashes\n",
"byte[] stakeHash1 = HashUtility.Blake2b224(stakePub1.Key);\n",
"byte[] stakeHash2 = HashUtility.Blake2b224(stakePub2.Key);\n",
"\n",
"// Create a Payment Policy Script with a type of Script Any\n",
"ScriptAny paymentPolicyScript = ScriptAnyBuilder.Create\n",
" .SetScript(NativeScriptBuilder.Create.SetKeyHash(paymentHash1))\n",
" .SetScript(NativeScriptBuilder.Create.SetKeyHash(paymentHash2))\n",
" .Build();\n",
" byte[] paymentPolicyId = paymentPolicyScript.GetPolicyId();\n",
"string bechPaymentPolicyId = Bech32.Encode(paymentPolicyId, \"script\");\n",
"Console.WriteLine($\"Payment Policy Id: {bechPaymentPolicyId}\");\n",
"\n",
"// Create a Stake Policy Script with a type of Script Any\n",
"ScriptAny stakePolicyScript = ScriptAnyBuilder.Create\n",
" .SetScript(NativeScriptBuilder.Create.SetKeyHash(stakeHash1))\n",
" .SetScript(NativeScriptBuilder.Create.SetKeyHash(stakeHash2))\n",
" .Build();\n",
"byte[] statkePolicyId = stakePolicyScript.GetPolicyId();\n",
"string bechStakePolicyId = Bech32.Encode(statkePolicyId, \"script\");\n",
"Console.WriteLine($\"Stake Policy Id: {bechStakePolicyId}\");\n",
" \n",
"//Generate Address\n",
"Address delegationScriptAddress = new AddressService().GetBaseScriptAddress(paymentPolicyScript, stakePolicyScript, NetworkType.Testnet);\n",
"Console.WriteLine($\"Shared/Script Address: {delegationScriptAddress.ToString()}\");"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Enterprise Shared/Script Address\n",
"\n",
"Just like a normal enterprise address, we just need to remove the staking piece to generate the address."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"dotnet_interactive": {
"language": "csharp"
},
"vscode": {
"languageId": "dotnet-interactive.csharp"
}
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Shared/Script Address: addr_test1wp2dnn68ml6znc4vt5amllndy7fd942spye2dzz2jt2jc3qcu6kls\r\n"
]
}
],
"source": [
"//Generate Address\n",
"Address enterpriseScriptAddress = new AddressService().GetEnterpriseScriptAddress(stakePolicyScript, NetworkType.Testnet);\n",
"Console.WriteLine($\"Shared/Script Address: {enterpriseScriptAddress.ToString()}\");"
]
}
],
"metadata": {
"kernelspec": {
"display_name": ".NET (C#)",
"language": "C#",
"name": ".net-csharp"
},
"language_info": {
"file_extension": ".cs",
"mimetype": "text/x-csharp",
"name": "C#",
"pygments_lexer": "csharp",
"version": "9.0"
},
"orig_nbformat": 4
},
"nbformat": 4,
"nbformat_minor": 2
}

0 comments on commit ab6afb8

Please sign in to comment.