Обновить формат Solana user PDA

This commit is contained in:
AidarKC
2026-05-24 19:41:13 +03:00
parent 74df7e2645
commit baef264bd0
7 changed files with 861 additions and 411 deletions
+150 -76
View File
@@ -4,7 +4,6 @@ import {
Ed25519Program,
PublicKey,
SYSVAR_INSTRUCTIONS_PUBKEY,
SystemProgram,
Transaction,
} from "@solana/web3.js";
import { createHash } from "crypto";
@@ -12,23 +11,36 @@ import { expect } from "chai";
import { Shine } from "../target/types/shine";
const MAGIC = Buffer.from("SHiNE", "utf8");
const FORMAT_MAJOR = 1;
const FORMAT_MAJOR = 2;
const FORMAT_MINOR = 0;
const RESERVED = Buffer.from([0, 0, 0, 0, 0]);
const ZERO_HASH = Buffer.alloc(32, 0);
const KEY_STATUS_CREATED = 0;
const LAST_BLOCK_STATE_PREFIX = Buffer.from("SHiNE_LAST_BLOCK", "utf8");
const BLOCK_TYPE_ROOT_KEY = 1;
const BLOCK_TYPE_DEVICE_KEY = 2;
const BLOCK_TYPE_BLOCKCHAIN_REGISTRY = 3;
const BLOCK_TYPE_SERVER_PROFILE = 30;
const BLOCK_TYPE_ACCESS_SERVERS = 40;
const BLOCK_TYPE_TRUSTED_STATE = 50;
const BLOCK_VERSION_0 = 0;
const BLOCKCHAIN_TYPE_MAIN_USER = 1;
const LIMIT_STEP = 10_000n;
const START_BONUS_LIMIT = 100_000n;
const USERS_ECONOMY_CONFIG_SEED = "shine_users_economy_config";
const SHINE_PAYMENTS_PROGRAM_ID = new PublicKey("m48pWRGWrMj3TEHjuU4zsp5Gju4e7ZaPovk8RcVt7kR");
const SHINE_LOGIN_GUARD_PROGRAM_ID = new PublicKey("3xkopA7cXagxzMFrKdv3NCBfV6BKiRJCk69kr27M2sRo");
const SHINE_PAYMENTS_PROGRAM_ID = new PublicKey(
"m48pWRGWrMj3TEHjuU4zsp5Gju4e7ZaPovk8RcVt7kR"
);
const SHINE_PAYMENTS_INFLOW_VAULT_SEED = "shine_payments_inflow_vault";
type MutableFields = {
blockchainKey: PublicKey;
deviceKey: PublicKey;
chainNumber: number;
blockchainPublicKey: PublicKey;
blockchainName: string;
usedBytes: bigint;
lastBlockNumber: number;
lastBlockHash: Buffer;
lastBlockSignature: Buffer;
arweaveTxId: string;
isServer: boolean;
serverKey: PublicKey;
serverAddress: string;
@@ -40,17 +52,22 @@ type MutableFields = {
type UnsignedRecord = {
createdAtMs: bigint;
updatedAtMs: bigint;
version: number;
prevHash: Buffer;
recordNumber: number;
prevRecordHash: Buffer;
login: string;
rootKeyStatus: number;
rootKey: PublicKey;
blockchainKeyStatus: number;
blockchainKey: PublicKey;
deviceKeyStatus: number;
deviceKey: PublicKey;
chainNumber: number;
balance: bigint;
blockchain: {
blockchainType: number;
blockchainName: string;
blockchainPublicKey: PublicKey;
paidLimitBytes: bigint;
usedBytes: bigint;
lastBlockNumber: number;
lastBlockHash: Buffer;
lastBlockSignature: Buffer;
arweaveTxId: string;
};
isServer: boolean;
serverKey: PublicKey;
serverAddress: string;
@@ -59,12 +76,6 @@ type UnsignedRecord = {
trustedCount: number;
};
function u16le(v: number): Buffer {
const b = Buffer.alloc(2);
b.writeUInt16LE(v, 0);
return b;
}
function u32le(v: number): Buffer {
const b = Buffer.alloc(4);
b.writeUInt32LE(v, 0);
@@ -77,10 +88,12 @@ function u64le(v: bigint): Buffer {
return b;
}
function serializeUnsignedRecord(r: UnsignedRecord): Buffer {
const loginBytes = Buffer.from(r.login, "utf8");
const serverAddressBytes = Buffer.from(r.serverAddress, "utf8");
function strBytes(value: string): Buffer {
const bytes = Buffer.from(value, "utf8");
return Buffer.concat([Buffer.from([bytes.length]), bytes]);
}
function serializeUnsignedRecord(r: UnsignedRecord): Buffer {
const out: Buffer[] = [];
out.push(MAGIC);
out.push(Buffer.from([FORMAT_MAJOR]));
@@ -89,44 +102,48 @@ function serializeUnsignedRecord(r: UnsignedRecord): Buffer {
out.push(u64le(r.createdAtMs));
out.push(u64le(r.updatedAtMs));
out.push(u32le(r.version));
out.push(r.prevHash);
out.push(u32le(r.recordNumber));
out.push(r.prevRecordHash);
out.push(strBytes(r.login));
out.push(Buffer.from([loginBytes.length]));
out.push(loginBytes);
out.push(Buffer.from([r.rootKeyStatus]));
out.push(Buffer.from([r.isServer ? 6 : 5]));
out.push(Buffer.from([BLOCK_TYPE_ROOT_KEY, BLOCK_VERSION_0]));
out.push(r.rootKey.toBuffer());
out.push(Buffer.from([r.blockchainKeyStatus]));
out.push(r.blockchainKey.toBuffer());
out.push(Buffer.from([r.deviceKeyStatus]));
out.push(Buffer.from([BLOCK_TYPE_DEVICE_KEY, BLOCK_VERSION_0]));
out.push(r.deviceKey.toBuffer());
out.push(Buffer.from([BLOCK_TYPE_BLOCKCHAIN_REGISTRY, BLOCK_VERSION_0, 1]));
out.push(Buffer.from([r.blockchain.blockchainType]));
out.push(strBytes(r.blockchain.blockchainName));
out.push(r.blockchain.blockchainPublicKey.toBuffer());
out.push(u64le(r.blockchain.paidLimitBytes));
out.push(u64le(r.blockchain.usedBytes));
out.push(u32le(r.blockchain.lastBlockNumber));
out.push(r.blockchain.lastBlockHash);
out.push(r.blockchain.lastBlockSignature);
if (r.blockchain.arweaveTxId.length === 0) {
out.push(Buffer.from([0]));
} else {
out.push(Buffer.from([1]));
out.push(strBytes(r.blockchain.arweaveTxId));
}
out.push(u16le(r.chainNumber));
out.push(u64le(r.balance));
out.push(Buffer.from([r.isServer ? 1 : 0]));
if (r.isServer) {
out.push(Buffer.from([BLOCK_TYPE_SERVER_PROFILE, BLOCK_VERSION_0, 1]));
out.push(r.serverKey.toBuffer());
out.push(Buffer.from([serverAddressBytes.length]));
out.push(serverAddressBytes);
out.push(strBytes(r.serverAddress));
out.push(Buffer.from([r.syncServers.length]));
for (const s of r.syncServers) {
const sb = Buffer.from(s, "utf8");
out.push(Buffer.from([sb.length]));
out.push(sb);
out.push(strBytes(s));
}
}
out.push(Buffer.from([BLOCK_TYPE_ACCESS_SERVERS, BLOCK_VERSION_0]));
out.push(Buffer.from([r.accessServers.length]));
for (const s of r.accessServers) {
const sb = Buffer.from(s, "utf8");
out.push(Buffer.from([sb.length]));
out.push(sb);
out.push(strBytes(s));
}
out.push(Buffer.from([BLOCK_TYPE_TRUSTED_STATE, BLOCK_VERSION_0]));
out.push(Buffer.from([r.trustedCount]));
out.push(RESERVED);
const unsigned = Buffer.concat(out);
const recordLen = unsigned.length + 64;
@@ -138,6 +155,17 @@ function sha256(buf: Buffer): Buffer {
return createHash("sha256").update(buf).digest();
}
function serializeLastBlockState(r: UnsignedRecord): Buffer {
return Buffer.concat([
LAST_BLOCK_STATE_PREFIX,
strBytes(r.login),
strBytes(r.blockchain.blockchainName),
u32le(r.blockchain.lastBlockNumber),
r.blockchain.lastBlockHash,
u64le(r.blockchain.usedBytes),
]);
}
function extractSigFromEdIx(ixData: Buffer): Buffer {
const signatureOffset = ixData.readUInt16LE(2);
return ixData.subarray(signatureOffset, signatureOffset + 64);
@@ -163,23 +191,25 @@ describe("shine_users e2e", () => {
SHINE_PAYMENTS_PROGRAM_ID
);
const economyAi = await provider.connection.getAccountInfo(usersEconomyConfigPda);
const economyAi = await provider.connection.getAccountInfo(
usersEconomyConfigPda
);
if (!economyAi) {
await program.methods
.initUsersEconomyConfig()
.accounts({
signer: provider.wallet.publicKey,
usersEconomyConfigPda,
systemProgram: SystemProgram.programId,
})
.rpc();
}
const root = anchor.web3.Keypair.generate();
const blockchainKey = anchor.web3.Keypair.generate().publicKey;
const blockchain = anchor.web3.Keypair.generate();
const deviceKey = anchor.web3.Keypair.generate().publicKey;
const serverKey1 = anchor.web3.Keypair.generate().publicKey;
const serverKey2 = anchor.web3.Keypair.generate().publicKey;
const blockchainName = `${login}-001`;
const createdAtMs = BigInt(Date.now());
const additionalLimitCreate = 20_000n;
@@ -188,17 +218,22 @@ describe("shine_users e2e", () => {
const createRecord: UnsignedRecord = {
createdAtMs,
updatedAtMs: createdAtMs,
version: 0,
prevHash: ZERO_HASH,
recordNumber: 0,
prevRecordHash: ZERO_HASH,
login,
rootKeyStatus: KEY_STATUS_CREATED,
rootKey: root.publicKey,
blockchainKeyStatus: KEY_STATUS_CREATED,
blockchainKey,
deviceKeyStatus: KEY_STATUS_CREATED,
deviceKey,
chainNumber: 1,
balance: START_BONUS_LIMIT + additionalLimitCreate,
blockchain: {
blockchainType: BLOCKCHAIN_TYPE_MAIN_USER,
blockchainName,
blockchainPublicKey: blockchain.publicKey,
paidLimitBytes: START_BONUS_LIMIT + additionalLimitCreate,
usedBytes: 0n,
lastBlockNumber: 0,
lastBlockHash: ZERO_HASH,
lastBlockSignature: Buffer.alloc(64, 0),
arweaveTxId: "",
},
isServer: true,
serverKey: serverKey1,
serverAddress: "https://srv-1.local",
@@ -206,6 +241,14 @@ describe("shine_users e2e", () => {
accessServers: ["access_srv_1"],
trustedCount: 0,
};
const createLastBlockHash = sha256(serializeLastBlockState(createRecord));
const createLastBlockEdIx = Ed25519Program.createInstructionWithPrivateKey({
privateKey: blockchain.secretKey,
message: createLastBlockHash,
});
createRecord.blockchain.lastBlockSignature = extractSigFromEdIx(
Buffer.from(createLastBlockEdIx.data)
);
const createUnsigned = serializeUnsignedRecord(createRecord);
const createHash = sha256(createUnsigned);
@@ -222,9 +265,16 @@ describe("shine_users e2e", () => {
createdAtMs: new anchor.BN(createdAtMs.toString()),
additionalLimit: new anchor.BN(additionalLimitCreate.toString()),
fields: {
blockchainKey,
deviceKey,
chainNumber: 1,
blockchainPublicKey: blockchain.publicKey,
blockchainName,
usedBytes: new anchor.BN(
createRecord.blockchain.usedBytes.toString()
),
lastBlockNumber: createRecord.blockchain.lastBlockNumber,
lastBlockHash: createRecord.blockchain.lastBlockHash,
lastBlockSignature: createRecord.blockchain.lastBlockSignature,
arweaveTxId: "",
isServer: true,
serverKey: serverKey1,
serverAddress: "https://srv-1.local",
@@ -237,15 +287,16 @@ describe("shine_users e2e", () => {
.accounts({
signer: provider.wallet.publicKey,
userPda,
systemProgram: SystemProgram.programId,
inflowVault: inflowVaultPda,
instructions: SYSVAR_INSTRUCTIONS_PUBKEY,
usersEconomyConfigPda,
loginGuardProgram: SHINE_LOGIN_GUARD_PROGRAM_ID,
})
.instruction();
await provider.sendAndConfirm(new Transaction().add(createEdIx, createIx), []);
await provider.sendAndConfirm(
new Transaction().add(createLastBlockEdIx, createEdIx, createIx),
[]
);
const createAcc = await provider.connection.getAccountInfo(userPda);
expect(createAcc).not.eq(null);
@@ -257,17 +308,23 @@ describe("shine_users e2e", () => {
const updateRecord: UnsignedRecord = {
createdAtMs,
updatedAtMs: createdAtMs + 1_000n,
version: 1,
prevHash: sha256(createUnsigned),
recordNumber: 1,
prevRecordHash: sha256(createUnsigned),
login,
rootKeyStatus: KEY_STATUS_CREATED,
rootKey: root.publicKey,
blockchainKeyStatus: KEY_STATUS_CREATED,
blockchainKey: anchor.web3.Keypair.generate().publicKey,
deviceKeyStatus: KEY_STATUS_CREATED,
deviceKey: anchor.web3.Keypair.generate().publicKey,
chainNumber: 1,
balance: START_BONUS_LIMIT + additionalLimitCreate + additionalLimitUpdate,
blockchain: {
blockchainType: BLOCKCHAIN_TYPE_MAIN_USER,
blockchainName,
blockchainPublicKey: blockchain.publicKey,
paidLimitBytes:
START_BONUS_LIMIT + additionalLimitCreate + additionalLimitUpdate,
usedBytes: 512n,
lastBlockNumber: 1,
lastBlockHash: sha256(Buffer.from("first-shine-block")),
lastBlockSignature: Buffer.alloc(64, 0),
arweaveTxId: "",
},
isServer: true,
serverKey: serverKey2,
serverAddress: "https://srv-2.local",
@@ -275,6 +332,14 @@ describe("shine_users e2e", () => {
accessServers: ["access_srv_2", "access_srv_3"],
trustedCount: 0,
};
const updateLastBlockHash = sha256(serializeLastBlockState(updateRecord));
const updateLastBlockEdIx = Ed25519Program.createInstructionWithPrivateKey({
privateKey: blockchain.secretKey,
message: updateLastBlockHash,
});
updateRecord.blockchain.lastBlockSignature = extractSigFromEdIx(
Buffer.from(updateLastBlockEdIx.data)
);
const updateUnsigned = serializeUnsignedRecord(updateRecord);
const updateHash = sha256(updateUnsigned);
@@ -294,9 +359,16 @@ describe("shine_users e2e", () => {
prevHash: sha256(createUnsigned),
additionalLimit: new anchor.BN(additionalLimitUpdate.toString()),
fields: {
blockchainKey: updateRecord.blockchainKey,
deviceKey: updateRecord.deviceKey,
chainNumber: 1,
blockchainPublicKey: updateRecord.blockchain.blockchainPublicKey,
blockchainName,
usedBytes: new anchor.BN(
updateRecord.blockchain.usedBytes.toString()
),
lastBlockNumber: updateRecord.blockchain.lastBlockNumber,
lastBlockHash: updateRecord.blockchain.lastBlockHash,
lastBlockSignature: updateRecord.blockchain.lastBlockSignature,
arweaveTxId: "",
isServer: true,
serverKey: serverKey2,
serverAddress: "https://srv-2.local",
@@ -309,14 +381,16 @@ describe("shine_users e2e", () => {
.accounts({
signer: provider.wallet.publicKey,
userPda,
systemProgram: SystemProgram.programId,
inflowVault: inflowVaultPda,
instructions: SYSVAR_INSTRUCTIONS_PUBKEY,
usersEconomyConfigPda,
})
.instruction();
await provider.sendAndConfirm(new Transaction().add(updateEdIx, updateIx), []);
await provider.sendAndConfirm(
new Transaction().add(updateLastBlockEdIx, updateEdIx, updateIx),
[]
);
const updatedAcc = await provider.connection.getAccountInfo(userPda);
expect(updatedAcc).not.eq(null);