|
| 1 | +"use strict"; |
| 2 | +/** |
| 3 | + * TaskForest SDK — Privacy Helpers |
| 4 | + * Convenience functions for creating privacy-enhanced jobs. |
| 5 | + */ |
| 6 | +var __importDefault = (this && this.__importDefault) || function (mod) { |
| 7 | + return (mod && mod.__esModule) ? mod : { "default": mod }; |
| 8 | +}; |
| 9 | +Object.defineProperty(exports, "__esModule", { value: true }); |
| 10 | +exports.PRIVACY_PER = exports.PRIVACY_ENCRYPTED = exports.PRIVACY_PUBLIC = void 0; |
| 11 | +exports.sealBid = sealBid; |
| 12 | +exports.generateSalt = generateSalt; |
| 13 | +exports.generateEncryptionKeypair = generateEncryptionKeypair; |
| 14 | +exports.encryptTaskData = encryptTaskData; |
| 15 | +exports.decryptTaskData = decryptTaskData; |
| 16 | +exports.encryptCredential = encryptCredential; |
| 17 | +exports.hash32 = hash32; |
| 18 | +exports.hashString = hashString; |
| 19 | +exports.preparePrivateJob = preparePrivateJob; |
| 20 | +const tweetnacl_1 = __importDefault(require("tweetnacl")); |
| 21 | +// --- Sealed bids --- |
| 22 | +/** Create a sealed bid hash for commit-reveal scheme. */ |
| 23 | +function sealBid(amountLamports, salt) { |
| 24 | + const data = new Uint8Array(8 + salt.length); |
| 25 | + const view = new DataView(data.buffer); |
| 26 | + view.setBigUint64(0, BigInt(amountLamports), true); |
| 27 | + data.set(salt, 8); |
| 28 | + return tweetnacl_1.default.hash(data).slice(0, 32); |
| 29 | +} |
| 30 | +/** Generate a random 32-byte salt. */ |
| 31 | +function generateSalt() { |
| 32 | + return tweetnacl_1.default.randomBytes(32); |
| 33 | +} |
| 34 | +// --- Encryption --- |
| 35 | +/** Generate an X25519 keypair for encryption. */ |
| 36 | +function generateEncryptionKeypair() { |
| 37 | + return tweetnacl_1.default.box.keyPair(); |
| 38 | +} |
| 39 | +/** Encrypt task data with NaCl box. */ |
| 40 | +function encryptTaskData(plaintext, recipientPubkey, senderSecretKey) { |
| 41 | + const nonce = tweetnacl_1.default.randomBytes(tweetnacl_1.default.box.nonceLength); |
| 42 | + const msg = new TextEncoder().encode(plaintext); |
| 43 | + const ct = tweetnacl_1.default.box(msg, nonce, recipientPubkey, senderSecretKey); |
| 44 | + if (!ct) |
| 45 | + throw new Error('Encryption failed'); |
| 46 | + return { ciphertext: ct, nonce }; |
| 47 | +} |
| 48 | +/** Decrypt task data. */ |
| 49 | +function decryptTaskData(ciphertext, nonce, senderPubkey, recipientSecretKey) { |
| 50 | + const pt = tweetnacl_1.default.box.open(ciphertext, nonce, senderPubkey, recipientSecretKey); |
| 51 | + if (!pt) |
| 52 | + throw new Error('Decryption failed'); |
| 53 | + return new TextDecoder().decode(pt); |
| 54 | +} |
| 55 | +// --- Credential vault --- |
| 56 | +/** Encrypt an API key for the credential vault. */ |
| 57 | +function encryptCredential(credential, vaultPubkey, posterSecretKey) { |
| 58 | + return encryptTaskData(credential, vaultPubkey, posterSecretKey); |
| 59 | +} |
| 60 | +// --- Hash helpers --- |
| 61 | +/** SHA-512 → 32 bytes (for on-chain hashes). */ |
| 62 | +function hash32(data) { |
| 63 | + return Array.from(tweetnacl_1.default.hash(data).slice(0, 32)); |
| 64 | +} |
| 65 | +/** Hash a string to 32 bytes. */ |
| 66 | +function hashString(str) { |
| 67 | + return hash32(new TextEncoder().encode(str)); |
| 68 | +} |
| 69 | +// --- Privacy levels --- |
| 70 | +exports.PRIVACY_PUBLIC = 0; |
| 71 | +exports.PRIVACY_ENCRYPTED = 1; |
| 72 | +exports.PRIVACY_PER = 2; |
| 73 | +/** Prepare privacy parameters for job creation. */ |
| 74 | +function preparePrivateJob(config) { |
| 75 | + const encKp = config.encryptionKeypair || generateEncryptionKeypair(); |
| 76 | + let encryptedPayload = null; |
| 77 | + if (config.taskData && config.privacyLevel >= exports.PRIVACY_ENCRYPTED) { |
| 78 | + encryptedPayload = encryptTaskData(config.taskData, encKp.publicKey, encKp.secretKey); |
| 79 | + } |
| 80 | + return { |
| 81 | + privacyLevel: config.privacyLevel, |
| 82 | + encryptionPubkey: Array.from(encKp.publicKey), |
| 83 | + encryptedPayload, |
| 84 | + encryptionKeypair: encKp, |
| 85 | + }; |
| 86 | +} |
0 commit comments