From 9f846925b3d41b06f748856719d4c615cfc5fbe3 Mon Sep 17 00:00:00 2001
From: Christian Decker <decker.christian@gmail.com>
Date: Mon, 12 Dec 2016 13:12:58 +0100
Subject: [PATCH] bitcoin: Add comparison between pubkeys

Some of the routing messages rely on a canonical ordering of pubkeys.
---
 bitcoin/pubkey.c | 8 ++++++++
 bitcoin/pubkey.h | 3 +++
 2 files changed, 11 insertions(+)

diff --git a/bitcoin/pubkey.c b/bitcoin/pubkey.c
index 54d2a4c4e..046371819 100644
--- a/bitcoin/pubkey.c
+++ b/bitcoin/pubkey.c
@@ -65,3 +65,11 @@ bool pubkey_eq(const struct pubkey *a, const struct pubkey *b)
 {
 	return structeq(&a->pubkey, &b->pubkey);
 }
+
+int pubkey_cmp(const struct pubkey *a, const struct pubkey *b)
+{
+	u8 keya[33], keyb[33];
+	pubkey_to_der(keya, a);
+	pubkey_to_der(keyb, b);
+	return memcmp(keya, keyb, sizeof(keya));
+}
diff --git a/bitcoin/pubkey.h b/bitcoin/pubkey.h
index 03e6748e2..6e63f2ab4 100644
--- a/bitcoin/pubkey.h
+++ b/bitcoin/pubkey.h
@@ -32,4 +32,7 @@ void pubkey_to_der(u8 der[PUBKEY_DER_LEN], const struct pubkey *key);
 
 /* Are these keys equal? */
 bool pubkey_eq(const struct pubkey *a, const struct pubkey *b);
+
+/* Compare the keys `a` and `b`. Return <0 if `a`<`b`, 0 if equal and >0 otherwise */
+int pubkey_cmp(const struct pubkey *a, const struct pubkey *b);
 #endif /* LIGHTNING_PUBKEY_H */