(s string, opMap map[string]byte)
| 337 | } |
| 338 | |
| 339 | func parseScriptFrom(s string, opMap map[string]byte) ([]byte, error) { |
| 340 | var res []byte |
| 341 | words := strings.Split(s, " ") |
| 342 | |
| 343 | for i, w := range words { |
| 344 | if w == "" { |
| 345 | continue |
| 346 | } |
| 347 | |
| 348 | w = strings.TrimPrefix(w, "OP_") |
| 349 | if opcode, ok := opMap[w]; ok { |
| 350 | res = append(res, opcode) |
| 351 | } else if isAllDigitalNumber(w) || strings.HasPrefix(w, "-") && isAllDigitalNumber(w[1:]) { |
| 352 | n, _ := strconv.ParseInt(w, 10, 64) |
| 353 | if n == -1 || (n >= 1 && n <= 16) { |
| 354 | res = append(res, byte(n+(opcodes.OP_1-1))) |
| 355 | } else if n == 0 { |
| 356 | res = append(res, opcodes.OP_0) |
| 357 | } else { |
| 358 | res = appendData(res, ScriptNumSerialize(n)) |
| 359 | } |
| 360 | } else if strings.HasPrefix(w, "0x") || strings.HasPrefix(w, "0X") { |
| 361 | bs, err := hex.DecodeString(w[2:]) |
| 362 | if err != nil { |
| 363 | return nil, err |
| 364 | } |
| 365 | |
| 366 | res = append(res, bs...) |
| 367 | } else if len(w) >= 2 && w[0] == '\'' && w[len(w)-1] == '\'' { |
| 368 | w = w[1 : len(w)-1] |
| 369 | res = appendData(res, []byte(w)) |
| 370 | } else { |
| 371 | return nil, fmt.Errorf("parse script error when parse %dth with word(%s)", i, w) |
| 372 | } |
| 373 | } |
| 374 | |
| 375 | return res, nil |
| 376 | } |
| 377 | |
| 378 | func parseScriptFlag(s string) (uint32, error) { |
| 379 | var res uint32 |
no test coverage detected