NamecoinUtil { public static String[] extractNamecoinField(byte[] scriptPubKey) { if ((scriptPubKey==null) || (scriptPubKey.length<2)) { return null; } if (!((scriptPubKey[0]==NamecoinUtil.OP_NAME_UDPATE)|| (scriptPubKey[0]==NamecoinUtil.OP_NAME_FIRSTUPDATE))) { return null; } String[] result = new String[2]; ByteBuffer scriptByteBuffer = ByteBuffer.wrap(scriptPubKey); scriptByteBuffer.get(); long nameSize=BitcoinUtil.convertVarIntByteBufferToLong(scriptByteBuffer); byte[] nameByteArray = new byte[(int)nameSize]; scriptByteBuffer.get(nameByteArray); String name = new String(nameByteArray,Charset.forName("UTF-8")); result[0]=name; if (scriptPubKey[0]==NamecoinUtil.OP_NAME_FIRSTUPDATE) { long intermediateInformationSize = BitcoinUtil.convertVarIntByteBufferToLong(scriptByteBuffer); byte[] intermediateInformation=new byte[(int)intermediateInformationSize]; scriptByteBuffer.get(intermediateInformation); } long valueSize = BitcoinUtil.convertVarIntByteBufferToLong(scriptByteBuffer); byte[] valueByteArray = new byte[(int)valueSize]; scriptByteBuffer.get(valueByteArray); String value = new String (valueByteArray, Charset.forName("UTF-8")); result[1]=value; return result; }  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 extractNamecoinFieldFirstUpdate() { String firstUpdateScript ="520A642F666C6173687570641460C7B068EDEA60281DAF424C38D8DAB87C96CF993D7B226970223A223134352E3234392E3130362E323238222C226D6170223A7B222A223A7B226970223A223134352E3234392E3130362E323238227D7D7D6D6D76A91451B4FC93AAB8CBDBD0AC9BC8EAF824643FC1E29B88AC"; String[] result=NamecoinUtil.extractNamecoinField(BitcoinUtil.convertHexStringToByteArray(firstUpdateScript)); assertNotNull( result,"Valid result obtained"); assertEquals("d/flashupd",result[0],"Domain name of first update detected correctly"); assertEquals("{\"ip\":\"145.249.106.228\",\"map\":{\"*\":{\"ip\":\"145.249.106.228\"}}}",result[1],"Domain value of first update detected correctly"); }
@Test public void extractNamecoinFieldUpdate() { String updateScript = "5309642F70616E656C6B612D7B226970223A22382E382E382E38222C226D6170223A7B222A223A7B226970223A22382E382E382E38227D7D7D6D7576A9148D804B079AC79AD0CA108A4E5B679DB591FF069B88AC"; String[] result=NamecoinUtil.extractNamecoinField(BitcoinUtil.convertHexStringToByteArray(updateScript)); assertNotNull( result,"Valid result obtained"); assertEquals("d/panelka",result[0],"Domain name of first update detected correctly"); assertEquals("{\"ip\":\"8.8.8.8\",\"map\":{\"*\":{\"ip\":\"8.8.8.8\"}}}",result[1],"Domain value of first update detected correctly"); }
@Test public void extractNamecoinFieldInvalid() { String[] resultNull = NamecoinUtil.extractNamecoinField(null); assertNull(resultNull,"Null script leads to null result"); String[] resultInvalid = NamecoinUtil.extractNamecoinField(new byte[] {0x01,0x02,0x03}); assertNull(resultInvalid,"Invalid script leads to null result"); }