Skip to content

Commit

Permalink
feat(aens): allow to pass salt between Name while claiming
Browse files Browse the repository at this point in the history
  • Loading branch information
davidyuk committed Jan 28, 2025
1 parent dac8e8b commit 875faef
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 7 deletions.
16 changes: 9 additions & 7 deletions src/aens.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ interface NamePreclaimOptions
Optional<SendTransactionOptions, 'onAccount' | 'onNode'> {}

interface NameClaimOptions
extends BuildTxOptions<Tag.NameClaimTx, 'accountId' | 'nameSalt' | 'name'>,
extends BuildTxOptions<Tag.NameClaimTx, 'accountId' | 'name'>,
Optional<SendTransactionOptions, 'onAccount' | 'onNode'> {}

class NotAuctionNameError extends LogicError {
Expand Down Expand Up @@ -263,10 +263,10 @@ export default class Name {
const opt = { ...this.options, ...options };
const tx = await buildTxAsync({
_isInternalBuild: true,
nameSalt: this.#salt,
...opt,
tag: Tag.NameClaimTx,
accountId: opt.onAccount.address,
nameSalt: this.#salt,
name: this.value,
});
return sendTransaction(tx, opt);
Expand All @@ -280,19 +280,21 @@ export default class Name {
* await name.preclaim({ ttl, fee, nonce })
* ```
*/
async preclaim(options: NamePreclaimOptions = {}): ReturnType<typeof sendTransaction> {
async preclaim(
options: NamePreclaimOptions = {},
): Promise<Awaited<ReturnType<typeof sendTransaction>> & { nameSalt: number }> {
const opt = { ...this.options, ...options };
const salt = genSalt();
const nameSalt = genSalt();
const tx = await buildTxAsync({
_isInternalBuild: true,
...opt,
tag: Tag.NamePreclaimTx,
accountId: opt.onAccount.address,
commitmentId: commitmentHash(this.value, salt),
commitmentId: commitmentHash(this.value, nameSalt),
});
const result = await sendTransaction(tx, opt);
this.#salt = salt;
return result;
this.#salt = nameSalt;
return { ...result, nameSalt };
}

/**
Expand Down
12 changes: 12 additions & 0 deletions test/integration/aens.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ describe('Aens', () => {
blockHash: preclaimRes.blockHash,
encodedTx: preclaimRes.encodedTx,
hash: preclaimRes.hash,
nameSalt: preclaimRes.nameSalt,
signatures: [preclaimRes.signatures[0]],
rawTx: preclaimRes.rawTx,
});
Expand Down Expand Up @@ -98,6 +99,17 @@ describe('Aens', () => {
});
}).timeout(timeoutBlock);

it('claims a name using different Name instances', async () => {
const string = randomName(30);

const name1 = new Name(string, aeSdk.getContext());
const { nameSalt } = await name1.preclaim();
expect(nameSalt).to.be.a('number');

const name2 = new Name(string, aeSdk.getContext());
await name2.claim({ nameSalt });
}).timeout(timeoutBlock);

it('claims a long name without preclaim', async () => {
const nameString = randomName(30);
const n = new Name(nameString, aeSdk.getContext());
Expand Down

0 comments on commit 875faef

Please sign in to comment.