Skip to content

Commit 68326e7

Browse files
feat(scripts): transfer names to owners in init (#713)
* feat(scripts): transfer names to owners in init * remove log * fix tests * fmt * fixes to the length checking --------- Co-authored-by: Chloe Martin <chloedaughterofmars@gmail.com>
1 parent cbd5881 commit 68326e7

File tree

4 files changed

+66
-19
lines changed

4 files changed

+66
-19
lines changed

packages/iota-names/sources/admin.move

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77
module iota_names::admin;
88

99
use iota::clock::Clock;
10-
use iota::tx_context::sender;
1110
use iota_names::core_config::CoreConfig;
1211
use iota_names::name::{Self, Name};
1312
use iota_names::iota_names::{Self, AdminCap, IotaNames};
@@ -18,6 +17,8 @@ use iota_names::deny_list;
1817

1918
#[error]
2019
const ENoNamesProvided: vector<u8> = b"No names provided";
20+
#[error]
21+
const ENamesRecipientsMismatch: vector<u8> = b"Names and recipients length mismatch";
2122

2223
/// Authorization witness to call protected functions of `iota_names`.
2324
public struct AdminAuth has drop {}
@@ -43,27 +44,30 @@ entry fun register_names(
4344
_: &AdminCap,
4445
iota_names: &mut IotaNames,
4546
names: vector<String>,
47+
mut recipients: vector<address>,
4648
no_years: u8,
4749
clock: &Clock,
4850
ctx: &mut TxContext,
4951
) {
5052
assert!(!names.is_empty(), ENoNamesProvided);
51-
let sender = sender(ctx);
53+
assert!(names.length() == recipients.length(), ENamesRecipientsMismatch);
5254
let config = *iota_names.get_config<CoreConfig>();
5355
let mut name_structs = vector::empty<Name>();
5456
let mut tmp_names = names;
5557
while (!tmp_names.is_empty()) {
5658
let n = name::new(tmp_names.pop_back());
5759
name_structs.push_back(n);
5860
};
61+
recipients.reverse();
5962
deny_list::remove_reserved_labels_for_names(iota_names, &name_structs);
6063
// Add records
6164
let registry = iota_names::auth_registry_mut<AdminAuth, Registry>(AdminAuth {}, iota_names);
6265
while (!name_structs.is_empty()) {
6366
let name = name_structs.pop_back();
67+
let recipient = recipients.pop_back();
6468
config.assert_is_valid_for_sale(&name);
6569
let nft = registry.add_record(name, no_years, clock, ctx);
66-
iota::transfer::public_transfer(nft, sender);
70+
iota::transfer::public_transfer(nft, recipient);
6771
};
6872
}
6973

packages/iota-names/tests/unit/admin_tests.move

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,10 @@ fun register_names_from_deny_list() {
121121
utf8(b"one.iota"),
122122
utf8(b"two.iota"),
123123
];
124+
let recipients = vector[
125+
ctx.sender(),
126+
ctx.sender(),
127+
];
124128

125129
{
126130
scenario.next_tx(ctx.sender());
@@ -143,6 +147,7 @@ fun register_names_from_deny_list() {
143147
&admin_cap,
144148
&mut iota_names,
145149
copy names,
150+
recipients,
146151
1,
147152
&clock,
148153
scenario.ctx()
@@ -185,6 +190,12 @@ fun register_multiple() {
185190
utf8(b"name3.iota"),
186191
utf8(b"name4.iota")
187192
];
193+
let recipients = vector[
194+
ctx.sender(),
195+
ctx.sender(),
196+
ctx.sender(),
197+
ctx.sender(),
198+
];
188199

189200
{
190201
scenario.next_tx(ctx.sender());
@@ -201,6 +212,7 @@ fun register_multiple() {
201212
&admin_cap,
202213
&mut iota_names,
203214
copy names,
215+
recipients,
204216
1,
205217
&clock,
206218
scenario.ctx()
@@ -242,6 +254,11 @@ fun test_admin_remove_existing_records() {
242254
utf8(b"name2.iota"),
243255
utf8(b"name3.iota"),
244256
];
257+
let recipients = vector[
258+
ctx.sender(),
259+
ctx.sender(),
260+
ctx.sender(),
261+
];
245262

246263
// Register the names first
247264
{
@@ -259,6 +276,7 @@ fun test_admin_remove_existing_records() {
259276
&admin_cap,
260277
&mut iota_names,
261278
copy names_to_register,
279+
recipients,
262280
1,
263281
&clock,
264282
scenario.ctx()
@@ -335,6 +353,10 @@ fun test_admin_remove_mixed_records() {
335353
utf8(b"existing1.iota"),
336354
utf8(b"existing2.iota"),
337355
];
356+
let recipients = vector[
357+
ctx.sender(),
358+
ctx.sender()
359+
];
338360

339361
let mixed_names = vector[
340362
utf8(b"existing1.iota"),
@@ -358,6 +380,7 @@ fun test_admin_remove_mixed_records() {
358380
&admin_cap,
359381
&mut iota_names,
360382
copy existing_names,
383+
recipients,
361384
1,
362385
&clock,
363386
scenario.ctx()
@@ -445,6 +468,7 @@ fun test_register_names_empty_list_fails() {
445468
&admin_cap,
446469
&mut iota_names,
447470
vector::empty<std::string::String>(),
471+
vector::empty<address>(),
448472
1,
449473
&clock,
450474
scenario.ctx()

scripts/init/init.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,13 @@ export const init = async (
6262
// Register names
6363
let namesToRegisterFile = './init/names-to-register.csv';
6464
if (fs.existsSync(namesToRegisterFile)) {
65-
await processNamesFileBatched(namesToRegisterFile, packageInfo, network, client);
65+
await processNamesFileBatched(
66+
namesToRegisterFile,
67+
packageInfo,
68+
network,
69+
client,
70+
newOwner || packageInfo.adminAddress,
71+
);
6672
}
6773

6874
if (!newOwner) {

scripts/reserved-names/register-names.ts

Lines changed: 28 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
// SPDX-License-Identifier: Apache-2.0
33

44
import fs from 'fs';
5+
import { bcs } from '@iota/iota-sdk/bcs';
56
import { Transaction } from '@iota/iota-sdk/transactions';
67
import { IOTA_CLOCK_OBJECT_ID, isValidIotaAddress } from '@iota/iota-sdk/utils';
78

@@ -43,17 +44,21 @@ export const parseCsvFile = (filePath: string): Record<string, string | undefine
4344

4445
export const registerNames = (
4546
txb: Transaction,
46-
names: string[],
47+
batch: Record<string, string>,
4748
iotaNamesPackageId: string,
4849
adminCap: string,
4950
iotaNamesObjectId: string,
5051
) => {
51-
return txb.moveCall({
52+
var names = Object.keys(batch);
53+
var recipients = names.map((name) => batch[name]);
54+
55+
txb.moveCall({
5256
target: `${iotaNamesPackageId}::admin::register_names`,
5357
arguments: [
5458
txb.object(adminCap),
5559
txb.object(iotaNamesObjectId),
5660
txb.pure.vector('string', names),
61+
txb.pure(bcs.vector(bcs.Address).serialize(recipients)),
5762
txb.pure.u8(YEARS_TO_RESERVE),
5863
txb.object(IOTA_CLOCK_OBJECT_ID),
5964
],
@@ -73,34 +78,42 @@ export async function processNamesFileBatched(
7378
},
7479
network: string,
7580
client: any,
81+
adminAddress: string,
7682
) {
7783
const nameAddressPairs = parseCsvFile(filePath);
78-
const allNames = Object.keys(nameAddressPairs);
79-
let batch: string[] = [];
80-
let batchLen = 0;
81-
for (const name of allNames) {
82-
const nameLen = name.length + (batch.length > 0 ? 1 : 0);
83-
if (batchLen + nameLen > PURE_ARG_SIZE_LIMIT) {
84+
let batch: Record<string, string> = {};
85+
let batchNameLen = 0;
86+
let batchRecipientLen = 0;
87+
for (const [name, address] of Object.entries(nameAddressPairs)) {
88+
var recipient = address || adminAddress;
89+
const nameLen = bcs.String.serializedSize(name)!;
90+
const recipientLen = bcs.Address.serializedSize(recipient)!;
91+
if (
92+
batchNameLen + nameLen > PURE_ARG_SIZE_LIMIT ||
93+
batchRecipientLen + recipientLen > PURE_ARG_SIZE_LIMIT
94+
) {
8495
await sendNamesBatchTransaction(batch, packageInfo, network, client);
85-
batch = [];
86-
batchLen = 0;
96+
batch = {};
97+
batchNameLen = 0;
98+
batchRecipientLen = 0;
8799
}
88-
batch.push(name);
89-
batchLen += nameLen;
100+
batch[name] = recipient;
101+
batchNameLen += nameLen;
102+
batchRecipientLen += recipientLen;
90103
}
91-
if (batch.length > 0) {
104+
if (Object.keys(batch).length > 0) {
92105
await sendNamesBatchTransaction(batch, packageInfo, network, client);
93106
}
94107
}
95108

96109
async function sendNamesBatchTransaction(
97-
batch: string[],
110+
batch: Record<string, string>,
98111
packageInfo: { packageId: string; adminCap: string; iotaNamesObjectId: string },
99112
network: string,
100113
client: any,
101114
) {
102115
const tx = new Transaction();
103-
console.log(`Registering ${batch.length} names`);
116+
console.log(`Registering ${Object.keys(batch).length} names`);
104117
registerNames(
105118
tx,
106119
batch,

0 commit comments

Comments
 (0)