Wallet Api

WalletApi는 KeyStore 와 상호작용하는 기능을 제공합니다. WalletApi를 사용해서 keystore에 저장되어 있는 key에 대한 unlock, lock을 할 수 있습니다. WalletApi는 또한 aergo client를 사용하는 high-level api를 제공합니다. TransactionApi의 경우 nonce 실패일 때 자동으로 최신 nonce를 가져와서 재시도를 합니다.

WalletApi는 한번에 하나의 unlock된 계정만 가질 수 있습니다. 만약 한개의 계정을 unlock한 후 다른 계정을 unlock하면 이전의 계정은 자동으로 lock됩니다.

Create

WalletApi를 만들기 위해서는 KeyStore가 필요합니다.

nonce 실패 시 재시도 횟수와 간격을 명시적으로 설정해서 생성.

// create a keystore
KeyStore keyStore = KeyStores.newInMemoryKeyStore();

// create a wallet api
WalletApi walletApi = new WalletApiFactory().create(keyStore);
System.out.println("WalletApi: " + walletApi);

nonce 실패 시 재시도 횟수와 간격을 기본 설정으로 해서 생성.

// create a keystore
KeyStore keyStore = KeyStores.newInMemoryKeyStore();

// create a wallet api with retry count 5 and interval 1s
TryCountAndInterval tryCountAndInterval = TryCountAndInterval
    .of(5, Time.of(1L, TimeUnit.SECONDS));
WalletApi walletApi = new WalletApiFactory().create(keyStore, tryCountAndInterval);
System.out.println("WalletApi: " + walletApi);

Unlock and Lock

Unlock을 함으로써 해당 계정으로 transaction을 발생시킬 수 있습니다.

// create a keystore
KeyStore keyStore = KeyStores.newInMemoryKeyStore();

// store new key to keystore
AergoKey aergoKey = new AergoKeyGenerator().create();
Authentication authentication = Authentication.of(aergoKey.getAddress(), "password");
keyStore.save(authentication, aergoKey);

// create a wallet api
WalletApi walletApi = new WalletApiFactory().create(keyStore);

// unlock account
boolean unlockResult = walletApi.unlock(authentication);
System.out.println("Unlock result: " + unlockResult);
System.out.println("Currently locked one: " + walletApi.getPrincipal());

// do something..
Signature signature = walletApi.signMessage(BytesValue.of("test".getBytes()));
System.out.println("Signature: " + signature);

// lock account
boolean lockResult = walletApi.lock();
System.out.println("Lock result: " + lockResult);

High Level Api

WalletApi는 aergo node와 상호작용 하기 위한 high-level api를 제공합니다. TransactionApi를 사용하기 위해서는 계정을 unlock해야합니다. QueryApi는 별도의 unlock된 계정이 필요하지 않습니다.

// prepare client
AergoClient aergoClient = new AergoClientBuilder().build();

// create a keystore
KeyStore keyStore = KeyStores.newInMemoryKeyStore();

// create a wallet api
WalletApi walletApi = new WalletApiFactory().create(keyStore);
System.out.println("WalletApi: " + walletApi);

// transaction api
TransactionApi transactionApi = walletApi.with(aergoClient).transaction();
System.out.println("Transaction Api: " + transactionApi);

// query api
QueryApi queryApi = walletApi.with(aergoClient).query();
System.out.println("Query Api: " + queryApi);