MCPcopy Create free account
hub / github.com/NoteProtocol/NoteWallet / buildNoteScriptV2

Function buildNoteScriptV2

src/note.ts:37–72  ·  view source on GitHub ↗
(
  xOnlyPubkey: Buffer,
  hasPayload: boolean = true,
  msgpackEncodedData?: Buffer
)

Source from the content-addressed store, hash-verified

35
36// Construct locking script with embedded data placed after the NOTE identifier, split into 520-byte segments, data is msgpack encoded
37export function buildNoteScriptV2(
38 xOnlyPubkey: Buffer,
39 hasPayload: boolean = true,
40 msgpackEncodedData?: Buffer
41): string {
42 // Complete data format
43 // data0,data1,data2,data3,data4 NOTE OP_FALSE OP_IF data<520bytes> ... OP_ENDIF OP_2DROP OP_2DROP OP_2DROP xOnlyPubkey OP_CHECKSIG
44
45 // Protocol tag 4e4f5445 -> NOTE
46 let scriptASM = `${Buffer.from(NOTE_PROTOCOL_ENVELOPE_ID, "utf8").toString(
47 "hex"
48 )}`;
49 // If there's a data section, split it into 520-byte segments
50 if (msgpackEncodedData) {
51 scriptASM += ` OP_FALSE OP_IF`;
52 // Split data into 520-byte segments, up to 100K in total
53 const dataList = splitBufferIntoSegments(
54 msgpackEncodedData,
55 MAX_SCRIPT_ELEMENT_SIZE,
56 MAX_DATA_SEGMENTS_LIMIT
57 );
58 for (let i = 0; i < dataList.length; i++) {
59 scriptASM += ` ${dataList[i]!.toString("hex")}`;
60 }
61 scriptASM += ` OP_ENDIF`;
62 }
63 // If there's a payload, add 2DROP to remove the data section of the unlock script
64 if (hasPayload) {
65 scriptASM += ` OP_2DROP OP_2DROP OP_2DROP`;
66 } else {
67 // If there's no payload, remove the NOTE identifier
68 scriptASM += ` OP_DROP`;
69 }
70 scriptASM += ` ${xOnlyPubkey.toString("hex")} OP_CHECKSIG`;
71 return scriptASM;
72}
73
74// Test code: Construct unlock script with embedded data placed after the NOTE identifier, split into 520-byte segments, data is msgpack encoded
75export function buildNoteUnlockScriptV2(

Callers 3

buildNoteScriptFunction · 0.85
buildDataScriptFunction · 0.85
buildCommitNoteScriptFunction · 0.85

Calls 1

splitBufferIntoSegmentsFunction · 0.90

Tested by

no test coverage detected