5-Minute Guide
Overview
This guide will walk you through the basic functions of Chain Core:
- Initialize the SDK
- Create keys (in the Chain Core Mock HSM)
- Initialize the HSM Signer
- Create an asset
- Create an account
- Issue asset units into an account
- Spend asset units from one account to another
- Retire asset units from an account
Sample Code
All code samples in this guide can be viewed in a single, runnable script. Available languages:
Initialize the SDK
Create an instance of the SDK. By default, the SDK will try to access a core located at http://localhost:1999
, which is the default if you’re running Chain Core locally.
Client client = new Client();
chain = Chain::Client.new
const client = new chain.Client()
- Java
- Node
- Ruby
Create Keys
Create a new key in the Mock HSM.
MockHsm.Key key = MockHsm.Key.create(client);
key = chain.mock_hsm.keys.create
const keyPromise = client.mockHsm.keys.create()
- Java
- Node
- Ruby
Initialize the HSM Signer
To be able to sign transactions, load the key into the HSM Signer, which will communicate with the Mock HSM.
HsmSigner.addKey(key, MockHsm.getSignerClient(client));
signer = Chain::HSMSigner.new
signer.add_key(key, chain.mock_hsm.signer_conn)
const signer = new chain.HsmSigner()
signer.addKey(key.xpub, client.mockHsm.signerConnection)
- Java
- Node
- Ruby
Create an Asset
Create a new asset, providing an alias, key, and quorum. The quorum is the threshold of keys that must sign a transaction issuing units of the asset.
new Asset.Builder()
.setAlias("gold")
.addRootXpub(key.xpub)
.setQuorum(1)
.create(client);
chain.assets.create(
alias: :gold,
root_xpubs: [key.xpub],
quorum: 1
)
const goldPromise = client.assets.create({
alias: 'gold',
rootXpubs: [key.xpub],
quorum: 1,
})
- Java
- Node
- Ruby
Create an Account
Create an account, providing an alias, key, and quorum. The quorum is the threshold of keys that must sign a transaction to spend asset units controlled by the account.
new Account.Builder()
.setAlias("alice")
.addRootXpub(key.xpub)
.setQuorum(1)
.create(client);
chain.accounts.create(
alias: :alice,
root_xpubs: [key.xpub],
quorum: 1
)
const alicePromise = client.accounts.create({
alias: 'alice',
rootXpubs: [key.xpub],
quorum: 1
})
- Java
- Node
- Ruby
Create a second account to interact with the first account.
new Account.Builder()
.setAlias("bob")
.addRootXpub(key.xpub)
.setQuorum(1)
.create(client);
chain.accounts.create(
alias: :bob,
root_xpubs: [key.xpub],
quorum: 1
)
const bobPromise = client.accounts.create({
alias: 'bob',
rootXpubs: [key.xpub],
quorum: 1
})
- Java
- Node
- Ruby
Issue Asset Units
Build, sign, and submit a transaction that issues new units of the gold
asset into the alice
account.
Transaction.Template issuance = new Transaction.Builder()
.addAction(new Transaction.Action.Issue()
.setAssetAlias("gold")
.setAmount(100)
).addAction(new Transaction.Action.ControlWithAccount()
.setAccountAlias("alice")
.setAssetAlias("gold")
.setAmount(100)
).build(client);
Transaction.submit(client, HsmSigner.sign(issuance));
issuance = chain.transactions.build do |b|
b.issue asset_alias: :gold, amount: 100
b.control_with_account account_alias: :alice, asset_alias: :gold, amount: 100
end
chain.transactions.submit(signer.sign(issuance))
client.transactions.build(builder => {
builder.issue({
assetAlias: 'gold',
amount: 100
})
builder.controlWithAccount({
accountAlias: 'alice',
assetAlias: 'gold',
amount: 100
})
}).then(issuance => {
return signer.sign(issuance)
}).then(signed => {
return client.transactions.submit(signed)
})
- Java
- Node
- Ruby
Spend Asset Units
Build, sign, and submit a transaction that spends units of the gold
asset from the alice
account to the bob
account.
Transaction.Template spending = new Transaction.Builder()
.addAction(new Transaction.Action.SpendFromAccount()
.setAccountAlias("alice")
.setAssetAlias("gold")
.setAmount(10))
.addAction(new Transaction.Action.ControlWithAccount()
.setAccountAlias("bob")
.setAssetAlias("gold")
.setAmount(10)
).build(client);
Transaction.submit(client, HsmSigner.sign(spending));
spending = chain.transactions.build do |b|
b.spend_from_account account_alias: :alice, asset_alias: :gold, amount: 10
b.control_with_account account_alias: :bob, asset_alias: :gold, amount: 10
end
chain.transactions.submit(signer.sign(spending))
client.transactions.build(builder => {
builder.spendFromAccount({
accountAlias: 'alice',
assetAlias: 'gold',
amount: 10
})
builder.controlWithAccount({
accountAlias: 'bob',
assetAlias: 'gold',
amount: 10
})
}).then(issuance => {
return signer.sign(issuance)
}).then(signed => {
return client.transactions.submit(signed)
})
- Java
- Node
- Ruby
Retire Asset Units
Build, sign, and submit a transaction that retires units of the gold
asset from the bob
account.
Transaction.Template retirement = new Transaction.Builder()
.addAction(new Transaction.Action.SpendFromAccount()
.setAccountAlias("bob")
.setAssetAlias("gold")
.setAmount(5)
).addAction(new Transaction.Action.Retire()
.setAssetAlias("gold")
.setAmount(5)
).build(client);
Transaction.submit(client, HsmSigner.sign(retirement));
retirement = chain.transactions.build do |b|
b.spend_from_account account_alias: :bob, asset_alias: :gold, amount: 5
b.retire asset_alias: :gold, amount: 5
end
chain.transactions.submit(signer.sign(retirement))
client.transactions.build(builder => {
builder.spendFromAccount({
accountAlias: 'alice',
assetAlias: 'gold',
amount: 5
})
builder.retire({
assetAlias: 'gold',
amount: 5
})
}).then(issuance => {
return signer.sign(issuance)
}).then(signed => {
return client.transactions.submit(signed)
})
- Java
- Node
- Ruby