/// <summary> /// Adds the given number to the given index in the program. Automatically /// uses shortest encoding possible. /// </summary> public ScriptBuilder Number(int index, long num) { if (num >= 0 && num < 16) { return(this.AddChunk(index, new ScriptChunk(Script.EncodeToOpN((int)num), null))); } return(this.BigNum(index, num)); }
/// <summary> /// Adds a copy of the given byte array as a data element (i.e. PUSHDATA) at the given index in the program. /// </summary> public ScriptBuilder Data(int index, byte[] data) { // implements BIP62 byte[] copy = data.Clone() as byte[]; int opcode; if (data.Length == 0) { opcode = ScriptOpCodes.OP_0; } else if (data.Length == 1) { byte b = data[0]; if (b >= 1 && b <= 16) { opcode = Script.EncodeToOpN(b); } else { opcode = 1; } } else if (data.Length < ScriptOpCodes.OP_PUSHDATA1) { opcode = data.Length; } else if (data.Length < 256) { opcode = ScriptOpCodes.OP_PUSHDATA1; } else if (data.Length < 65536) { opcode = ScriptOpCodes.OP_PUSHDATA2; } else { throw new ScriptException("Unimplemented"); } return(this.AddChunk(index, new ScriptChunk(opcode, copy))); }
/// <summary> /// Adds the given number as a OP_N opcode to the given index in the program. /// Only handles values 0-16 inclusive. /// </summary> public ScriptBuilder SmallNum(int index, int num) { Guard.Require(num >= 0, "Cannot encode negative numbers with SmallNum"); Guard.Require(num <= 16, "Cannot encode numbers larger than 16 with SmallNum"); return(this.AddChunk(index, new ScriptChunk(Script.EncodeToOpN(num), null))); }