import nacl from "tweetnacl";
import bs58 from "bs58";
function buildOrderMessage(msg) {
const buf = Buffer.alloc(92);
let offset = 0;
// market (32 bytes)
buf.set(bs58.decode(msg.market), offset);
offset += 32;
// owner (32 bytes)
buf.set(bs58.decode(msg.owner), offset);
offset += 32;
// side (1 byte)
buf.writeUInt8(msg.side, offset);
offset += 1;
// outcome (1 byte)
buf.writeUInt8(msg.outcome, offset);
offset += 1;
// priceBps (2 bytes, little-endian)
buf.writeUInt16LE(msg.priceBps, offset);
offset += 2;
// quantity (8 bytes, little-endian)
buf.writeBigUInt64LE(BigInt(msg.quantity), offset);
offset += 8;
// nonce (8 bytes, little-endian)
buf.writeBigUInt64LE(BigInt(msg.nonce), offset);
offset += 8;
// expiry (8 bytes, little-endian, signed)
buf.writeBigInt64LE(BigInt(msg.expiry), offset);
return buf;
}
function signOrderMessage(msg, keypair) {
const orderBytes = buildOrderMessage(msg);
const textPayload = "PMX Order:\n" + orderBytes.toString("hex");
const messageBytes = Buffer.from(textPayload, "utf-8");
const signature = nacl.sign.detached(messageBytes, keypair.secretKey);
return Buffer.from(signature).toString("base64");
}