-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
109 lines (99 loc) · 2.89 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
require('dotenv').config()
const { program } = require("commander");
const { getOrCreateAssociatedTokenAccount } = require('@solana/spl-token');
const { clusterApiUrl, Connection, Keypair, PublicKey } = require('@solana/web3.js');
const {
registerDomainName,
transferNameOwnership,
NameRegistryState,
getDomainKey,
SOL_TLD_AUTHORITY
} = require("@bonfida/spl-name-service")
const bs58 = require("bs58");
program.version("0.0.1")
const SECRET_KEY = process.env.ADMIN_WALLET_PRIVATE_KEY
const wallet = Keypair.fromSecretKey(bs58.decode(SECRET_KEY));
const usdc_contract_address = new PublicKey(process.env.USDC_CONTRACT_ADDRESS)
const connection = new Connection(
clusterApiUrl(process.env.CLUSTER),
'confirmed'
);
const domainLookup = async (domain_sol) => {
try {
const { pubkey } = await getDomainKey(domain_sol);
// Step 2
// The registry object contains all the info about the domain name
// The NFT owner is of type PublicKey | undefined
const { registry, nftOwner } = await NameRegistryState.retrieve(
connection,
pubkey
);
console.log(registry)
} catch(e) {
console.log(e.message)
}
}
const registerSOLDomain = async (domain_sol) => {
try {
const space = 1 * 1_000; // We want a 1kB sized domain (max 10kB)
const buyer = wallet.publicKey; // Publickey of the buyer
const buyerTokenAccount = await getOrCreateAssociatedTokenAccount(
connection,
wallet,
usdc_contract_address,
wallet.publicKey
)
console.log(buyer.toBase58())
console.log(buyerTokenAccount.address.toBase58())
const [, ix] = await registerDomainName(
connection,
domain_sol,
space,
buyer,
buyerTokenAccount.address
);
console.log(ix)
return true
} catch(e) {
console.log(e.message)
return false
}
}
const transferSOLDomain = async (domain_sol, phantomWalletDestinatiob) => {
try {
// New owner of the domain
const newOwner = new PublicKey(phantomWalletDestinatiob);
const ix = await transferNameOwnership(
connection,
domain_sol,
newOwner,
undefined,
SOL_TLD_AUTHORITY
);
console.log(ix)
return true;
} catch(e) {
console.log(e.message)
return false;
}
}
program.command("lookup_domain")
.requiredOption("-n, --name <string>", "domain name")
.action(async (directory, cmd) => {
const { name } = cmd.opts();
domainLookup(name)
});
program.command("register_domain")
.requiredOption("-n, --name <string>", "domain name")
.action(async (directory, cmd) => {
const { name } = cmd.opts();
registerSOLDomain(name)
});
program.command("transfer_domain")
.requiredOption("-n, --name <string>", "domain name")
.requiredOption("-d, --destination <string>", "destinamtion address")
.action(async (directory, cmd) => {
const { name, destination } = cmd.opts();
transferSOLDomain(name, destination)
});
program.parse(process.argv);