This repository has been archived by the owner on Jan 10, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add name-service program and js bindings (#1600)
- Loading branch information
Showing
23 changed files
with
6,469 additions
and
4 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
# SPL Name Service | ||
|
||
A spl program for issuing and managing ownership of: domain names, Solana Pubkeys, URLs, twitter handles, ipfs cid's etc.. | ||
|
||
This program could be used for dns, pubkey etc lookups via a browser extension for example, the goal is to create an easy way to identify Solana public keys with various links. | ||
Broader use cases are also imaginable. | ||
|
||
Key points: | ||
- A Name is a string that maps to a record (program derived account) which can hold data. | ||
- Each name is of a certain class and has a certain owner, both are identified by their pubkeys. The class of a name needs to sign the issuance of it. | ||
- A name can have a parent name that is identified by the address of its record. The owner of the parent name (when it exists) needs to sign the issuance of the child name. | ||
- The data of a name registry is controlled byb the class keypair or, when it is set to `Pubkey::default()`, by the name owner keypair. | ||
- Only the owner can delete a name registry. | ||
|
||
|
||
Remarks and use cases: | ||
- Domain name declarations: One could arbitrarly set-up a class that we can call Top-Level-Domain names. Names in this class can only be issued with the permission of the class keypair, ie the administrator, who can enforce that TLD names are of the type `".something"`. From then on one could create and own the TLD `".sol"` and create a class of ".sol" sub-domains, administrating the issuance of the `"something.sol"` sub-domains that way (by setting tha parent name to the address of the `".sol"` registry).\ | ||
An off-chain browser extension could then, similarly to DNS, parse the user SPL name service URL input and descend the chain of names, verifying that the names exist with the correct parenthood, and finally use the data of the last child name (or also a combination of the parents data) in order to resolve this call towards a real DNS URL or any kind of data.\ | ||
Although the ownership and class system makes the administration a given class centralized, the creation of new classes is permissionless and as a class owner any kind of decentralized governance signing program could be used. | ||
|
||
- Twitter handles can be added as names of one specific name class. The class authority of will therefore hold the right to add a twitter handle name. This enables the verification of twitter accounts for example by asking the user to tweet his pubkey or a signed message. A bot that holds the private issuing authority key can then sign the Create instruction (with a metadata_authority that is the tweeted pubkey) and send it back to the user who will then submit it to the program.\ | ||
In this case the class will still be able to control the data of the name registry, and not the user for example. | ||
Therefore, another way of using this program would be to create a name (`"verified-twitter-handles"` for example) with the `Pubkey::default()` class and with the owner being the authority. That way verified twitter names could be issued as child names of this parent by the owner, leaving the user as being to able to modify the data of his twitter name registry. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
# Generated by Cargo | ||
# will have compiled files and executables | ||
src/target/ | ||
|
||
# Remove Cargo.lock from gitignore if creating an executable, leave it for libraries | ||
# More information here https://doc.rust-lang.org/cargo/guide/cargo-toml-vs-cargo-lock.html | ||
Cargo.lock | ||
|
||
# These are backup files generated by rustfmt | ||
**/*.rs.bk | ||
|
||
target | ||
.vscode | ||
hfuzz* | ||
third_party/ | ||
|
||
*/node_modules | ||
js/dist | ||
js/lib | ||
js/docs | ||
js/src/secret.ts |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
# Name Service program | ||
|
||
A spl program for issuing and managing ownership of: domain names, Solana Pubkeys, URLs, twitter handles, ipfs cid's, metadata, etc.. | ||
|
||
This program provides an interface and implementation that third parties can | ||
utilize to create and use their own version of a name service of any kind. | ||
|
||
Full documentation is available at https://spl.solana.com/name-service | ||
|
||
JavaScript binding are available in the `./js` directory. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
{ | ||
"name": "spl-name-service", | ||
"version": "0.1.0", | ||
"license": "MIT", | ||
"repository": { | ||
"type": "git", | ||
"url": "https://github.com/solana-labs/solana-program-library" | ||
}, | ||
"main": "dist/index.js", | ||
"types": "dist/index.d.ts", | ||
"scripts": { | ||
"dev": "tsc && node --trace-warnings dist/test.js", | ||
"build": "tsc", | ||
"prepublish": "tsc", | ||
"lint": "yarn pretty && eslint .", | ||
"lint:fix": "yarn pretty:fix && eslint . --fix", | ||
"pretty": "prettier --check 'src/*.[jt]s'", | ||
"pretty:fix": "prettier --write 'src/*.[jt]s'", | ||
"doc": "yarn typedoc src/index.ts" | ||
}, | ||
"devDependencies": { | ||
"@tsconfig/recommended": "^1.0.1", | ||
"@types/bs58": "^4.0.1", | ||
"@types/node": "^14.14.20", | ||
"babel-eslint": "^10.1.0", | ||
"eslint": "^7.17.0", | ||
"eslint-plugin-import": "^2.22.1", | ||
"nodemon": "^2.0.7", | ||
"prettier": "^2.2.1", | ||
"save-dev": "0.0.1-security", | ||
"ts-node": "^9.1.1", | ||
"tslib": "^2.2.0", | ||
"typedoc": "^0.20.35", | ||
"typescript": "^4.1.3" | ||
}, | ||
"dependencies": { | ||
"@project-serum/sol-wallet-adapter": "^0.1.5", | ||
"@solana/spl-token": "0.1.3", | ||
"@solana/web3.js": "^1.2.6", | ||
"bip32": "^2.0.6", | ||
"bn.js": "^5.1.3", | ||
"@bonfida/borsh-js": "^0.3.1", | ||
"bs58": "4.0.1", | ||
"buffer-layout": "^1.2.0", | ||
"core-util-is": "^1.0.2", | ||
"crypto": "^1.0.1", | ||
"crypto-ts": "^1.0.2", | ||
"fs": "^0.0.1-security", | ||
"tweetnacl": "^1.0.3", | ||
"webpack-dev-server": "^3.11.2" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,190 @@ | ||
import { | ||
Account, | ||
Connection, | ||
PublicKey, | ||
SystemProgram, | ||
TransactionInstruction, | ||
} from "@solana/web3.js"; | ||
import { Numberu64 } from "./utils"; | ||
import { updateInstruction, transferInstruction, createInstruction, deleteInstruction } from "./instructions"; | ||
import { createHash, HashOptions } from 'crypto'; | ||
import { getHashedName, getNameAccountKey, getNameOwner, Numberu32 } from "."; | ||
import { hash } from "tweetnacl"; | ||
import { NameRegistryState } from "./state"; | ||
|
||
//////////////////////////////////////////////////////////// | ||
|
||
export const NAME_PROGRAM_ID = new PublicKey( | ||
"Gh9eN9nDuS3ysmAkKf4QJ6yBzf3YNqsn6MD8Ms3TsXmA" | ||
); | ||
export const HASH_PREFIX = "SPL Name Service"; | ||
|
||
//////////////////////////////////////////////////////////// | ||
|
||
/** | ||
* Creates a name account with the given rent budget, allocated space, owner and class. | ||
* | ||
* @param connection The solana connection object to the RPC node | ||
* @param name The name of the new account | ||
* @param space The space in bytes allocated to the account | ||
* @param payerKey The allocation cost payer | ||
* @param nameOwner The pubkey to be set as owner of the new name account | ||
* @param lamports The budget to be set for the name account. If not specified, it'll be the minimum for rent exemption | ||
* @param nameClass The class of this new name | ||
* @param parentName The parent name of the new name. If specified its owner needs to sign | ||
* @returns | ||
*/ | ||
export async function createNameRegistry( | ||
connection: Connection, | ||
name: string, | ||
space: number, | ||
payerKey: PublicKey, | ||
nameOwner: PublicKey, | ||
lamports?: number, | ||
nameClass?: PublicKey, | ||
parentName?: PublicKey, | ||
): Promise<TransactionInstruction> { | ||
let hashed_name = await getHashedName(name); | ||
let nameAccountKey = await getNameAccountKey(hashed_name, nameClass, parentName); | ||
|
||
let balance = lamports | ||
? lamports | ||
: await connection.getMinimumBalanceForRentExemption(space); | ||
|
||
let nameParentOwner: PublicKey | undefined; | ||
if (!!parentName) { | ||
let parentAccount = await getNameOwner(connection, parentName); | ||
} | ||
|
||
let createNameInstr = createInstruction( | ||
NAME_PROGRAM_ID, | ||
SystemProgram.programId, | ||
nameAccountKey, | ||
nameOwner, | ||
payerKey, | ||
hashed_name, | ||
new Numberu64(balance), | ||
new Numberu32(space), | ||
nameClass, | ||
parentName, | ||
nameParentOwner | ||
); | ||
|
||
return createNameInstr; | ||
} | ||
|
||
/** | ||
* Overwrite the data of the given name registry. | ||
* | ||
* @param connection The solana connection object to the RPC node | ||
* @param name The name of the name registry to update | ||
* @param offset The offset to which the data should be written into the registry | ||
* @param input_data The data to be written | ||
* @param nameClass The class of this name, if it exsists | ||
* @param nameParent The parent name of this name, if it exists | ||
*/ | ||
export async function updateNameRegistryData( | ||
connection: Connection, | ||
name: string, | ||
offset: number, | ||
input_data: Buffer, | ||
nameClass?: PublicKey, | ||
nameParent?: PublicKey, | ||
): Promise<TransactionInstruction> { | ||
let hashed_name = await getHashedName(name); | ||
let nameAccountKey = await getNameAccountKey(hashed_name, nameClass, nameParent); | ||
|
||
let signer: PublicKey; | ||
if (!!nameClass) { | ||
signer = nameClass; | ||
} else { | ||
signer = (await NameRegistryState.retrieve(connection, nameAccountKey)).owner; | ||
} | ||
|
||
let updateInstr = updateInstruction( | ||
NAME_PROGRAM_ID, | ||
nameAccountKey, | ||
new Numberu32(offset), | ||
input_data, | ||
signer | ||
); | ||
|
||
return updateInstr; | ||
} | ||
|
||
/** | ||
* Cahnge the owner of a given name account. | ||
* | ||
* @param connection The solana connection object to the RPC node | ||
* @param name The name of the name account | ||
* @param newOwner The new owner to be set | ||
* @param curentNameOwner the current name Owner | ||
* @param nameClass The class of this name, if it exsists | ||
* @param nameParent The parent name of this name, if it exists | ||
* @returns | ||
*/ | ||
export async function transferNameOwnership( | ||
connection: Connection, | ||
name: string, | ||
newOwner: PublicKey, | ||
currentNameOwner: PublicKey, | ||
nameClass?: PublicKey, | ||
nameParent?: PublicKey, | ||
): Promise<TransactionInstruction> { | ||
let hashed_name = await getHashedName(name); | ||
let nameAccountKey = await getNameAccountKey(hashed_name, nameClass, nameParent); | ||
|
||
let curentNameOwner: PublicKey; | ||
if (!!nameClass) { | ||
curentNameOwner = nameClass; | ||
} else { | ||
curentNameOwner = (await NameRegistryState.retrieve(connection, nameAccountKey)).owner; | ||
} | ||
|
||
let transferInstr = transferInstruction( | ||
NAME_PROGRAM_ID, | ||
nameAccountKey, | ||
newOwner, | ||
curentNameOwner, | ||
nameClass | ||
); | ||
|
||
return transferInstr; | ||
} | ||
|
||
/** | ||
* Delete the name account and transfer the rent to the target. | ||
* | ||
* @param connection The solana connection object to the RPC node | ||
* @param name The name of the name account | ||
* @param refundTargetKey The refund destination address | ||
* @param nameClass The class of this name, if it exsists | ||
* @param nameParent The parent name of this name, if it exists | ||
* @returns | ||
*/ | ||
export async function deleteNameRegistry( | ||
connection: Connection, | ||
name: string, | ||
refundTargetKey: PublicKey, | ||
nameClass?: PublicKey, | ||
nameParent?: PublicKey, | ||
): Promise<TransactionInstruction> { | ||
let hashed_name = await getHashedName(name); | ||
let nameAccountKey = await getNameAccountKey(hashed_name, nameClass, nameParent); | ||
|
||
let nameOwner: PublicKey; | ||
if (!!nameClass) { | ||
nameOwner = nameClass; | ||
} else { | ||
nameOwner = (await NameRegistryState.retrieve(connection, nameAccountKey)).owner; | ||
} | ||
|
||
let changeAuthoritiesInstr = deleteInstruction( | ||
NAME_PROGRAM_ID, | ||
nameAccountKey, | ||
refundTargetKey, | ||
nameOwner | ||
); | ||
|
||
return changeAuthoritiesInstr; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
export * from "./utils"; | ||
export * from "./bindings"; |
Oops, something went wrong.