diff --git a/examples/create_sub_org/.env.example b/examples/create_sub_org/.env.example new file mode 100644 index 0000000..485fb49 --- /dev/null +++ b/examples/create_sub_org/.env.example @@ -0,0 +1,8 @@ +# If you do not have an organization, create one at https://app.turnkey.com/dashboard/auth/initial +TURNKEY_ORGANIZATION_ID= +# API credentials. They need to be attached to a user in your organization. +# These credentials must be a valid hex-encoded public/private key pair (P256) +TURNKEY_API_PUBLIC_KEY= +TURNKEY_API_PRIVATE_KEY= +# This will be used as a public key when creating the sub-organization (it will be the target authenticator for the root user!) +SUB_ORGANIZATION_ROOT_PUBLIC_KEY= diff --git a/examples/create_sub_org/Gemfile b/examples/create_sub_org/Gemfile new file mode 100644 index 0000000..7f3948d --- /dev/null +++ b/examples/create_sub_org/Gemfile @@ -0,0 +1,4 @@ +source 'https://rubygems.org' + +gem 'dotenv' +gem 'turnkey_client', path: '../../turnkey_client/' diff --git a/examples/create_sub_org/Gemfile.lock b/examples/create_sub_org/Gemfile.lock new file mode 100644 index 0000000..8525974 --- /dev/null +++ b/examples/create_sub_org/Gemfile.lock @@ -0,0 +1,29 @@ +PATH + remote: ../../turnkey_client + specs: + turnkey_client (0.0.2) + json (~> 2.1, >= 2.1.0) + openssl (~> 3.2, >= 3.0.0) + typhoeus (~> 1.0, >= 1.0.1) + +GEM + remote: https://rubygems.org/ + specs: + dotenv (3.0.2) + ethon (0.16.0) + ffi (>= 1.15.0) + ffi (1.16.3) + json (2.7.1) + openssl (3.2.0) + typhoeus (1.4.1) + ethon (>= 0.9.0) + +PLATFORMS + arm64-darwin-23 + +DEPENDENCIES + dotenv + turnkey_client! + +BUNDLED WITH + 2.2.3 diff --git a/examples/create_sub_org/README.md b/examples/create_sub_org/README.md new file mode 100644 index 0000000..7d0fa00 --- /dev/null +++ b/examples/create_sub_org/README.md @@ -0,0 +1,18 @@ +# Who am I? + +This examples shows how to use [`turnkey_client`](https://rubygems.org/gems/turnkey_client) to create a new sub-organization and a wallet. We generate a new Tron account (default account) + +To run this example: +* `cp .env.example .env` +* Follow the instructions in the `.env` file to fill in values for `TURNKEY_ORGANIZATION_ID`, `TURNKEY_API_PUBLIC_KEY`, `TURNKEY_API_PRIVATE_KEY`, and `SUB_ORGANIZATION_ROOT_PUBLIC_KEY` +* Install the dependencies: `bundle install` +* Then run the `create_sub_org.rb` script: `bundle exec ruby create_sub_org.rb` + +Here's an example run: + +``` +bundle exec ruby create_sub_org.rb +Created new Turnkey sub-organization successfully +Sub-Organization ID: 122da99b-db34-44ed-84ee-e326c5b149ec +Tron wallet address: TZFeFMTfJKtkvHMhyveuYXSxKReBbmUiZw +``` diff --git a/examples/create_sub_org/create_sub_org.rb b/examples/create_sub_org/create_sub_org.rb new file mode 100644 index 0000000..cb49c6e --- /dev/null +++ b/examples/create_sub_org/create_sub_org.rb @@ -0,0 +1,69 @@ +require 'turnkey_client' +require 'dotenv' +require 'json' +require 'date' + +# Load local .env file +Dotenv.load + +raise 'Please set TURNKEY_ORGANIZATION_ID in your .env file' if ENV['TURNKEY_ORGANIZATION_ID'].nil? +raise 'Please set SUB_ORGANIZATION_ROOT_PUBLIC_KEY in your .env file' if ENV['SUB_ORGANIZATION_ROOT_PUBLIC_KEY'].nil? + +# Make a whoami request +begin + client = TurnkeyClient.configure do |c| + c.api_public_key = ENV['TURNKEY_API_PUBLIC_KEY'] + c.api_private_key = ENV['TURNKEY_API_PRIVATE_KEY'] + end + + now_in_ms = DateTime.now.strftime('%Q') + + # https://docs.turnkey.com/api#tag/Organizations/operation/CreateSubOrganization + create_sub_organization_response = TurnkeyClient::OrganizationsApi.new(client).create_sub_organization( + { + type: 'ACTIVITY_TYPE_CREATE_SUB_ORGANIZATION_V4', + organizationId: ENV['TURNKEY_ORGANIZATION_ID'], + timestampMs: now_in_ms, + parameters: { + subOrganizationName: "Sub-Organization @#{now_in_ms}", + rootUsers: [ + { + userName: "Root User", + apiKeys: [{ + apiKeyName: "Root User API key", + publicKey: ENV['SUB_ORGANIZATION_ROOT_PUBLIC_KEY'], + }], + authenticators: [], + userEmail: nil, + } + ], + rootQuorumThreshold: 1, + wallet: { + walletName: "Tron Wallet", + mnemonicLength: 12, + accounts: [ + { + curve: 'CURVE_SECP256K1', + pathFormat: 'PATH_FORMAT_BIP32', + # Default Tron account in the standard: https://github.com/tronprotocol/tips/blob/master/tip-01.md + path: "m/44'/195'/0'", + addressFormat: 'ADDRESS_FORMAT_TRON' + } + ] + } + } + } + ) + + created_sub_organization = create_sub_organization_response&.activity&.dig(:result, :createSubOrganizationResultV4) + # TODO + raise "Something went wrong: no sub-organization in activity response: #{create_sub_organization_response.to_hash}" if created_sub_organization.nil? + + sub_organization_id = created_sub_organization[:subOrganizationId] + tron_address = created_sub_organization[:wallet][:addresses].first + puts 'Created new Turnkey sub-organization successfully' + puts "Sub-Organization ID: #{sub_organization_id}" + puts "Tron wallet address: #{tron_address}" +rescue TurnkeyClient::ApiError => e + puts "Exception when calling create sub-organization endpoint: #{e}" +end