[KAS SDK] KASWallet, a wallet integrated with KAS API

Tech at Klaytn
Klaytn
Published in
16 min readMay 2, 2021

See the list of articles here.
🇰🇷: KAS API와 통합된 지갑, KASWallet

KAS SDK v1.0.2 features KASWallet, which is integrated with KAS Wallet API. KASWallet allows you to use all the convenient features previously provided by caver with Klaytn accounts stored in KAS Wallet API.

In this article, we will explain what KASWallet is and how to use it.

1. What is KASWallet?

KASWallet is a wallet designed to do what KeyringContainer, an in-memory wallet provided by caver.wallet used to do, via KAS Wallet API.

Caver’s caver.contract, caver.kct.kip7 and caver.kct.kip17 use caver.wallet to sign and send transactions to the network. With the previous KAS SDK, using features provided by caver.contract, caver.kct.kip7 or caver.kct.kip17 with Klaytn accounts stored on KAS Wallet API has had some limitations.

Starting from KAS SDK v1.0.2, KASWallet is provided through caver.wallet, so that you can deploy and execute contracts using your Klaytn accounts stored on the KAS Wallet API. You can also sign transactions provided by caver using Klaytn accounts stored on the KAS Wallet API.

2. Structure of KASWallet

Above is a diagram for explaining KASWallet.

KeyringContainer and KASWallet implement the IWallet interface respectively, and Caver defines the KeyringContainer instance to the wallet member variable, and CaverExtKAS defines the KASWallet instance to thewallet.

The getter for the member parameter wallet, getWallet, must be implemented so that both Caver and CaverExtKAS return KeyringContainer or KASWallet instance, which is a class implementation of IWallet interface, via getWallet. All the necessary functions required for Contract, KIP7 and KIP17 are defined in IWallet.

3. Using caver.contract with KAS Wallet API‘s Klaytn

Since caver.wallet is the KASWallet for KAS SDK, using caver.contract on KAS SDK allows you to deploy and execute contracts on Klaytn using Klaytn accounts stored in KAS Wallet API. Using caver.contract with your Klaytn accounts in KAS Wallet API is the same as that for using caver.contract with caver.

Here we will demonstrate in a simple way how you can deploy and execute smart contracts. The Klaytn accounts used here for deploying and executing smart contracts are the accounts that are managed by KAS Wallet API. These Klaytn accounts need to hold enough KLAY balance to send transactions.

3.1. caver-js-ext-kas

Using caver.contract of caver-js-ext-kas is the same as using caver.contract of caver-js.

3.1.1. Deploying smart contract

We will show you how you can deploy smart contracts using caver-js-ext-kas.

const contract = new caver.contract(abi)

First of all, create a contract instance using caver.contract.

const deployed = await contract.deploy({ data: byteCode }).send({ from: ‘0x{the address of a Klaytn account in KAS Wallet API}’, gas: 10000000 })

And then call the deploy method from the contract instance. deploy returns an object a with the send function, which sends a transaction to the network.

The transaction will only be sent to the network when you call the send function of the returned object. The tutorial defines fromand gas as parameter objects, which are passed when calling the send function. The from address needs to be the address of the Klaytn accounts stored in KAS Wallet API.

Executing the above codes will create a SmartContractDeploy transaction internally and send it to the network after signing it with the from account stored on KAS Wallet API. As a result, the contract instance containing the information of the deployed contract will be returned.

3.1.2. Executing contract

Here is how you execute a smart contract on Klaytn using caver-js-ext-kas:

const contractAddress = ‘0x5D3Cd9eB73f00BbECB949DCE08BB26019FcB599f’
const contract = new caver.contract(abi, contractAddress)

Just as for deploying, you first create a contract instance in order to execute a smart contract. The parameters to be passed to the constructors are the ABI of the smart contract and the contract address.

const receipt = await contract.methods.set(‘key’, ‘value’).send({ from: ‘0x{the address of a Klaytn account in KAS Wallet API}’, gas: 5000000 })

The created contract can use the functions defined in the smart contract by calling methods. After that, you can use the functions defined in the contract using contract.methods. Just as in the previous example, calling each function will return an object in which the send function for sending transactions to the network is defined.

Call send to the returned object to execute the smart contract. from and gas are defined in the object parameters that are passed to send, and from must be a Klaytn account address stored in KAS Wallet API.

Executing the above codes will create a SmartContractExecution transaction and send it to the network after signing it with the from account stored on KAS Wallet API.

3.2. caver-java-ext-kas

3.2.1. Deploying smart contract

This section explains how to deploy a smart contract using caver-java-ext-kas.

CaverExtKAS caver = new CaverExtKAS(ChainId.BAOBAB_TESTNET, “your_access_key”, “your_secret_access_key”);
Contract contract = new Contract(caver, ABI);

First, create a Contract instance.

SendOptions sendOptions = new SendOptions(“0x{the address of a Klaytn account in KAS Wallet API}”, BigInteger.valueOf(10000000));
Contract deployed = contract.deploy(sendOptions, BINARY);

Next, call the deploy method of the contract instance. Pass SendOptions, contract’s bytecodes, and the contract’s constructor parameter as the parameters for deploy. Since the above code doesn’t have a parameter for the smart contract’s constructors to be deployed, just pass sendOptions and BINARY as parameters.

In the above example, from and gas have been defined in SendOptions. The from address should be the Klaytn account address stored in KAS Wallet API.

Executing the above codes will create a SmartContractDeploy transaction internally and send it to the network after signing it with the from account stored on KAS Wallet API. As a result, the contract instance containing the information of the deployed contract will be returned.

3.2.2. Executing smart contract

Here is how you execute a smart contract deployed on Klaytn using caver-js-ext-kas.

CaverExtKAS caver = new CaverExtKAS(ChainId.BAOBAB_TESTNET, “your_access_key”, “your_secret_access_key”);
String address = “Your contract address”;
Contract contract = new Contract(caver, ABI, address);

Just as for deploying, create a contract instance in order to execute a smart contract.

SendOptions sendOptions = new SendOptions(“0x{the address of a Klaytn account in KAS Wallet API}”, BigInteger.valueOf(5000000));
TransactionReceipt.TransactionReceiptData receiptData
= contract.send(sendOptions,”set”, “key”, “value”);

The contract defines a send function for the execution of the smart contract. When calling the send function, the first parameter to pass is SendOptions, which is necessary for executing the contract, and then the name of the contract, followed by the function parameters of the contract.

Executing the above codes will create a SmartContractDeploy transaction internally and send it to the network after signing it with the from account stored on KAS Wallet API. As a result, the transaction receipt will be returned.

4. Using caver.kct with Klaytn account in KAS Wallet API

Using caver.kct with the Klaytn accounts stored in KAS Wallet API is the same as using caver.kct with caver.

In this tutorial, we will explain how you can deploy and execute token contracts for KIP-7 and KIP-17. The Klaytn accounts used here for deploying and executing token contracts are managed by KAS Wallet API, which must hold enough KLAY to send transactions.

4.1. caver-js-ext-kas

Using caver.kct.kip7 of caver-js-ext-kas is the same as using caver.kct.kip7 of caver-js.

4.1.1. Deploying KIP-7 token contract

Here is how you deploy a KIP-7 token contract using caver-js-ext-kas.

const deployed = await caver.kct.kip7.deploy({
name: ‘Jasmine’,
symbol: ‘JAS’,
decimals: 18,
initialSupply: ‘100000000000000000000’
}, ‘0x{the address of a Klaytn account in KAS Wallet API}’)

The KIP-7 token contract is deployed using the static function caver.kct.kip7.deploy provided by KIP7. The parameters include token contract data and the Klaytn account address. The Klaytn account address to be used in the above tutorial should be the one that is stored in KAS Wallet API.

When the above codes are executed, the KIP-7 token contract will be deployed using the account stored in KAS Wallet API. As a result, a KIP7 instance, which includes the data of the deployed KIP-7 contract, will be returned.

4.1.2. Executing KIP-7 token contract

Here is how you execute a KIP-17 contract deployed on Klaytn using caver-js-ext-kas.

const contractAddress = ‘0x42c3809EeED7c5C497067fE4092D1c354D3a01Cb’
const kip7 = new caver.kct.kip7(contractAddress)

Pass the deployed KIP-7 contract address as a constructor parameter to create a KIP7 instance.

const receipt = await kip7.transfer(‘0x{address in hex}’, 1, { from: ‘0x{the address of a Klaytn account in KAS Wallet API}’ })

And then call the method of the KIP7 instance. The tutorial calls the kip7.transfer function, and the parameters of the actual KIP-7 token contract transfer function, as well as the object, where the data required for sending the transaction are defined, are passed.

Executing the above code will execute the KIP-7 token contract using the account stored in KAS Wallet API. The return value is the transaction receipt.

4.1.3. Deploying KIP-17 token contract

The code using caver-js-ext-kas to deploy and execute KIP-17 token contracts is similar to KIP-7. Here is how you deploy a KIP-17 token contract.

const deployed = await caver.kct.kip17.deploy({
name: ‘Jasmine’,
symbol: ‘JAS’,
}, ‘0x{the address of a Klaytn account in KAS Wallet API}’)

KIP-17 token contract is deployed using the static function caver.kct.kip17.deploy, which is provided by KIP17. Parameters include the data of the token contract and the Klaytn account address. The Klaytn account address to be used for deploying the contract must be the one that is stored in KAS Wallet API.

Executing the above codes will deploy the KIP-17 token contract using the account stored in KAS Wallet API, and as a result, a KIP17 instance, which contains the KIP-17 contract data will be returned.

4.1.4. Executing KIP-17 token contract

Here is how you execute a KIP-17 token contract deployed on Klaytn using caver-js-ext-kas:

const contractAddress = ‘0xAD719B194457D0641B410Ce75C4D22442533A781’
const kip17 = new caver.kct.kip17(contractAddress)

First, pass the KIP-17 contract address as a parameter to create a KIP17 instance.

const receipt = await kip17.mintWithTokenURI(‘0x{address i hex}’, tokenId, tokenURI, { from: ‘0x{the address of a Klaytn account in KAS Wallet API}’ })

And then call the method for the KIP17 instance. The tutorial calls the kip17.mintWithTokenURI function. Parameters for mintWithTokenURI function of the KIP-17 token contract are passed, followed by the object, where the data required for sending the transaction are defined.

Executing the above codes will execute the KIP-17 token contract, using the account stored in KAS Wallet API. As a result, the transaction receipt will be returned.

4.2. caver-java-ext-kas

4.2.1. Deploying KIP-7 token contract

Here is how you deploy a KIP-7 token contract using caver-java-ext-kas:

CaverExtKAS caver = new CaverExtKAS(ChainId.BAOBAB_TESTNET, “your_access_key”, “your_secret_access_key”);
String deployer = “0x{address in hex}”;
int decimals = 18;
BigInteger initialSupply = BigInteger.valueOf(100).multiply(BigInteger.TEN.pow(decimals)); // 100 * 1⁰¹⁸
KIP7 kip7 = KIP7.deploy(caver, deployer, “Jasmine”, “JAS”, decimals, initialSupply);

The KIP-7 token contract is deployed using the static function deploy provided by KIP7. The parameters to be passed include a CaverExtKAS instance, a Klaytn account address, and the token contract data. The Klaytn account address to be used for deploying the contract must be the one that is stored in KAS Wallet API.

Executing the above contracts will deploy the KIP-7 token contract using the account stored in KAS Wallet API. As a result, a KIP7 instance, which contains the KIP-7 contract data, will be returned.

4.2.2. Execute KIP-7 token contract

Here is how you execute a KIP-7 token contract deployed on Klaytn using caver-java-ext-kas:

CaverExtKAS caver = new CaverExtKAS(ChainId.BAOBAB_TESTNET, “your_access_key”, “your_secret_access_key”);String address = “0x42c3809EeED7c5C497067fE4092D1c354D3a01Cb”;
KIP7 kip7 = new KIP7(caver, address);

First, pass the CaverExtKAS instance and the KIP-7 contract address as constructor parameters to create the KIP7 instance.

SendOptions sendOptions = new SendOptions(baseAccount);
String toAccount = “0x{to address}”;
BigInteger transferAmount = BigInteger.TEN.multiply(BigInteger.TEN.pow(18)); // 10 * 1⁰¹⁸
TransactionReceipt.TransactionReceiptData receiptData = kip7.transfer(toAccount, transferAmount, sendOptions);

And then call a method of the KIP17 instance. The tutorial calls the kip7.transfer function, and the parameters for the KIP-7 token contract transfer function are passed in the same order as those of the function, followed by the object, where the data required for sending the transaction are defined.

Executing the above code will execute the KIP-7 token contract, using the account stored in KAS Wallet API. As a result, transaction receipt will be returned.

4.2.3. Deploy KIP-17 token contract

Using caver-java-ext-kas to deploy and execute KIP-17 token contracts is similar to KIP-7. Here is how you deploy a KIP-17 token contract.

CaverExtKAS caver = new CaverExtKAS(ChainId.BAOBAB_TESTNET, “your_access_key”, “your_secret_access_key”);String deployer = “0x{address in hex}”;KIP17 kip17 = KIP17.deploy(caver, deployer, “Jasmine”, “JAS”);

The KIP-17 token contract is deployed using the static function deploy provided from KIP17. The parameters to be passed include CaverExtKAS instance, the Klaytn account address, and the token contract data. The Klaytn account address to be used for deploying the contract must be the one that is stored in KAS Wallet API.

Executing the above code will deploy a KIP-17 token contract, using the account stored in KAS Wallet API. As a result, a KIP17 instance, which contains the data for the deployed KIP-17 is returned.

4.2.4. Execute KIP-17 token contract

Here is how you execute a KIP-17 token contract deployed on Klaytn using caver-java-ext-kas:

CaverExtKAS caver = new CaverExtKAS(ChainId.BAOBAB_TESTNET, “your_access_key”, “your_secret_access_key”);String contractAddress = “0xAD719B194457D0641B410Ce75C4D22442533A781”;
KIP17 kip17 = new KIP17(caver, contractAddress);

First, pass the CaverExtKAS instance and the KIP-17 contract address as constructor parameters to create a KIP17 instance.

String from = “0x{from address}”;
String to = “0x{to address}”;
String tokenURI = “tokenURI”;
SendOptions sendOptions = new SendOptions(from);
TransactionReceipt.TransactionReceiptData receiptData = kip17.mintWithTokenURI(to, BigInteger.ZERO, tokenURI, sendOptions);

And then call the method of the KIP17 instance. In the tutorial, the kip17.mintWithTokenURI function is called. The parameters of the mintWithTokenURI function of the KIP-17 token contract are passed in order, followed by the object, where the data required for sending the transaction are defined.

Executing the above code will execute the KIP-17 token contract, using the account stored in KAS Wallet API. As a result, the transcription receipt will be returned.

5. Using KeyringContainer with KAS SDK

KAS SDK supports KASWallet, which uses KAS Wallet API, instead of the in-memory wallet KeyringContainer, which caver.wallet of caver originally provides. If you want to use KeyringContainer with KAS SDK, you have to make a separate instance.

Furthermore, both caver-js-ext-kas and caver-java-ext-kas support the setWallet function, which allows you to select a wallet for deploying and executing contracts with caver.contract and caver.kct. With this, you can be more flexible in choosing the wallet to be used for deploying or executing contracts for each instance when using KAS SDK.

5.1. caver-js-ext-kas

Here is how you create a KeyringContainer instance using caver-js-ext-kas:

const keyringContainer = new caver.keyringContainer()

Here is how you use the keyringContainer with caver.contract, caver.kct.kip7 and caver.kct.kip17.

const contract = new caver.contract(abi, contractAddress)
contract.setWallet(keyringContainer)
const kip7 = new caver.kct.kip7(contractAddress)
kip7.setWallet(keyringContainer)
const kip7 = new caver.kct.kip17(contractAddress)
kip7.setWallet(keyringContainer)

You can set the IWallet to be used with the contract by calling the setWallet method of the Contract instance. It functions the same way for both KIP7 and KIP17.

The static method deploy function, provided by KIP7 and KIP17, allows you to set a wallet by passing IWallet to the last parameter. Below is the code for passing keyringContainer to the last parameter for the deploy function.

const kip7 = await caver.kct.kip7.deploy({…}, deployer, keyringContainer)const kip17 = await caver.kct.kip17.deploy({…}, deployer, keyringContainer)

Executing the above code will use the keyring stored inside the keyringContainer when signing the SmartContractDeploy transaction.

5.2. caver-java-ext-kas

Here is how you create a KeyringContainer instance using caver-java-ext-kas:

KeyringContainer keyringContainer = new KeyringContainer();

Here is how you use the keyringContainer with caver.contract, caver.kct.kip7 and caver.kct.kip17.

CaverExtKAS caver = new CaverExtKAS(ChainId.BAOBAB_TESTNET, “your_access_key”, “your_secret_access_key”);KeyringContainer keyringContainer = new KeyringContainer();
Contract contract = new Contract(caver, abi, contractAddress);
contract.setWallet(keyringContainer);
KIP7 kip7 = new KIP7(caver, contractAddress);
kip7.setWallet(keyringContainer);
KIP17 kip17 = new KIP17(caver, contractAddress);
kip17.setWallet(keyringContainer);

You can set the IWallet to be used in contract by calling the setWallet method of the Contract instance. It functions the same way for both KIP7 and KIP17.

The static method deploy function, provided by KIP7 and KIP17, allows you to pass IWallet to the last parameter. Below is the code for passing keyringContainer to the last parameter of the deploy function.

CaverExtKAS caver = new CaverExtKAS(ChainId.BAOBAB_TESTNET, “your_access_key”, “your_secret_access_key”);KeyringContainer keyringContainer = new KeyringContainer();KIP7 kip7 = KIP7.deploy(caver, “0x{address in hex}”, “Jasmine”, “JAS”, decimals, initialValue, keyringContainer);KIP17 kip17 = KIP17.deploy(caver, “0x{address in hex}”, “Jasmine”, “JAS”, keyringContainer);

Executing the above code will use the keyring stored inside the keyringContainer to sign the SmartContractDeploy transaction.

6. Sample Codes

Below is the sample code for deploying and executing smart contracts that store and load data in the key/value format using caver-js-ext-kas:

// js : KAS Wallet API with `caver.contract`
const CaverExtKAS = require('caver-js-ext-kas')
const caver = new CaverExtKAS()
caver.initKASAPI(chainId, accessKeyId, secretAccessKey)const abi = [
{
constant: true,
inputs: [{ name: 'key', type: 'string' }],
name: 'get',
outputs: [{ name: '', type: 'string' }],
payable: false,
stateMutability: 'view',
type: 'function',
},
{
constant: false,
inputs: [{ name: 'key', type: 'string' }, { name: 'value', type: 'string' }],
name: 'set',
outputs: [],
payable: false,
stateMutability: 'nonpayable',
type: 'function',
},
]
const byteCode = '0x608060405234801561001057600080fd5b5061051f806100206000396000f3fe608060405234801561001057600080fd5b50600436106100365760003560e01c8063693ec85e1461003b578063e942b5161461016f575b600080fd5b6100f460048036036020811015610ㅇ05157600080fd5b810190808035906020019064010000000081111561006e57600080fd5b82018360208201111561008057600080fd5b803590602001918460018302840111640100000000831117156100a257600080fd5b91908080601f016020809104026020016040519081016040528093929190818152602001838380828437600081840152601f19601f8201169050808301925050505050505091929192905050506102c1565b6040518080602001828103825283818151815260200191508051906020019080838360005b83811015610134578082015181840152602081019050610119565b50505050905090810190601f1680156101615780820380516001836020036101000a031916815260200191505b509250505060405180910390f35b6102bf6004803603604081101561018557600080fd5b81019080803590602001906401000000008111156101a257600080fd5b8201836020820111156101b457600080fd5b803590602001918460018302840111640100000000831117156101d657600080fd5b91908080601f016020809104026020016040519081016040528093929190818152602001838380828437600081840152601f19601f8201169050808301925050505050505091929192908035906020019064010000000081111561023957600080fd5b82018360208201111561024b57600080fd5b8035906020019184600183028401116401000000008311171561026d57600080fd5b91908080601f016020809104026020016040519081016040528093929190818152602001838380828437600081840152601f19601f8201169050808301925050505050505091929192905050506103cc565b005b60606000826040518082805190602001908083835b602083106102f957805182526020820191506020810190506020830392506102d6565b6001836020036101000a03801982511681845116808217855250505050505090500191505090815260200160405180910390208054600181600116156101000203166002900480601f0160208091040260200160405190810160405280929190818152602001828054600181600116156101000203166002900480156103c05780601f10610395576101008083540402835291602001916103c0565b820191906000526020600020905b8154815290600101906020018083116103a357829003601f168201915b50505050509050919050565b806000836040518082805190602001908083835b6020831061040357805182526020820191506020810190506020830392506103e0565b6001836020036101000a0380198251168184511680821785525050505050509050019150509081526020016040518091039020908051906020019061044992919061044e565b505050565b828054600181600116156101000203166002900490600052602060002090601f016020900481019282601f1061048f57805160ff19168380011785556104bd565b828001600101855582156104bd579182015b828111156104bc5782518255916020019190600101906104a1565b5b5090506104ca91906104ce565b5090565b6104f091905b808211156104ec5760008160009055506001016104d4565b5090565b9056fea165627a7a723058203ffebc792829e0434ecc495da1b53d24399cd7fff506a4fd03589861843e14990029'const contract = new caver.contract(abi)const deployed = await contract.deploy({ data: byteCode }).send({ from: '0x{the address of a Klaytn account in KAS Wallet API}', gas: 10000000 })console.log(`Deployed contract address: ${deployed.options.address}`)

Below is the sample code for deploying and executing KIP7 using caver-js-ext-kas.

// js : KAS Wallet API with `caver.kct.kip7`
const CaverExtKAS = require('caver-js-ext-kas')
const caver = new CaverExtKAS()
caver.initKASAPI(chainId, accessKeyId, secretAccessKey)const deployed = await caver.kct.kip7.deploy({
name: 'Jasmine',
symbol: 'JAS',
decimals: 18,
initialSupply: '100000000000000000000'
}, '0x{the address of a Klaytn account in KAS Wallet API}')
console.log(`Deployed contract address: ${deployed.options.address}`)
const contractAddress = deployed.options.address
const kip7 = new caver.kct.kip7(contractAddress)
const receipt = await kip7.transfer('0x{address in hex}', 1, { from: '0x{the address of a Klaytn account in KAS Wallet API}' })
console.log(receipt)

Below is the sample code for deploying and executing KIP17 using caver-js-ext-kas.

// js : KAS Wallet API account with `caver.kct.kip17`
const CaverExtKAS = require('caver-js-ext-kas')
const caver = new CaverExtKAS()
caver.initKASAPI(chainId, accessKeyId, secretAccessKey)const deployed = await caver.kct.kip17.deploy({
name: 'Jasmine',
symbol: 'JAS',
}, '0x{the address of a Klaytn account in KAS Wallet API}')
console.log(`Deployed contract address: ${deployed.options.address}`)
const contractAddress = deployed.options.address
const kip17 = new caver.kct.kip17(contractAddress)
const receipt = await kip17.mintWithTokenURI('0x{address i hex}', tokenId, tokenURI, { from: '0x{the address of a Klaytn account in KAS Wallet API}' })
console.log(receipt)

Below is the sample code for deploying and executing KIP7 and KIP17 contracts with KeyringContainer of caver-js-ext-kas.

// js : Use KeyringContainer with KAS SDK
const CaverExtKAS = require('caver-js-ext-kas')
const caver = new CaverExtKAS()
caver.initKASAPI(chainId, accessKeyId, secretAccessKey)const keyringContainer = new caver.keyringContainer()const keyring = keyringContainer.keyring.createFromPrivateKey('0x{private key}')
keyringContainer.add(keyring)
// Use keyringContainer with caver.contract
const contract = new caver.contract(abi, '0x{contract address}')
contract.setWallet(keyringContainer)
const receipt = await contract.methods.set('key', 'value').send({ from: keyring.address, gas: 5000000 })
// Use keyringContainer with caver.kct.kip7
const deployed = await caver.kct.kip7.deploy({
name: 'Jasmine',
symbol: 'JAS',
decimals: 18,
initialSupply: '100000000000000000000'
}, keyring.address, keyringContainer)
const kip7Address = deployed.options.address
const kip7 = new caver.kct.kip7(kip7Address)
kip7.setWallet(keyringContainer)
const receiptWithTransfer = await kip7.transfer('0x{address in hex}', 1, { from: keyring.address })
console.log(receiptWithTransfer)
// Use keyringContainer with caver.kct.kip17
const deployed = await caver.kct.kip17.deploy({
name: 'Jasmine',
symbol: 'JAS',
}, keyring.address, keyringContainer)
const kip17Address = deployed.options.address
const kip17 = new caver.kct.kip17(kip17Address)
kip17.setWallet(keyringContainer)
const receiptWithMinting = await kip17.mintWithTokenURI('0x{address i hex}', tokenId, tokenURI, { from: keyring.address })
console.log(receiptWithMinting)

Below is the sample code for deploying and executing smart contracts that store and load data in key/value format using caver-java-ext-kas.

import com.klaytn.caver.contract.Contract;
import com.klaytn.caver.contract.SendOptions;
import com.klaytn.caver.methods.response.TransactionReceipt;
import com.klaytn.caver.utils.ChainId;
import org.web3j.abi.datatypes.Type;
import org.web3j.protocol.exceptions.TransactionException;
import xyz.groundx.caver_ext_kas.CaverExtKAS;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.math.BigInteger;
import java.util.List;
public class SampleContract {
static final String BINARY = "608060405234801561001057600080fd5b5061051f806100206000396000f3fe608060405234801561001057600080fd5b50600436106100365760003560e01c8063693ec85e1461003b578063e942b5161461016f575b600080fd5b6100f46004803603602081101561005157600080fd5b810190808035906020019064010000000081111561006e57600080fd5b82018360208201111561008057600080fd5b803590602001918460018302840111640100000000831117156100a257600080fd5b91908080601f016020809104026020016040519081016040528093929190818152602001838380828437600081840152601f19601f8201169050808301925050505050505091929192905050506102c1565b6040518080602001828103825283818151815260200191508051906020019080838360005b83811015610134578082015181840152602081019050610119565b50505050905090810190601f1680156101615780820380516001836020036101000a031916815260200191505b509250505060405180910390f35b6102bf6004803603604081101561018557600080fd5b81019080803590602001906401000000008111156101a257600080fd5b8201836020820111156101b457600080fd5b803590602001918460018302840111640100000000831117156101d657600080fd5b91908080601f016020809104026020016040519081016040528093929190818152602001838380828437600081840152601f19601f8201169050808301925050505050505091929192908035906020019064010000000081111561023957600080fd5b82018360208201111561024b57600080fd5b8035906020019184600183028401116401000000008311171561026d57600080fd5b91908080601f016020809104026020016040519081016040528093929190818152602001838380828437600081840152601f19601f8201169050808301925050505050505091929192905050506103cc565b005b60606000826040518082805190602001908083835b602083106102f957805182526020820191506020810190506020830392506102d6565b6001836020036101000a03801982511681845116808217855250505050505090500191505090815260200160405180910390208054600181600116156101000203166002900480601f0160208091040260200160405190810160405280929190818152602001828054600181600116156101000203166002900480156103c05780601f10610395576101008083540402835291602001916103c0565b820191906000526020600020905b8154815290600101906020018083116103a357829003601f168201915b50505050509050919050565b806000836040518082805190602001908083835b6020831061040357805182526020820191506020810190506020830392506103e0565b6001836020036101000a0380198251168184511680821785525050505050509050019150509081526020016040518091039020908051906020019061044992919061044e565b505050565b828054600181600116156101000203166002900490600052602060002090601f016020900481019282601f1061048f57805160ff19168380011785556104bd565b828001600101855582156104bd579182015b828111156104bc5782518255916020019190600101906104a1565b5b5090506104ca91906104ce565b5090565b6104f091905b808211156104ec5760008160009055506001016104d4565b5090565b9056fea165627a7a723058203ffebc792829e0434ecc495da1b53d24399cd7fff506a4fd03589861843e14990029";
static final String ABI = "[{\"constant\":true,\"inputs\":[{\"name\":\"key\",\"type\":\"string\"}],\"name\":\"get\",\"outputs\":[{\"name\":\"\",\"type\":\"string\"}],\"payable\":false,\"stateMutability\":\"view\",\"type\":\"function\"},{\"constant\":false,\"inputs\":[{\"name\":\"key\",\"type\":\"string\"},{\"name\":\"value\",\"type\":\"string\"}],\"name\":\"set\",\"outputs\":[],\"payable\":false,\"stateMutability\":\"nonpayable\",\"type\":\"function\"}]";
public static void main(String[] args) throws NoSuchMethodException, TransactionException, IOException, InstantiationException, IllegalAccessException, InvocationTargetException, ClassNotFoundException {
String accessKey = "your access key";
String secretAccessKey = "your secret access key";
CaverExtKAS caver = new CaverExtKAS(ChainId.BAOBAB_TESTNET, accessKey, secretAccessKey);
//The deployer account must be created through the KAS Wallet API.
//The deployer must have enough KLAY to deploy the contract
String deployer = "0x{address}";
//Deploy contract
BigInteger gas = BigInteger.valueOf(10000000);
SendOptions deploySendOptions = new SendOptions(deployer, gas);
Contract sampleContract = new Contract(caver, ABI);
sampleContract.deploy(deploySendOptions, BINARY);
System.out.println("Deployed contract address: " + sampleContract.getContractAddress());
//Connect already deployed contract
Contract deployedContract = new Contract(caver, ABI, sampleContract.getContractAddress());

//Execute smart contract's "set" function
SendOptions sendOptions = new SendOptions(deployer, BigInteger.valueOf(5000000));
TransactionReceipt.TransactionReceiptData receiptData = deployedContract.send(sendOptions, "set", "key", "value");
System.out.println(receiptData.getTransactionHash());
//Execute smart contract's "get" function
List<Type> result = deployedContract.call("get", "key");
System.out.println((String)result.get(0).getValue());
}
}

Below is the sample code for deploying and executing KIP7 using caver-java-ext-kas.

import com.klaytn.caver.contract.SendOptions;
import com.klaytn.caver.kct.kip7.KIP7;
import com.klaytn.caver.methods.response.TransactionReceipt;
import com.klaytn.caver.utils.ChainId;
import org.web3j.protocol.exceptions.TransactionException;
import xyz.groundx.caver_ext_kas.CaverExtKAS;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.math.BigInteger;
public class SampleKIP7 {
public static void main(String[] args) throws NoSuchMethodException, TransactionException, IOException, InstantiationException, IllegalAccessException, InvocationTargetException, ClassNotFoundException {
String accessKey = "your access key";
String secretAccessKey = "your secret access key";
CaverExtKAS caver = new CaverExtKAS(ChainId.BAOBAB_TESTNET, accessKey, secretAccessKey);
//The deployer account must be created through the KAS Wallet API.
//The deployer must have enough KLAY to deploy the contract
String deployer = "0x{address}";

//deploy contract
BigInteger initialSupply = BigInteger.TEN.multiply(BigInteger.TEN.pow(18)); // 10 * 10¹⁸
KIP7 kip7 = KIP7.deploy(caver, deployer, "Jasmine", "JAS", 18, initialSupply);
System.out.println(kip7.getContractAddress());
//Connect already deployed contract
KIP7 deployedKIP7 = new KIP7(caver, kip7.getContractAddress());
//Execute KIP7’s transfer function.
String from = deployer;
String to = "0x{to address}";
SendOptions sendOptions = new SendOptions(from);
BigInteger transferAmount = BigInteger.ONE.multiply(BigInteger.TEN.pow(18));
TransactionReceipt.TransactionReceiptData receiptData = deployedKIP7.transfer(to, transferAmount, sendOptions);
System.out.println(receiptData.getTransactionHash());
}
}

Below is the sample code for deploying and executing KIP17 using caver-java-ext-kas.

import com.klaytn.caver.contract.SendOptions;
import com.klaytn.caver.kct.kip17.KIP17;
import com.klaytn.caver.methods.response.TransactionReceipt;
import com.klaytn.caver.utils.ChainId;
import org.junit.Ignore;
import org.web3j.protocol.exceptions.TransactionException;
import xyz.groundx.caver_ext_kas.CaverExtKAS;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.math.BigInteger;
public class SampleKIP17 {public static void main(String[] args) throws NoSuchMethodException, IOException, InstantiationException, ClassNotFoundException, IllegalAccessException, InvocationTargetException, TransactionException {
String accessKey = "your access key";
String secretAccessKey = "your secret access key";
CaverExtKAS caver = new CaverExtKAS(ChainId.BAOBAB_TESTNET, accessKey, secretAccessKey);
//The deployer account must be created through the KAS Wallet API.
//The deployer must have enough KLAY to deploy the contract
String deployer = "0x{address}";
//deploy contract
KIP17 kip17 = KIP17.deploy(caver, deployer, "Jasmine", "JAS");
System.out.println(kip17.getContractAddress());
//Connect already deployed contract
KIP17 deployedKip17 = new KIP17(caver, kip17.getContractAddress());
//Execute KIP7’s transfer function.
String from = deployer;
String to = "0x{to address}";
String tokenURI = "tokenURI";
SendOptions sendOptions = new SendOptions(from);
TransactionReceipt.TransactionReceiptData receiptData = deployedKip17.mintWithTokenURI(to, BigInteger.ZERO, tokenURI, sendOptions);
System.out.println(receiptData.getTransactionHash());
}
}

Below is the sample code for deploying and executing KIP7 and KIP17 with KeyringContainer of caver-java-ext-kas.

import com.klaytn.caver.contract.SendOptions;
import com.klaytn.caver.kct.kip17.KIP17;
import com.klaytn.caver.kct.kip7.KIP7;
import com.klaytn.caver.methods.response.TransactionReceipt;
import com.klaytn.caver.utils.ChainId;
import com.klaytn.caver.wallet.KeyringContainer;
import com.klaytn.caver.wallet.keyring.KeyringFactory;
import com.klaytn.caver.wallet.keyring.SingleKeyring;
import org.junit.Ignore;
import org.web3j.protocol.exceptions.TransactionException;
import xyz.groundx.caver_ext_kas.CaverExtKAS;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.math.BigInteger;
public class SampleKeyringContainer {

public static void main(String[] args) throws NoSuchMethodException, IOException, InstantiationException, ClassNotFoundException, IllegalAccessException, InvocationTargetException, TransactionException {
String accessKey = "your access key";
String secretAccessKey = "your secret access key";
CaverExtKAS caver = new CaverExtKAS(ChainId.BAOBAB_TESTNET, accessKey, secretAccessKey);
KeyringContainer container = new KeyringContainer();//deployer must have KLAY to deploy and execute smart contract.
String deployerPrivateKey = "0x{private key}";
SingleKeyring deployerKeyring = (SingleKeyring)container.add(KeyringFactory.createFromPrivateKey(deployerPrivateKey));
String deployerAddr = deployerKeyring.getAddress();
String toAddress = "0x{address}";
//KIP7
BigInteger initialSupply = BigInteger.TEN.multiply(BigInteger.TEN.pow(18)); // 10 * 10¹⁸
KIP7 kip7 = KIP7.deploy(caver, deployerAddr, "Jasmine", "JAS", 18, initialSupply, container);
KIP7 deployedKIP7 = new KIP7(caver, kip7.getContractAddress());
BigInteger transferAmount = BigInteger.ONE.multiply(BigInteger.TEN.pow(18));
SendOptions transferSendOptions = new SendOptions(deployerAddr);
TransactionReceipt.TransactionReceiptData receiptData_transfer = deployedKIP7.transfer(toAddress, transferAmount, transferSendOptions);//KIP17
KIP17 kip17 = KIP17.deploy(caver, deployerKeyring.getAddress(), "Jasmine", "JAS", container);
KIP17 deployedKIP17 = new KIP17(caver, kip7.getContractAddress());
SendOptions mintSendOptions = new SendOptions(deployerAddr);
TransactionReceipt.TransactionReceiptData receiptData_mint = deployedKIP17.mintWithTokenURI(toAddress, BigInteger.ZERO, "tokenURI", mintSendOptions);
}
}

--

--