/** * Restore watch-only account deterministic public key from XPUB. * * @return DeterministicKey * */ private DeterministicKey createMasterPubKeyFromXPub(String xpubstr) throws AddressFormatException { byte[] xpubBytes = Base58.decodeChecked(xpubstr); ByteBuffer bb = ByteBuffer.wrap(xpubBytes); if(bb.getInt() != 0x0488B21E) { throw new AddressFormatException("invalid xpub version"); } byte[] chain = new byte[32]; byte[] pub = new byte[33]; // depth: bb.get(); // parent fingerprint: bb.getInt(); // child no. bb.getInt(); bb.get(chain); bb.get(pub); return HDKeyDerivation.createMasterPubKeyFromBytes(pub, chain); }
public static String deriveSIN(ECKey ecKey) throws IllegalArgumentException { // Get sha256 hash and then the RIPEMD-160 hash of the public key (this call gets the result in one step). byte[] pubKeyHash = ecKey.getPubKeyHash(); // Convert binary pubKeyHash, SINtype and version to Hex String version = "0F"; String SINtype = "02"; String pubKeyHashHex = bytesToHex(pubKeyHash); // Concatenate all three elements String preSIN = version + SINtype + pubKeyHashHex; // Convert the hex string back to binary and double sha256 hash it leaving in binary both times byte[] preSINbyte = hexToBytes(preSIN); byte[] hash2Bytes = Sha256Hash.hashTwice(preSINbyte); // Convert back to hex and take first four bytes String hashString = bytesToHex(hash2Bytes); String first4Bytes = hashString.substring(0, 8); // Append first four bytes to fully appended SIN string String unencoded = preSIN + first4Bytes; byte[] unencodedBytes = new BigInteger(unencoded, 16).toByteArray(); return Base58.encode(unencodedBytes); }
private byte[] getBytes(CreateArkTransactionRequest createArkTransactionRequest, String senderPublicKey) { ByteBuffer buffer = ByteBuffer.allocate(1000); buffer.order(ByteOrder.LITTLE_ENDIAN); buffer.put(createArkTransactionRequest.getType()); buffer.putInt((int) createArkTransactionRequest.getTimestamp()); // todo: fix downcast buffer.put(BaseEncoding.base16().lowerCase().decode(senderPublicKey)); if(createArkTransactionRequest.getRecipientId() != null){ buffer.put(Base58.decodeChecked(createArkTransactionRequest.getRecipientId())); } else { buffer.put(new byte[21]); } if (createArkTransactionRequest.getVendorField() != null) { byte[] vbytes = createArkTransactionRequest.getVendorField().getBytes(); if(vbytes.length < 65){ buffer.put(vbytes); buffer.put(new byte[64-vbytes.length]); } } else { buffer.put(new byte[64]); } buffer.putLong(createArkTransactionRequest.getAmount()); buffer.putLong(createArkTransactionRequest.getFee()); byte[] outBuffer = new byte[buffer.position()]; buffer.rewind(); buffer.get(outBuffer); return outBuffer; }
private byte[] parseBase58(String key) throws KeyFormatException { byte[] versionAndDataBytes; try { versionAndDataBytes = Base58.decodeChecked(key); } catch (AddressFormatException e) { throw new KeyFormatException(e); } version = versionAndDataBytes[0] & 0xFF; byte[] payload = new byte[versionAndDataBytes.length - 1]; System.arraycopy(versionAndDataBytes, 1, payload, 0, versionAndDataBytes.length - 1); clearData(versionAndDataBytes); return payload; }
public SigningKeyImpl(@Nonnull String s) throws AddressFormatException { Bytestring stripped = new Bytestring(Base58.decodeChecked(s)); boolean compressed; switch (stripped.bytes.length) { case (34) : { if (stripped.bytes[33] != 1) { throw new AddressFormatException("Wrong compressed byte"); } stripped = stripped.drop(-1); compressed = true; break; } case (33) : { compressed = false; break; } default : { throw new AddressFormatException("Invalid length."); } } switch (stripped.bytes[0]) { case (-128) : { params = NetworkParameters.fromID(NetworkParameters.ID_MAINNET); break; } case (-17) : { params = NetworkParameters.fromID(NetworkParameters.ID_TESTNET); break; } default: { throw new AddressFormatException("Invalid net byte."); } } stripped = stripped.drop(1); signingKey = ECKey.fromPrivate(stripped.bytes, compressed); vk = new VerificationKeyImpl(signingKey.getPubKey(), params); }
/** * Base58Checksum Util * * @param b * @return */ private String encodeWithChecksum(byte[] base58Bytes){ //String strBase58Encoded = Base58.encode(b); //byte[] base58Bytes = strBase58Encoded.getBytes(); byte[] checksum = copyOfRange(Utils.doubleDigest(base58Bytes), 0, 4); // append byte[] base58Checksum = new byte[base58Bytes.length + checksum.length]; System.arraycopy(base58Bytes, 0, base58Checksum, 0, base58Bytes.length); System.arraycopy(checksum, 0, base58Checksum, base58Bytes.length, checksum.length); // return Base58.encode(base58Checksum);//new String(base58Checksum); }
/** * This methods creates a QRCode which contains a list of IP addresses and a public key for a TLS connection. The * data is serialized with avro and then Base58 encoded. The resulting string is then stored inside the QR code which * is returned as a {@link java.awt.image.BufferedImage}. */ public static BufferedImage CreateQRCodeForTLSSetup(List<String> ipAddresses, PublicKey publicKey) throws IOException, WriterException { ReflectDatumWriter<QRCodeData> specificDatumWriter = new ReflectDatumWriter<>(QRCodeData.class); ByteArrayOutputStream bout = new ByteArrayOutputStream(); Encoder encoder = EncoderFactory.get().binaryEncoder(bout, null); QRCodeData data = new QRCodeData( IPAddressHelper.getAllUsableIPAddresses(), publicKey ); specificDatumWriter.write(data, encoder); encoder.flush(); bout.flush(); return CreateQRCodeFromString(Base58.encode(bout.toByteArray())); }
public static String getAddressFromPrivateKey(BigInteger privateKey) { return getAddressFromPrivateKey(Base58.encode(privateKey.toByteArray())); }
/** * Converts a given public key to a valid MultiChain address. * See {@link <a href="http://www.multichain.com/developers/address-key-format/">MultiChain Documentation</a>} * @param pubKey byte array containing the public key * @return String representing the corresponding address. */ public static String getPublicAddress(String[] version, String addressChecksum, byte[] pubKey) { //Step 3 MessageDigest digest; try { digest = MessageDigest.getInstance("SHA-256"); digest.reset(); byte[] hash = digest.digest(pubKey); //Step 4 RIPEMD160Digest ripemd = new RIPEMD160Digest(); ripemd.update(hash, 0, hash.length); byte[] out = new byte[20]; ripemd.doFinal(out, 0); String hashStr = Util.byteArrayToHexString(out); //Step 5 String step5 = ""; if (BuildConfig.DEBUG && version.length != 4) throw new AssertionError("Version length != 4"); for (int i = 0; i < 4; i++) { //Assumes version.length == 4 step5 += version[i] + hashStr.substring((i*10),(i*10)+10); } digest.reset(); //Step 6 byte[] step6 = digest.digest(Util.hexStringToByteArray(step5)); digest.reset(); //Step 7 byte[] step7 = digest.digest(step6); digest.reset(); //Step 8 byte[] checksum = new byte[]{ step7[0],step7[1],step7[2],step7[3] }; //Step 9 byte[] byteAddressChecksum = Util.hexStringToByteArray(addressChecksum); byte[] xor = new byte[4]; for (int i = 0; i < 4; i++) { int xorvalue = (int)checksum[i] ^ (int)byteAddressChecksum[i]; xor[i] = (byte)(0xff & xorvalue); } //Step 10 String addressbytes = step5 + Util.byteArrayToHexString(xor); //Step 11 String address = Base58.encode(Util.hexStringToByteArray(addressbytes)); return address; } catch (NoSuchAlgorithmException e1) { e1.printStackTrace(); return null; } }
public static String wifToHex(String wifKey) throws Exception { byte[] bytes = Base58.decode(wifKey); String pk = Util.encodeHexArray(bytes); pk = pk.substring(2, pk.length() - 10); return pk; }
private String testEncode(String what) { return HEX.encode(Base58.decodeChecked(what)); }
private String testEncode(String what) throws AddressFormatException { return HEX.encode(Base58.decodeChecked(what)); }
/** * Create a share from its string representation. * * @param encodedShare * the string representing the share * @param network * the network that this share is supposed to be used on * @return the decoded share or null if the string was not a valid share * encoding * @throws AddressFormatException */ public static Share fromString(String encodedShare, NetworkParameters network) throws AddressFormatException { // Base58 decode byte[] decoded = Base58.decodeChecked(encodedShare); if (decoded == null || decoded.length < 2) { return null; } ByteArrayInputStream reader = new ByteArrayInputStream(decoded); //ByteReader reader = new ByteReader(decoded); try { // Find encoding byte versionByte = (byte) reader.read(); byte prefixByte = (byte) reader.read(); EncodingParameters params = findEncodingParameters(decoded.length, versionByte, prefixByte, network); if (params == null) { return null; } byte[] contentHash = null; int threshold; int shareNumber; switch (params.encodingFormat) { case LONG: reader.read(contentHash, 0, 2);//contentHash = reader.getBytes(2); threshold = b2i((byte)reader.read()); shareNumber = b2i((byte)reader.read()); break; case SHORT: contentHash = null; threshold = 0; shareNumber = b2i((byte)reader.read()); break; case COMPACT: int i = b2i((byte)reader.read()); contentHash = null; threshold = i >> 4; shareNumber = i & 0x0f; break; default: return null; } //byte[] content = reader.getBytes(params.contentSize); byte[] content = new byte[params.contentSize]; reader.read(content, 0, params.contentSize); return new Share(params.contentType, contentHash, shareNumber, threshold, content, params.encodingFormat, network); } catch (Exception e) { // This should not happen as we already have checked the content // length return null; } }