/* Create p2sh for this redeem script. */ u8 *scriptpubkey_p2sh(const tal_t *ctx, const u8 *redeemscript) { struct sha256 h; struct ripemd160 redeemhash; u8 *script = tal_arr(ctx, u8, 0); add_op(&script, OP_HASH160); sha256(&h, redeemscript, tal_count(redeemscript)); ripemd160(&redeemhash, h.u.u8, sizeof(h)); add_push_bytes(&script, redeemhash.u.u8, sizeof(redeemhash.u.u8)); add_op(&script, OP_EQUAL); return script; }
/* Create p2sh for this redeem script. */ u8 *scriptpubkey_p2sh(const tal_t *ctx, const u8 *redeemscript) { struct sha256 h; u8 redeemhash[RIPEMD160_DIGEST_LENGTH]; u8 *script = tal_arr(ctx, u8, 0); add_op(&script, OP_HASH160); sha256(&h, redeemscript, tal_count(redeemscript)); RIPEMD160(h.u.u8, sizeof(h), redeemhash); add_push_bytes(&script, redeemhash, sizeof(redeemhash)); add_op(&script, OP_EQUAL); return script; }
static void add_number(u8 **script, u32 num) { if (num == 0) add_op(script, 0); else if (num <= 16) add_op(script, 0x50 + num); else { u8 n = num; /* We could handle others, but currently unnecessary. */ assert(num < 256); add_push_bytes(script, &n, sizeof(n)); } }
/* A common script pattern: A can have it with secret, or B can have * it after delay. */ u8 *bitcoin_redeem_secret_or_delay(const tal_t *ctx, const struct pubkey *delayed_key, u32 locktime, const struct pubkey *key_if_secret_known, const struct sha256 *hash_of_secret) { struct ripemd160 ripemd; u8 *script = tal_arr(ctx, u8, 0); ripemd160(&ripemd, hash_of_secret->u.u8, sizeof(hash_of_secret->u)); /* If the secret is supplied.... */ add_op(&script, OP_HASH160); add_push_bytes(&script, ripemd.u.u8, sizeof(ripemd.u.u8)); add_op(&script, OP_EQUAL); add_op(&script, OP_IF); /* They can collect the funds. */ add_push_key(&script, key_if_secret_known); add_op(&script, OP_ELSE); /* Other can collect after a delay. */ add_push_le32(&script, locktime); add_op(&script, OP_CHECKSEQUENCEVERIFY); add_op(&script, OP_DROP); add_push_key(&script, delayed_key); add_op(&script, OP_ENDIF); add_op(&script, OP_CHECKSIG); return script; }
int bot_parse_service(struct IRC *bot, char *server, char *command, char *me, char *channel, char *msg){ if(DEBUG){ printf("[server: %s] [command: %s] [me: %s] [channel: %s] %s\n",server,command,me,channel,msg); } // 353 is the NAMES list if(strcasecmp(command, "353") == 0){ parse_op(bot,msg); if(DEBUG){ print_op(bot); } } if(strcasecmp(command, "MODE") ==0){ if((msg[0]=='+') && (msg[1]=='o')){ // if it's not already in the oplist if(is_op(bot,&msg[3])==-1){ add_op(bot,&msg[3]); if(DEBUG){ print_op(bot); } } } else if((msg[0]=='-') && (msg[1]=='o')){ if(is_op(bot,&msg[3])!=-1){ rm_op(bot,&msg[3]); if(DEBUG){ print_op(bot); } } } } return 0; }
/*Figure 2.11 A Parsing Procedure Including Semanntic Processing*/ void expression(expr_rec *result) { expr_rec left_operand, right_operand, constant_folding; op_rec op; primary(& left_operand); while(next_token() == PLUSOP || next_token() == MINUSOP) { int valor; add_op(& op); primary(& right_operand); if (left_operand.kind==LITERALEXPR && right_operand.kind==LITERALEXPR){ if(op.operator==MINUS){ valor = left_operand.val-right_operand.val; } else{ valor = left_operand.val+right_operand.val; } left_operand.val = valor; } left_operand = gen_infix(left_operand, op, right_operand); } *result= left_operand; }
/* tal_count() gives the length of the script. */ u8 *bitcoin_redeem_single(const tal_t *ctx, const struct pubkey *key) { u8 *script = tal_arr(ctx, u8, 0); add_push_key(&script, key); add_op(&script, OP_CHECKSIG); return script; }
static void add_unary_op (toyvm_function *fn, enum opcode opcode, const char *rest_of_line, int linenum) { int operand = atoi (rest_of_line); add_op (fn, opcode, operand, linenum); }
static void add_push_bytes(u8 **scriptp, const void *mem, size_t len) { if (len < 76) add_op(scriptp, OP_PUSHBYTES(len)); else if (len < 256) { char c = len; add_op(scriptp, OP_PUSHDATA1); add(scriptp, &c, 1); } else if (len < 65536) { le16 v = cpu_to_le16(len); add_op(scriptp, OP_PUSHDATA2); add(scriptp, &v, 2); } else { le32 v = cpu_to_le32(len); add_op(scriptp, OP_PUSHDATA4); add(scriptp, &v, 4); } add(scriptp, mem, len); }
static binary_op(stream, node, need_lval) { auto op = node[0], sz = type_size( node[2] ); expr_code( stream, node[3], is_assop(op) ); asm_push( stream ); expr_code( stream, node[4], 0 ); if ( op == '[]' ) subscript(stream, node, need_lval); else if ( op == '*' ) pop_mult(stream, 0, node[2][5]); else if ( op == '/' ) pop_div(stream, 0, node[2][5]); else if ( op == '%' ) pop_mod(stream, 0, node[2][5]); else if ( op == '+' ) add_op(stream, node, 0, sz); else if ( op == '-' ) add_op(stream, node, 0, sz); else if ( op == '<<' ) pop_lshift(stream, 0); else if ( op == '>>' ) pop_rshift(stream, 0); else if ( op == '&' ) pop_bitand(stream, 0, sz); else if ( op == '|' ) pop_bitor(stream, 0, sz); else if ( op == '^' ) pop_bitxor(stream, 0, sz); else if ( op == '=' ) pop_assign(stream, sz); else if ( op == '*=' ) pop_mult(stream, 1); else if ( op == '/=' ) pop_div(stream, 1); else if ( op == '%=' ) pop_mod(stream, 1); else if ( op == '+=' ) add_op(stream, node, 1, sz); else if ( op == '-=' ) add_op(stream, node, 1, sz); else if ( op == '<<=' ) pop_lshift(stream, 1); else if ( op == '>>=' ) pop_rshift(stream, 1); else if ( op == '&=' ) pop_bitand(stream, 1, sz); else if ( op == '|=' ) pop_bitor(stream, 1, sz); else if ( op == '^=' ) pop_bitxor(stream, 1, sz); else int_error("Unknown operator: '%Mc'", op); /* The assignment operators are implemented to return lvalues, but * the C standard says they're not lvalues. (The C++ standard has * them as lvalues.) */ if ( is_assop(op) ) dereference(stream, type_size(node[2]), 0); }
/** * Function representing the <exp_prime> productions */ attr RecursiveDescentParser::exp_prime(attr lhs) { OpCode opCode; attr retVal, termAttr, rhs; retVal.type = T_ERROR; if(errorCondition) return retVal; if(token == TK_PLUS || token == TK_MINUS) { #if DEBUG_PARSER std::cout << "<exp_prime> --> <add_op><term><exp_prime>\n"; #endif Token sToken = token; opCode = add_op(); termAttr = term(); rhs = exp_prime( termAttr ); retVal.type = lhs.type; retVal.addr = symTab.getNextAddress(); // create a temp iCode.threeAddressCode(retVal, opCode, lhs, rhs); if(lhs.type != rhs.type) { errorHandler("Type mismatch (exp_prime)", sToken); } } else if(token == TK_RIGHT_PARENTHESES || token == TK_RIGHT_BRACKET || token == TK_SEMICOLON || token == TK_LT || token == TK_GT || token == TK_EQUAL || token == TK_LTE || token == TK_GTE || token == TK_NOT_EQ) { #if DEBUG_PARSER std::cout << "<exp_prime> --> e\n"; #endif retVal = lhs; //retVal.param = false; } else { errorHandler(); } return retVal; }
/* One of: * mysig and relative locktime passed, OR * theirsig and hash preimage. */ u8 *bitcoin_redeem_revocable(const tal_t *ctx, const struct pubkey *mykey, u32 locktime, const struct pubkey *theirkey, const struct sha256 *rhash) { u8 *script = tal_arr(ctx, u8, 0); u8 rhash_ripemd[RIPEMD160_DIGEST_LENGTH]; le32 locktime_le = cpu_to_le32(locktime); /* If there are two args: */ add_op(&script, OP_DEPTH); add_op(&script, OP_1SUB); add_op(&script, OP_IF); /* Must hash to revocation_hash, and be signed by them. */ RIPEMD160(rhash->u.u8, sizeof(rhash->u), rhash_ripemd); add_op(&script, OP_HASH160); add_push_bytes(&script, rhash_ripemd, sizeof(rhash_ripemd)); add_op(&script, OP_EQUALVERIFY); add_push_key(&script, theirkey); /* Not two args? Must be us using timeout. */ add_op(&script, OP_ELSE); add_push_bytes(&script, &locktime_le, sizeof(locktime_le)); add_op(&script, OP_CHECKSEQUENCEVERIFY); add_op(&script, OP_DROP); add_push_key(&script, mykey); add_op(&script, OP_ENDIF); /* And check it (ither path) */ add_op(&script, OP_CHECKSIG); return script; }
/*Figure 2.11 A Parsing Procedure Including Semanntic Processing*/ void expression(expr_rec *result) { expr_rec left_operand, right_operand; op_rec op; int valor=0; primary(& left_operand); while(next_token() == PLUSOP || next_token() == MINUSOP) { add_op(& op); primary(& right_operand); left_operand = gen_infix(left_operand, op, right_operand); } *result= left_operand; }
struct syntax_node * other_term(){ if(strcmp(current_token->value, "+") !=0&& strcmp(current_token->value, "-") !=0) { return NULL; } struct syntax_node * t; t = (struct syntax_node*)malloc(sizeof(struct syntax_node)); chushihua_t(t); t->child[0] = add_op(); t->child[1] = Exp(); strcpy(t->kind_name, "OtherTerm"); return t; }
/* tal_count() gives the length of the script. */ u8 *bitcoin_redeem_2of2(const tal_t *ctx, const struct pubkey *key1, const struct pubkey *key2) { u8 *script = tal_arr(ctx, u8, 0); add_number(&script, 2); if (key_less(key1, key2)) { add_push_key(&script, key1); add_push_key(&script, key2); } else { add_push_key(&script, key2); add_push_key(&script, key1); } add_number(&script, 2); add_op(&script, OP_CHECKMULTISIG); return script; }
static toyvm_function * toyvm_function_parse (const char *filename, const char *name) { FILE *f = NULL; toyvm_function *fn = NULL; char *line = NULL; ssize_t linelen; size_t bufsize; int linenum = 0; assert (filename); assert (name); f = fopen (filename, "r"); if (!f) { fprintf (stderr, "cannot open file %s: %s\n", filename, strerror (errno)); goto error; } fn = (toyvm_function *)calloc (1, sizeof (toyvm_function)); if (!fn) { fprintf (stderr, "out of memory allocating toyvm_function\n"); goto error; } fn->fn_filename = filename; /* Read the lines of the file. */ while ((linelen = getline (&line, &bufsize, f)) != -1) { /* Note that this is a terrible parser, but it avoids the need to bring in lex/yacc as a dependency. */ linenum++; if (0) fprintf (stdout, "%3d: %s", linenum, line); /* Lines beginning with # are comments. */ if (line[0] == '#') continue; /* Skip blank lines. */ if (line[0] == '\n') continue; #define LINE_MATCHES(OPCODE) (0 == strncmp ((OPCODE), line, strlen (OPCODE))) if (LINE_MATCHES ("DUP\n")) add_op (fn, DUP, 0, linenum); else if (LINE_MATCHES ("ROT\n")) add_op (fn, ROT, 0, linenum); else if (LINE_MATCHES ("BINARY_ADD\n")) add_op (fn, BINARY_ADD, 0, linenum); else if (LINE_MATCHES ("BINARY_SUBTRACT\n")) add_op (fn, BINARY_SUBTRACT, 0, linenum); else if (LINE_MATCHES ("BINARY_MULT\n")) add_op (fn, BINARY_MULT, 0, linenum); else if (LINE_MATCHES ("BINARY_COMPARE_LT\n")) add_op (fn, BINARY_COMPARE_LT, 0, linenum); else if (LINE_MATCHES ("RECURSE\n")) add_op (fn, RECURSE, 0, linenum); else if (LINE_MATCHES ("RETURN\n")) add_op (fn, RETURN, 0, linenum); else if (LINE_MATCHES ("PUSH_CONST ")) add_unary_op (fn, PUSH_CONST, line + strlen ("PUSH_CONST "), linenum); else if (LINE_MATCHES ("JUMP_ABS_IF_TRUE ")) add_unary_op (fn, JUMP_ABS_IF_TRUE, line + strlen("JUMP_ABS_IF_TRUE "), linenum); else { fprintf (stderr, "%s:%d: parse error\n", filename, linenum); free (fn); fn = NULL; goto error; } #undef LINE_MATCHES } free (line); fclose (f); return fn; error: free (line); if (f) fclose (f); free (fn); return NULL; }
/* Create a script for our HTLC output: receiving. */ u8 *scriptpubkey_htlc_recv(const tal_t *ctx, const struct pubkey *ourkey, const struct pubkey *theirkey, uint32_t htlc_abstimeout, uint32_t locktime, const struct sha256 *commit_revoke, const struct sha256 *rhash) { /* R value presented: -> us. * Commit revocation value presented: -> them. * HTLC times out -> them. */ u8 *script = tal_arr(ctx, u8, 0); struct ripemd160 ripemd; add_op(&script, OP_HASH160); add_op(&script, OP_DUP); /* Did we supply HTLC R value? */ ripemd160(&ripemd, rhash->u.u8, sizeof(rhash->u)); add_push_bytes(&script, &ripemd, sizeof(ripemd)); add_op(&script, OP_EQUAL); add_op(&script, OP_IF); add_push_le32(&script, locktime); add_op(&script, OP_CHECKSEQUENCEVERIFY); /* Drop extra hash as well as locktime. */ add_op(&script, OP_2DROP); add_push_key(&script, ourkey); add_op(&script, OP_ELSE); /* If they provided commit revocation, available immediately. */ ripemd160(&ripemd, commit_revoke->u.u8, sizeof(commit_revoke->u)); add_push_bytes(&script, &ripemd, sizeof(ripemd)); add_op(&script, OP_EQUAL); add_op(&script, OP_NOTIF); /* Otherwise, they must wait for HTLC timeout. */ add_push_le32(&script, htlc_abstimeout); add_op(&script, OP_CHECKLOCKTIMEVERIFY); add_op(&script, OP_DROP); add_op(&script, OP_ENDIF); add_push_key(&script, theirkey); add_op(&script, OP_ENDIF); add_op(&script, OP_CHECKSIG); return script; }
/* Create a script for our HTLC output: sending. */ u8 *scriptpubkey_htlc_send(const tal_t *ctx, const struct pubkey *ourkey, const struct pubkey *theirkey, uint32_t htlc_abstimeout, uint32_t locktime, const struct sha256 *commit_revoke, const struct sha256 *rhash) { /* R value presented: -> them. * Commit revocation value presented: -> them. * HTLC times out -> us. */ u8 *script = tal_arr(ctx, u8, 0); struct ripemd160 ripemd; add_op(&script, OP_HASH160); add_op(&script, OP_DUP); /* Did they supply HTLC R value? */ ripemd160(&ripemd, rhash->u.u8, sizeof(rhash->u)); add_push_bytes(&script, &ripemd, sizeof(ripemd)); add_op(&script, OP_EQUAL); add_op(&script, OP_SWAP); /* How about commit revocation value? */ ripemd160(&ripemd, commit_revoke->u.u8, sizeof(commit_revoke->u)); add_push_bytes(&script, &ripemd, sizeof(ripemd)); add_op(&script, OP_EQUAL); add_op(&script, OP_ADD); /* If either matched... */ add_op(&script, OP_IF); add_push_key(&script, theirkey); add_op(&script, OP_ELSE); /* If HTLC times out, they can collect after a delay. */ add_push_le32(&script, htlc_abstimeout); add_op(&script, OP_CHECKLOCKTIMEVERIFY); add_push_le32(&script, locktime); add_op(&script, OP_CHECKSEQUENCEVERIFY); add_op(&script, OP_2DROP); add_push_key(&script, ourkey); add_op(&script, OP_ENDIF); add_op(&script, OP_CHECKSIG); return script; }