Skip to content

Commit

Permalink
fix(aens): handle revoked names, refactor docs
Browse files Browse the repository at this point in the history
  • Loading branch information
davidyuk committed Apr 15, 2024
1 parent 4fada6e commit 70b46c2
Show file tree
Hide file tree
Showing 7 changed files with 124 additions and 188 deletions.
66 changes: 4 additions & 62 deletions src/actions/aens.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,3 @@
// # æternity CLI `AENS` file
//
// This script initialize all `AENS` function

import { isAddressValid, getDefaultPointerKey } from '@aeternity/aepp-sdk';
import { initSdkByWalletFile } from '../utils/cli.js';
import { printTransaction } from '../utils/print.js';
Expand All @@ -15,61 +11,39 @@ async function ensureNameStatus(name, sdk, status, operation) {
}
}

// ## Claim `name` function
export async function preClaim(walletPath, name, options) {
const {
ttl, fee, nonce, json,
} = options;

// Validate `name`(check if `name` end on `.chain`)
// validateName(name)

validateName(name);
const sdk = await initSdkByWalletFile(walletPath, options);

// Check if that `name' available
await ensureNameStatus(name, sdk, 'AVAILABLE', 'preclaimed');
// Create `pre-claim` transaction
const preClaimTx = await sdk.aensPreclaim(name, { ttl, fee, nonce });
await printTransaction(preClaimTx, json, sdk);
}

// ## Claim `name` function
export async function claim(walletPath, name, salt, options) {
const {
ttl, fee, nonce, json, nameFee,
} = options;
// Validate `name`
// validateName(name)

validateName(name);
const sdk = await initSdkByWalletFile(walletPath, options);

// Check if that `name' available
await ensureNameStatus(name, sdk, 'AVAILABLE', 'claimed');

// Wait for next block and create `claimName` transaction
const claimTx = await sdk.aensClaim(name, salt, {
nonce, ttl, fee, nameFee,
});
await printTransaction(claimTx, json, sdk);
}

// ##Update `name` function
export async function updateName(walletPath, name, addresses, options) {
const {
ttl, fee, nonce, json, nameTtl, clientTtl, extendPointers = false,
} = options;

// Validate `address`
const invalidAddresses = addresses.filter((address) => !isAddressValid(address));
if (invalidAddresses.length) throw new CliError(`Addresses "[${invalidAddresses}]" is not valid`);
// Validate `name`
validateName(name);
const sdk = await initSdkByWalletFile(walletPath, options);

// Check if that `name` is unavailable and we can update it
await ensureNameStatus(name, sdk, 'CLAIMED', 'updated');

// Create `updateName` transaction
const updateTx = await sdk.aensUpdate(
name,
Object.fromEntries(addresses.map((address) => [getDefaultPointerKey(address), address])),
Expand All @@ -80,60 +54,38 @@ export async function updateName(walletPath, name, addresses, options) {
await printTransaction(updateTx, json, sdk);
}

// ##Extend `name` ttl function
export async function extendName(walletPath, name, nameTtl, options) {
const {
ttl, fee, nonce, json,
} = options;

// Validate `name`
validateName(name);
const sdk = await initSdkByWalletFile(walletPath, options);

// Check if that `name` is unavailable and we can update it
await ensureNameStatus(name, sdk, 'CLAIMED', 'extended');

// Create `updateName` transaction
const updateTx = await sdk.aensUpdate(name, {}, {
ttl, fee, nonce, nameTtl, extendPointers: true,
});
await printTransaction(updateTx, json, sdk);
}

// ##Transfer `name` function
export async function transferName(walletPath, name, address, options) {
const {
ttl, fee, nonce, json,
} = options;

// Validate `address`
if (!isAddressValid(address)) throw new CliError(`Address "${address}" is not valid`);
// Validate `name`
validateName(name);
const sdk = await initSdkByWalletFile(walletPath, options);

// Check if that `name` is unavailable and we can transfer it
await ensureNameStatus(name, sdk, 'CLAIMED', 'transferred');

// Create `transferName` transaction
const transferTX = await sdk.aensTransfer(name, address, { ttl, fee, nonce });
await printTransaction(transferTX, json, sdk);
}

// ## Revoke `name` function
export async function revokeName(walletPath, name, options) {
const {
ttl, fee, nonce, json,
} = options;

// Validate `name`
validateName(name);
const sdk = await initSdkByWalletFile(walletPath, options);

// Check if `name` is unavailable and we can revoke it
await ensureNameStatus(name, sdk, 'CLAIMED', 'revoked');

// Create `revokeName` transaction
const revokeTx = await sdk.aensRevoke(name, { ttl, fee, nonce });
await printTransaction(revokeTx, json, sdk);
}
Expand All @@ -142,15 +94,9 @@ export async function nameBid(walletPath, name, nameFee, options) {
const {
ttl, fee, nonce, json,
} = options;
// Validate `name`
validateName(name);

const sdk = await initSdkByWalletFile(walletPath, options);

// Check if that `name' available
await ensureNameStatus(name, sdk, 'AUCTION', 'bidded');

// Wait for next block and create `claimName` transaction
const nameBidTx = await sdk.aensBid(name, nameFee, { nonce, ttl, fee });
await printTransaction(nameBidTx, json, sdk);
}
Expand All @@ -161,26 +107,22 @@ export async function fullClaim(walletPath, name, options) {
} = options;
validateName(name);
if (name.split('.')[0] < 13) throw new CliError('Full name claiming works only with name longer then 12 symbol (not trigger auction)');

const sdk = await initSdkByWalletFile(walletPath, options);

// Check if that `name' available
await ensureNameStatus(name, sdk, 'AVAILABLE', 'claimed');

// Wait for next block and create `claimName` transaction
nonce = nonce && +nonce;
const preclaim = await sdk.aensPreclaim(name, { nonce, ttl, fee });

nonce = nonce && nonce + 1;
const nameInstance = await preclaim.claim({
nonce, ttl, fee, nameFee,
});

nonce = nonce && nonce + 1;
const updateTx = await nameInstance.update(
{ account_pubkey: sdk.address },
{
nonce, ttl, fee, nameTtl, clientTtl,
},
);

await printTransaction(updateTx, json, sdk);
}
5 changes: 4 additions & 1 deletion src/arguments.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Option } from 'commander';
import BigNumber from 'bignumber.js';
import { MIN_GAS_PRICE } from '@aeternity/aepp-sdk';
import { MIN_GAS_PRICE, CLIENT_TTL } from '@aeternity/aepp-sdk';
import { noValue } from './utils/default-option-description.js';

export const coinAmountParser = (amount) => {
Expand Down Expand Up @@ -40,3 +40,6 @@ export const ttlOption = (usingNode) => new Option('-T, --ttl [ttl]', 'Validity
.default(noValue, usingNode ? 3 : 0);

export const networkIdOption = new Option('--networkId [networkId]', 'Network id');

export const clientTtlOption = new Option('--clientTtl [clientTtl]', 'a suggestion measured in seconds on how long clients should cache name pointers')
.default(CLIENT_TTL, '1 hour');
2 changes: 1 addition & 1 deletion src/commands/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ const EXECUTABLE_CMD = [
{ name: 'inspect', desc: 'Get information on transactions, blocks,...' },
{ name: 'account', desc: 'Handle wallet operations' },
{ name: 'contract', desc: 'Contract interactions' },
{ name: 'name', desc: 'AENS system' },
{ name: 'name', desc: 'manage AENS names' },
{ name: 'tx', desc: 'Generates transactions to sign and submit manually' },
{ name: 'oracle', desc: 'Interact with oracles' },
{ name: 'spend', desc: 'Send coins to account or contract' },
Expand Down

0 comments on commit 70b46c2

Please sign in to comment.