2个稳定版本
1.2.0 | 2023年3月3日 |
---|---|
1.1.0 | 2023年3月5日 |
#18 in #metaplex
110KB
2K SLoC
Metaplex令牌授权规则
一个程序,可以创建和执行规则以限制常见的令牌操作,如转让和出售。
概述
授权规则是实现了 Rule
枚举的变体,该枚举实现了 validate()
函数。
有 原始规则 和 组合规则,它们是通过组合一个或多个原始规则创建的。
原始规则 存储用于评估的任何账户或数据,并在运行时基于账户和传递给 validate()
函数的明确定义的 Payload
产生真或假的输出。
组合规则 根据是否任何或所有原始规则返回真来返回真或假。组合规则可以组合成更高层次的组合规则,以实现更复杂的布尔逻辑。由于 Rule
枚举的递归定义,对顶级组合规则上的 validate()
的调用将从顶层开始,并在每个级别进行验证,直到组件原始规则。
环境设置
- 从 https://rustup.rs/ 安装 Rust
- 从 https://docs.solana.com/cli/install-solana-cli-tools#use-solanas-install-tool 安装 Solana
- 运行
yarn install
以安装依赖项
构建和测试 Rust 程序
$ cd program/
$ cargo build-bpf
$ cargo test-bpf
$ cd ..
构建程序,生成 JS API,并重新构建 IDL(使用 Shank 和 Solita)
$ yarn build:rust
$ yarn solita
仅构建 JS SDK(必须先生成)
$ yarn build:sdk
构建程序并生成/构建 IDL/SDK/docs
$ yarn build
启动 Amman 并运行测试脚本
在单独的壳中运行以下命令
$ amman start
然后,运行 Amman 脚本
$ yarn amman
示例
Rust
注意:更多 Rust 示例可以在 program/tests 目录中找到。
use mpl_token_auth_rules::{
instruction::{
builders::{CreateOrUpdateBuilder, ValidateBuilder},
CreateOrUpdateArgs, InstructionBuilder, ValidateArgs,
},
payload::{Payload, PayloadType},
state::{CompareOp, Rule, RuleSetV1},
};
use num_derive::ToPrimitive;
use rmp_serde::Serializer;
use serde::Serialize;
use solana_client::rpc_client::RpcClient;
use solana_sdk::{
instruction::AccountMeta, native_token::LAMPORTS_PER_SOL, signature::Signer,
signer::keypair::Keypair, transaction::Transaction,
};
#[repr(C)]
#[derive(ToPrimitive)]
pub enum Operation {
OwnerTransfer,
Delegate,
SaleTransfer,
}
impl ToString for Operation {
fn to_string(&self) -> String {
match self {
Operation::OwnerTransfer => "OwnerTransfer".to_string(),
Operation::Delegate => "Delegate".to_string(),
Operation::SaleTransfer => "SaleTransfer".to_string(),
}
}
}
fn main() {
let url = "https://api.devnet.solana.com".to_string();
let rpc_client = RpcClient::new(url);
let payer = Keypair::new();
let signature = rpc_client
.request_airdrop(&payer.pubkey(), LAMPORTS_PER_SOL)
.unwrap();
loop {
let confirmed = rpc_client.confirm_transaction(&signature).unwrap();
if confirmed {
break;
}
}
// --------------------------------
// Create RuleSet
// --------------------------------
// Find RuleSet PDA.
let (rule_set_addr, _ruleset_bump) = mpl_token_auth_rules::pda::find_rule_set_address(
payer.pubkey(),
"test rule_set".to_string(),
);
// Additional signer.
let adtl_signer = Keypair::new();
// Create some rules.
let adtl_signer_rule = Rule::AdditionalSigner {
account: adtl_signer.pubkey(),
};
let amount_rule = Rule::Amount {
amount: 1,
operator: CompareOp::LtEq,
field: "Amount".to_string(),
};
let overall_rule = Rule::All {
rules: vec![adtl_signer_rule, amount_rule],
};
// Create a RuleSet.
let mut rule_set = RuleSetV1::new("test rule_set".to_string(), payer.pubkey());
rule_set
.add(Operation::OwnerTransfer.to_string(), overall_rule)
.unwrap();
println!("{:#?}", rule_set);
// Serialize the RuleSet using RMP serde.
let mut serialized_rule_set = Vec::new();
rule_set
.serialize(&mut Serializer::new(&mut serialized_rule_set))
.unwrap();
// Create a `create` instruction.
let create_ix = CreateOrUpdateBuilder::new()
.payer(payer.pubkey())
.rule_set_pda(rule_set_addr)
.build(CreateOrUpdateArgs::V1 {
serialized_rule_set,
})
.unwrap()
.instruction();
// Add it to a transaction.
let latest_blockhash = rpc_client.get_latest_blockhash().unwrap();
let create_tx = Transaction::new_signed_with_payer(
&[create_ix],
Some(&payer.pubkey()),
&[&payer],
latest_blockhash,
);
// Send and confirm transaction.
let signature = rpc_client.send_and_confirm_transaction(&create_tx).unwrap();
println!("Create tx signature: {}", signature);
// --------------------------------
// Validate Operation
// --------------------------------
// Create a Keypair to simulate a token mint address.
let mint = Keypair::new().pubkey();
// Store the payload of data to validate against the rule definition.
let payload = Payload::from([("Amount".to_string(), PayloadType::Number(1))]);
// Create a `validate` instruction with the additional signer.
let validate_ix = ValidateBuilder::new()
.rule_set_pda(rule_set_addr)
.mint(mint)
.additional_rule_accounts(vec![AccountMeta::new_readonly(adtl_signer.pubkey(), true)])
.build(ValidateArgs::V1 {
operation: Operation::OwnerTransfer.to_string(),
payload,
update_rule_state: false,
rule_set_revision: None,
})
.unwrap()
.instruction();
// Add it to a transaction.
let latest_blockhash = rpc_client.get_latest_blockhash().unwrap();
let validate_tx = Transaction::new_signed_with_payer(
&[validate_ix],
Some(&payer.pubkey()),
&[&payer, &adtl_signer],
latest_blockhash,
);
// Send and confirm transaction.
let signature = rpc_client
.send_and_confirm_transaction(&validate_tx)
.unwrap();
println!("Validate tx signature: {}", signature);
}
JavaScript
注意:更多JS示例可以在/cli/源代码中找到,包括/cli/examples/中的示例规则集。
import { encode, decode } from '@msgpack/msgpack';
import { createCreateInstruction, createTokenAuthorizationRules, PREFIX, PROGRAM_ID } from './helpers/mpl-token-auth-rules';
import { Keypair, Connection, PublicKey, Transaction, SystemProgram } from '@solana/web3.js';
export const findRuleSetPDA = async (payer: PublicKey, name: string) => {
return await PublicKey.findProgramAddress(
[
Buffer.from(PREFIX),
payer.toBuffer(),
Buffer.from(name),
],
PROGRAM_ID,
);
}
export const createTokenAuthorizationRules = async (
connection: Connection,
payer: Keypair,
name: string,
data: Uint8Array,
) => {
const ruleSetAddress = await findRuleSetPDA(payer.publicKey, name);
let createIX = createCreateOrUpdateInstruction(
{
payer: payer.publicKey,
ruleSetPda: ruleSetAddress[0],
systemProgram: SystemProgram.programId,
},
{
createOrUpdateArgs: {__kind: "V1", serializedRuleSet: data },
},
PROGRAM_ID,
)
const tx = new Transaction().add(createIX);
const { blockhash } = await connection.getLatestBlockhash();
tx.recentBlockhash = blockhash;
tx.feePayer = payer.publicKey;
const sig = await connection.sendTransaction(tx, [payer], { skipPreflight: true });
await connection.confirmTransaction(sig, "finalized");
return ruleSetAddress[0];
}
const connection = new Connection("<YOUR_RPC_HERE>", "finalized");
let payer = Keypair.generate()
// Encode the file using msgpack so the pre-encoded data can be written directly to a Solana program account
const encoded = encode(JSON.parse(fs.readFileSync("./examples/pubkey_list_match.json")));
// Create the ruleset
await createTokenAuthorizationRules(connection, payer, name, encoded);
依赖项
~18–26MB
~391K SLoC