NamecoinUtil { public static String getNameOperation(byte[] scriptPubKey) { if (scriptPubKey==null) { return NamecoinUtil.STR_OP_UNKNOWN; } if (scriptPubKey.length>1) { byte nameOp=scriptPubKey[0]; switch(nameOp) { case NamecoinUtil.OP_NAME_NEW: return NamecoinUtil.STR_OP_NAME_NEW; case NamecoinUtil.OP_NAME_FIRSTUPDATE: return NamecoinUtil.STR_OP_NAME_FIRSTUPDATE; case NamecoinUtil.OP_NAME_UDPATE: return NamecoinUtil.STR_OP_NAME_UDPATE; default: return NamecoinUtil.STR_OP_UNKNOWN; } } return NamecoinUtil.STR_OP_UNKNOWN; }  static String[] extractNamecoinField(byte[] scriptPubKey); static String getNameOperation(byte[] scriptPubKey); final static byte OP_NAME_NEW; final static byte OP_NAME_FIRSTUPDATE; final static byte OP_NAME_UDPATE; final static String STR_OP_UNKNOWN; final static String STR_OP_NAME_NEW; final static String STR_OP_NAME_FIRSTUPDATE; final static String STR_OP_NAME_UDPATE; }
@Test public void getNameOperationPositive() { String newScript = "511459C39A7CC5E0B91801294A272AD558B1F67A4E6D6D76A914DD900A6C1223698FC262E28C8A1D8D73B40B375188AC"; String resultOpNew=NamecoinUtil.getNameOperation(BitcoinUtil.convertHexStringToByteArray(newScript)); assertEquals(NamecoinUtil.STR_OP_NAME_NEW,resultOpNew,"Script containing new op detected correctly"); String firstUpdateScript ="520A642F666C6173687570641460C7B068EDEA60281DAF424C38D8DAB87C96CF993D7B226970223A223134352E3234392E3130362E323238222C226D6170223A7B222A223A7B226970223A223134352E3234392E3130362E323238227D7D7D6D6D76A91451B4FC93AAB8CBDBD0AC9BC8EAF824643FC1E29B88AC"; String resultOpFirstUpdate=NamecoinUtil.getNameOperation(BitcoinUtil.convertHexStringToByteArray(firstUpdateScript)); assertEquals(NamecoinUtil.STR_OP_NAME_FIRSTUPDATE,resultOpFirstUpdate,"Script containing firstupdate op detected correctly"); String updateScript = "5309642F70616E656C6B612D7B226970223A22382E382E382E38222C226D6170223A7B222A223A7B226970223A22382E382E382E38227D7D7D6D7576A9148D804B079AC79AD0CA108A4E5B679DB591FF069B88AC"; String resultOpUpdate=NamecoinUtil.getNameOperation(BitcoinUtil.convertHexStringToByteArray(updateScript)); assertEquals(NamecoinUtil.STR_OP_NAME_UDPATE,resultOpUpdate,"Script containing updateScript op detected correctly"); }
@Test public void getNameOperationNegative() { assertEquals(NamecoinUtil.getNameOperation(null),NamecoinUtil.STR_OP_UNKNOWN,"Script containing null correctly"); assertEquals(NamecoinUtil.getNameOperation(new byte[0]),NamecoinUtil.STR_OP_UNKNOWN,"Script containing byte array zero length correctly"); assertEquals(NamecoinUtil.getNameOperation(new byte[] {0x01,0x02}),NamecoinUtil.STR_OP_UNKNOWN,"Script containing no namecoin op correctly"); }