Skip to content

Commit

Permalink
limit the target len to 150 bytes
Browse files Browse the repository at this point in the history
  • Loading branch information
peterjah committed Dec 31, 2024
1 parent 1cd4608 commit 75b5cb3
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 14 deletions.
9 changes: 9 additions & 0 deletions smart-contract/assembly/__tests__/dns.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,15 @@ describe('Test DNS allocation', () => {
mockBalance(scAddress, transferredAmount);
dnsAlloc(args.serialize());
});
throws('Invalid target', () => {
let args = new Args();
args.add(domain);
args.add('lol'.repeat(51));
mockBalance(owner, transferredAmount);
mockTransferredCoins(transferredAmount);
mockBalance(scAddress, transferredAmount);
dnsAlloc(args.serialize());
});
throws('Domain already exists', () => {
let args = new Args();
args.add(domain);
Expand Down
31 changes: 17 additions & 14 deletions smart-contract/assembly/contracts/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -100,8 +100,8 @@ export function isValidDomain(domain: string): bool {
return true;
}

export function isValidTarget(target: string): bool {
return target.length <= 150;
export function isValidTarget(targetBytes: StaticArray<u8>): bool {
return targetBytes.length <= 150;
}

function domainToTokenIdKey(domainBytes: StaticArray<u8>): StaticArray<u8> {
Expand Down Expand Up @@ -188,12 +188,14 @@ export function dnsAlloc(binaryArgs: StaticArray<u8>): StaticArray<u8> {
.expect('target argument is missing or invalid');
const owner = Context.caller().toString();

const targetBytes = stringToBytes(target);
const domainBytes = stringToBytes(domain);

assert(isValidDomain(domain), 'Invalid domain');
assert(isValidTarget(target), 'Invalid target');
assert(
!Storage.has(domainToTargetKey(stringToBytes(domain))),
'Domain already registered',
);
assert(isValidTarget(targetBytes), 'Invalid target');

const domainToTargetKay = domainToTargetKey(domainBytes);
assert(!Storage.has(domainToTargetKay), 'Domain already registered');

const counterBytes = Storage.get(COUNTER_KEY);

Expand All @@ -203,9 +205,8 @@ export function dnsAlloc(binaryArgs: StaticArray<u8>): StaticArray<u8> {
_update(owner, counter, '');

// Store the domain and token ID
const targetBytes = stringToBytes(target);
const domainBytes = stringToBytes(domain);
Storage.set(domainToTargetKey(domainBytes), targetBytes);

Storage.set(domainToTargetKay, targetBytes);
Storage.set(targetToDomainKey(targetBytes, domainBytes), []);
Storage.set(tokenIdToDomainKey(counterBytes), domainBytes);
Storage.set(domainToTokenIdKey(domainBytes), counterBytes);
Expand Down Expand Up @@ -340,7 +341,8 @@ export function dnsUpdateTarget(binaryArgs: StaticArray<u8>): void {
.nextString()
.expect('target argument is missing or invalid');

assert(isValidTarget(newTarget), 'Invalid target');
const newTargetBytes = stringToBytes(newTarget);
assert(isValidTarget(newTargetBytes), 'Invalid target');

const domainBytes = stringToBytes(domain);
const tokenId = bytesToU256(Storage.get(domainToTokenIdKey(domainBytes)));
Expand All @@ -352,12 +354,13 @@ export function dnsUpdateTarget(binaryArgs: StaticArray<u8>): void {
);

// remove the old target
const oldTarget = Storage.get(domainToTargetKey(domainBytes));
const domainToTargetKay = domainToTargetKey(domainBytes);
const oldTarget = Storage.get(domainToTargetKay);
Storage.del(targetToDomainKey(oldTarget, domainBytes));
// Add the domain to the new target
Storage.set(targetToDomainKey(stringToBytes(newTarget), domainBytes), []);
Storage.set(targetToDomainKey(newTargetBytes, domainBytes), []);
// Update the target for the domain
Storage.set(domainToTargetKey(domainBytes), stringToBytes(newTarget));
Storage.set(domainToTargetKay, newTargetBytes);
}

/**
Expand Down

0 comments on commit 75b5cb3

Please sign in to comment.