@@ -41,13 +41,45 @@ struct LockPublicKey {
41
41
self . isCompressed = ( header == 0x02 || header == 0x03 )
42
42
}
43
43
44
+ /**
45
+ * Guarantees to init a public key uncompressed with 65 bytes in the following form:
46
+ *
47
+ * 0x04 ++ xBytes ++ yBytes
48
+ *
49
+ * Where `xBytes` and `yBytes` represent a 32-byte coordinates of a point
50
+ * on the secp256k1 elliptic curve, which follow the formula below:
51
+ *
52
+ * y^2 == x^3 + 7
53
+ *
54
+ * @return uncompressed public key
55
+ */
56
+ init ( x: Data , y: Data ) {
57
+ let header : UInt8 = 0x04
58
+ self . data = [ header] + x + y
59
+ self . isCompressed = false
60
+ }
61
+
44
62
func compressedPublicKey( ) -> LockPublicKey {
45
63
LockPublicKey ( bytes: KeyHelpers . compressPublicKey ( fromPublicKey: data) )
46
64
}
47
65
48
66
func uncompressedPublicKey( ) -> LockPublicKey {
49
67
LockPublicKey ( bytes: KeyHelpers . uncompressPublicKey ( fromPublicKey: data) )
50
68
}
69
+
70
+ func pointCurve( ) throws -> PointOnCurve {
71
+ let selfUncompressed = uncompressedPublicKey ( )
72
+ var xAndY = selfUncompressed. data
73
+ xAndY. removeFirst ( ) // Remove the header
74
+ let expectedLengthOfScalar = Scalar32Bytes . expectedByteCount
75
+ let expectedLengthOfKey = expectedLengthOfScalar * 2
76
+ guard xAndY. count == expectedLengthOfKey else {
77
+ fatalError ( " expected length of key is \( expectedLengthOfKey) bytes, but got: \( xAndY. count) " )
78
+ }
79
+ let x = xAndY. prefix ( expectedLengthOfScalar)
80
+ let y = xAndY. suffix ( expectedLengthOfScalar)
81
+ return try PointOnCurve ( x: x, y: y)
82
+ }
51
83
}
52
84
53
85
extension LockPublicKey : Equatable {
0 commit comments