minauth-erc721-timelock-plugin
Version:
This package contains an implementation of a MinAuth plugin that combines zero-knowledge Merkle "membership" proofs with the ability to register into to a Merkle tree with locking an Ethereum NFT.
58 lines • 2.12 kB
JavaScript
import { CircuitString, Field, Poseidon } from 'o1js';
import * as z from 'zod';
export const UserCommitmentHexSchema = z.object({
commitmentHex: z.string().regex(/^(0x)?[0-9a-fA-F]+$/, {
message: 'Invalid hexadecimal string'
})
});
export const UserSecretInputSchema = z.object({
secret: z.string().max(CircuitString.maxLength)
});
export const UserCommitmentFieldSchema = z.object({
commitment: z.instanceof(Field, {
message: 'Invalid commitment type. Should be o1js.Field.'
})
});
export const UserSecretSchema = z.object({
secretHash: z.instanceof(Field, {
message: 'Invalid secret hash type. Should be o1js.Field.'
})
});
export const SECRET_MAX_LENGTH = CircuitString.maxLength;
export const mkUserSecret = ({ secret }) => {
if (secret.length > CircuitString.maxLength) {
throw new Error(`Secret string length is ${secret.length} but max length is ${CircuitString.maxLength}`);
}
const secretCircuitString = CircuitString.fromString(secret);
const fields = secretCircuitString.values.map((c) => c.value);
return { secretHash: Poseidon.hash(fields) };
};
export const userCommitmentHex = ({ secretHash }) => {
const commitment = Poseidon.hash([secretHash]);
return { commitmentHex: hexField(commitment) };
};
export const userCommitmentField = ({ secretHash }) => {
const commitment = Poseidon.hash([secretHash]);
return { commitment };
};
export const commitmentHexToField = ({ commitmentHex }) => {
let commitment = commitmentHex;
// Ensure the commitment string starts with '0x'
if (!commitment.startsWith('0x')) {
commitment = '0x' + commitment;
}
const ret = new Field(BigInt(commitment));
return { commitment: ret };
};
export const commitmentFieldToHex = ({ commitment }) => {
return { commitmentHex: hexField(commitment) };
};
export const hexField = (f) => {
const decimalInt = BigInt(f.toString());
let hex = decimalInt.toString(16);
if (hex.length % 2) {
hex = '0' + hex;
}
return '0x' + hex;
};
//# sourceMappingURL=commitment-types.js.map