Skip to content

Commit 3cbe1aa

Browse files
authored
Add ability to create Threema ID QR code content (#15)
1 parent 269f211 commit 3cbe1aa

File tree

4 files changed

+43
-1
lines changed

4 files changed

+43
-1
lines changed

README.md

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -219,6 +219,21 @@ You can query the remaining credits via API:
219219
System.out.println("Remaining credits: " + gw.getRemainingCredits());
220220
```
221221

222+
### Threema QR Code
223+
224+
To establich trust with your users you can create a QR code with your
225+
Threema ID and your public key. When the user scans that QR code your
226+
gateway ID will show "green" in their contact list. The `qrcode()`
227+
method creates the text content which need to be converted to a QR
228+
graphic with a library of your choice (e.g.
229+
[zxing](https://github.com/zxing/zxing)).
230+
231+
```java
232+
PublicKey publicKey = KeyEncoder.getPublicKey(privateKey);
233+
String qrtext = KeyEncoder.qrcode(threemaid, publicKey);
234+
System.out.println(qrtext);
235+
```
236+
222237
### Callback Handling
223238

224239
You can configure your own HTTP server to receive messages from the

src/main/java/com/mountainminds/three4j/KeyEncoder.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,7 @@ public void nextBytes(byte[] bytes) {
128128
* @param publicKey public key
129129
* @return QR code text
130130
*/
131-
public String qrcode(ThreemaId threemaid, PublicKey publicKey) {
131+
public static String qrcode(ThreemaId threemaid, PublicKey publicKey) {
132132
return String.format("3mid:%s,%s", threemaid.getValue(), encode(publicKey));
133133
}
134134

src/test/java/com/mountainminds/three4j/KeyEncoderTest.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,4 +26,12 @@ public void getPublicKey_should_return_corresponding_public_key() {
2626
assertEquals(pair.getPublic(), publicKey);
2727
}
2828

29+
@Test
30+
public void qrcode_should_create_threema_qr_code() {
31+
var threemaid = ThreemaId.of("*GWYTEST");
32+
var publicKey = KeyEncoder.decodePublicKey("1234567812345678123456781234567812345678123456781234567812345678");
33+
var qr = KeyEncoder.qrcode(threemaid, publicKey);
34+
assertEquals("3mid:*GWYTEST,1234567812345678123456781234567812345678123456781234567812345678", qr);
35+
}
36+
2937
}

src/test/java/com/mountainminds/three4j/guide/ApiGuide.java

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,7 @@ public static void main(String[] args) throws Exception {
8080
ThreemaId receiverId = lookups(gw);
8181
sendtextmessage(gw, myPrivateKey, receiverId);
8282
simplemessage(gw);
83+
threemaidqrcode(from, myPrivateKey);
8384

8485
callbackserver(gw, secret, myPrivateKey);
8586

@@ -252,6 +253,24 @@ static void accountinfo(Gateway gw) throws IOException {
252253

253254
}
254255

256+
private static void threemaidqrcode(ThreemaId threemaid, PrivateKey privateKey) {
257+
// ### Threema QR Code
258+
//
259+
// To establich trust with your users you can create a QR code with your
260+
// Threema ID and your public key. When the user scans that QR code your
261+
// gateway ID will show "green" in their contact list. The `qrcode()`
262+
// method creates the text content which need to be converted to a QR
263+
// graphic with a library of your choice (e.g.
264+
// [zxing](https://github.com/zxing/zxing)).
265+
266+
// <CODE>
267+
PublicKey publicKey = KeyEncoder.getPublicKey(privateKey);
268+
String qrtext = KeyEncoder.qrcode(threemaid, publicKey);
269+
System.out.println(qrtext);
270+
// </CODE>
271+
272+
}
273+
255274
static void callbackserver(Gateway gw, String secret, PrivateKey myPrivateKey) throws Exception {
256275

257276
// ### Callback Handling

0 commit comments

Comments
 (0)