@Test public void testParseSignedMessage_Malformed_Missing_Blank_Line() throws Exception { String signatureBlock = FixtureAsserts.fixture("/fixtures/messages/test-malformed-1.txt"); BitcoinMessages.SignedMessage actual = BitcoinMessages.parseSignedMessage(Optional.of(signatureBlock)); assertThat(actual.getMessage()).isEqualTo("Hello World"); assertThat(actual.getAddress()).isEqualTo("16R2kAxaUNM4xj6ykKbxEugpJdYyJzTP13"); assertThat(actual.getSignature()).isEqualTo("H0b22gIQIfutUzm7Z9qchdfhUtaO52alhNPK3emrkGOfbOzGHVPuWD9rMIphxniwBNgF/YN4c5C/dMwXz3yJz5k="); assertThat(actual.getVersion()).startsWith("MultiBit HD"); assertThat(actual.getComment()).isEqualTo("https: }
public static SignedMessage parseSignedMessage(Optional<String> signatureBlock) { if (!signatureBlock.isPresent()) { return new SignedMessage("", "", "", "", ""); } String block = signatureBlock.get(); if ((block.contains(BEGIN_SIGNED_MESSAGE) || block.contains(BEGIN_SIGNATURE)) && block.contains(END_BITCOIN_SIGNATURE)) { String ls; if (block.contains("\r\n")) { ls = "\r\n"; } else { ls = "\n"; } int lsLength = ls.length(); List<String> lines = Splitter.on(ls).splitToList(block); String message = ""; String version = ""; String comment = ""; String address = ""; String signature = ""; int state = 0; boolean appendLs = false; for (String line : lines) { if (line.contains(BEGIN_SIGNED_MESSAGE)) { state = 1; continue; } if (line.contains(BEGIN_SIGNATURE)) { state = 2; if (message.length() > 0 && !appendLs) { message = message.substring(0, message.length() - lsLength); } continue; } if (line.contains(END_BITCOIN_SIGNATURE)) { break; } switch (state) { case 0: break; case 1: if (appendLs) { message += ls; } if (Strings.isNullOrEmpty(line)) { message += ls; appendLs = false; } else { message += line; appendLs = true; } break; case 2: if (line.startsWith(VERSION)) { version = line.length() > VERSION.length() + 1 ? line.substring((VERSION.length() + 1)).trim() : ""; continue; } if (line.startsWith(COMMENT)) { comment = line.length() > COMMENT.length() + 1 ? line.substring((COMMENT.length() + 1)).trim() : ""; continue; } if (line.startsWith(ADDRESS)) { address = line.length() > ADDRESS.length() + 1 ? line.substring((ADDRESS.length() + 1)).trim() : ""; continue; } if (Strings.isNullOrEmpty(line.trim())) { state = 3; continue; } if (!line.contains(":")) { state = 3; signature += line; continue; } break; case 3: signature += line; break; default: throw new IllegalStateException("Unknown state: " + state); } } return new SignedMessage( message, address, signature, version, comment ); } return new SignedMessage(block, "", "", "", ""); }
BitcoinMessages { public static SignedMessage parseSignedMessage(Optional<String> signatureBlock) { if (!signatureBlock.isPresent()) { return new SignedMessage("", "", "", "", ""); } String block = signatureBlock.get(); if ((block.contains(BEGIN_SIGNED_MESSAGE) || block.contains(BEGIN_SIGNATURE)) && block.contains(END_BITCOIN_SIGNATURE)) { String ls; if (block.contains("\r\n")) { ls = "\r\n"; } else { ls = "\n"; } int lsLength = ls.length(); List<String> lines = Splitter.on(ls).splitToList(block); String message = ""; String version = ""; String comment = ""; String address = ""; String signature = ""; int state = 0; boolean appendLs = false; for (String line : lines) { if (line.contains(BEGIN_SIGNED_MESSAGE)) { state = 1; continue; } if (line.contains(BEGIN_SIGNATURE)) { state = 2; if (message.length() > 0 && !appendLs) { message = message.substring(0, message.length() - lsLength); } continue; } if (line.contains(END_BITCOIN_SIGNATURE)) { break; } switch (state) { case 0: break; case 1: if (appendLs) { message += ls; } if (Strings.isNullOrEmpty(line)) { message += ls; appendLs = false; } else { message += line; appendLs = true; } break; case 2: if (line.startsWith(VERSION)) { version = line.length() > VERSION.length() + 1 ? line.substring((VERSION.length() + 1)).trim() : ""; continue; } if (line.startsWith(COMMENT)) { comment = line.length() > COMMENT.length() + 1 ? line.substring((COMMENT.length() + 1)).trim() : ""; continue; } if (line.startsWith(ADDRESS)) { address = line.length() > ADDRESS.length() + 1 ? line.substring((ADDRESS.length() + 1)).trim() : ""; continue; } if (Strings.isNullOrEmpty(line.trim())) { state = 3; continue; } if (!line.contains(":")) { state = 3; signature += line; continue; } break; case 3: signature += line; break; default: throw new IllegalStateException("Unknown state: " + state); } } return new SignedMessage( message, address, signature, version, comment ); } return new SignedMessage(block, "", "", "", ""); } }
BitcoinMessages { public static SignedMessage parseSignedMessage(Optional<String> signatureBlock) { if (!signatureBlock.isPresent()) { return new SignedMessage("", "", "", "", ""); } String block = signatureBlock.get(); if ((block.contains(BEGIN_SIGNED_MESSAGE) || block.contains(BEGIN_SIGNATURE)) && block.contains(END_BITCOIN_SIGNATURE)) { String ls; if (block.contains("\r\n")) { ls = "\r\n"; } else { ls = "\n"; } int lsLength = ls.length(); List<String> lines = Splitter.on(ls).splitToList(block); String message = ""; String version = ""; String comment = ""; String address = ""; String signature = ""; int state = 0; boolean appendLs = false; for (String line : lines) { if (line.contains(BEGIN_SIGNED_MESSAGE)) { state = 1; continue; } if (line.contains(BEGIN_SIGNATURE)) { state = 2; if (message.length() > 0 && !appendLs) { message = message.substring(0, message.length() - lsLength); } continue; } if (line.contains(END_BITCOIN_SIGNATURE)) { break; } switch (state) { case 0: break; case 1: if (appendLs) { message += ls; } if (Strings.isNullOrEmpty(line)) { message += ls; appendLs = false; } else { message += line; appendLs = true; } break; case 2: if (line.startsWith(VERSION)) { version = line.length() > VERSION.length() + 1 ? line.substring((VERSION.length() + 1)).trim() : ""; continue; } if (line.startsWith(COMMENT)) { comment = line.length() > COMMENT.length() + 1 ? line.substring((COMMENT.length() + 1)).trim() : ""; continue; } if (line.startsWith(ADDRESS)) { address = line.length() > ADDRESS.length() + 1 ? line.substring((ADDRESS.length() + 1)).trim() : ""; continue; } if (Strings.isNullOrEmpty(line.trim())) { state = 3; continue; } if (!line.contains(":")) { state = 3; signature += line; continue; } break; case 3: signature += line; break; default: throw new IllegalStateException("Unknown state: " + state); } } return new SignedMessage( message, address, signature, version, comment ); } return new SignedMessage(block, "", "", "", ""); }  }
BitcoinMessages { public static SignedMessage parseSignedMessage(Optional<String> signatureBlock) { if (!signatureBlock.isPresent()) { return new SignedMessage("", "", "", "", ""); } String block = signatureBlock.get(); if ((block.contains(BEGIN_SIGNED_MESSAGE) || block.contains(BEGIN_SIGNATURE)) && block.contains(END_BITCOIN_SIGNATURE)) { String ls; if (block.contains("\r\n")) { ls = "\r\n"; } else { ls = "\n"; } int lsLength = ls.length(); List<String> lines = Splitter.on(ls).splitToList(block); String message = ""; String version = ""; String comment = ""; String address = ""; String signature = ""; int state = 0; boolean appendLs = false; for (String line : lines) { if (line.contains(BEGIN_SIGNED_MESSAGE)) { state = 1; continue; } if (line.contains(BEGIN_SIGNATURE)) { state = 2; if (message.length() > 0 && !appendLs) { message = message.substring(0, message.length() - lsLength); } continue; } if (line.contains(END_BITCOIN_SIGNATURE)) { break; } switch (state) { case 0: break; case 1: if (appendLs) { message += ls; } if (Strings.isNullOrEmpty(line)) { message += ls; appendLs = false; } else { message += line; appendLs = true; } break; case 2: if (line.startsWith(VERSION)) { version = line.length() > VERSION.length() + 1 ? line.substring((VERSION.length() + 1)).trim() : ""; continue; } if (line.startsWith(COMMENT)) { comment = line.length() > COMMENT.length() + 1 ? line.substring((COMMENT.length() + 1)).trim() : ""; continue; } if (line.startsWith(ADDRESS)) { address = line.length() > ADDRESS.length() + 1 ? line.substring((ADDRESS.length() + 1)).trim() : ""; continue; } if (Strings.isNullOrEmpty(line.trim())) { state = 3; continue; } if (!line.contains(":")) { state = 3; signature += line; continue; } break; case 3: signature += line; break; default: throw new IllegalStateException("Unknown state: " + state); } } return new SignedMessage( message, address, signature, version, comment ); } return new SignedMessage(block, "", "", "", ""); }  static String formatAsBitcoinSignedMessage(
    String addressText,
    String message,
    String signature
  ); static SignedMessage parseSignedMessage(Optional<String> signatureBlock); }
BitcoinMessages { public static SignedMessage parseSignedMessage(Optional<String> signatureBlock) { if (!signatureBlock.isPresent()) { return new SignedMessage("", "", "", "", ""); } String block = signatureBlock.get(); if ((block.contains(BEGIN_SIGNED_MESSAGE) || block.contains(BEGIN_SIGNATURE)) && block.contains(END_BITCOIN_SIGNATURE)) { String ls; if (block.contains("\r\n")) { ls = "\r\n"; } else { ls = "\n"; } int lsLength = ls.length(); List<String> lines = Splitter.on(ls).splitToList(block); String message = ""; String version = ""; String comment = ""; String address = ""; String signature = ""; int state = 0; boolean appendLs = false; for (String line : lines) { if (line.contains(BEGIN_SIGNED_MESSAGE)) { state = 1; continue; } if (line.contains(BEGIN_SIGNATURE)) { state = 2; if (message.length() > 0 && !appendLs) { message = message.substring(0, message.length() - lsLength); } continue; } if (line.contains(END_BITCOIN_SIGNATURE)) { break; } switch (state) { case 0: break; case 1: if (appendLs) { message += ls; } if (Strings.isNullOrEmpty(line)) { message += ls; appendLs = false; } else { message += line; appendLs = true; } break; case 2: if (line.startsWith(VERSION)) { version = line.length() > VERSION.length() + 1 ? line.substring((VERSION.length() + 1)).trim() : ""; continue; } if (line.startsWith(COMMENT)) { comment = line.length() > COMMENT.length() + 1 ? line.substring((COMMENT.length() + 1)).trim() : ""; continue; } if (line.startsWith(ADDRESS)) { address = line.length() > ADDRESS.length() + 1 ? line.substring((ADDRESS.length() + 1)).trim() : ""; continue; } if (Strings.isNullOrEmpty(line.trim())) { state = 3; continue; } if (!line.contains(":")) { state = 3; signature += line; continue; } break; case 3: signature += line; break; default: throw new IllegalStateException("Unknown state: " + state); } } return new SignedMessage( message, address, signature, version, comment ); } return new SignedMessage(block, "", "", "", ""); }  static String formatAsBitcoinSignedMessage(
    String addressText,
    String message,
    String signature
  ); static SignedMessage parseSignedMessage(Optional<String> signatureBlock);  }