Skip to content

Commit 24d657b

Browse files
authored
Merge pull request #56 from lightsparkdev/release/crypto-v0.2.0
Merge release/crypto-v0.2.0 into main
2 parents e3af022 + 0827382 commit 24d657b

File tree

129 files changed

+1857
-599
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

129 files changed

+1857
-599
lines changed

.editorconfig

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ indent_style = space
88
insert_final_newline = true
99
ktlint_code_style = official
1010
ktlint_function_signature_body_expression_wrapping = default
11-
ktlint_function_signature_rule_force_multiline_when_parameter_count_greater_or_equal_than = -1
11+
ktlint_function_signature_rule_force_multiline_when_parameter_count_greater_or_equal_than = unset
1212
ktlint_ignore_back_ticked_identifier = false
1313
ktlint_standard_trailing-comma-on-call-site = disabled
1414
max_line_length = 120

androidwalletdemo/src/main/java/com/lightspark/androidwalletdemo/requestpayment/RequestPaymentViewModel.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ class RequestPaymentViewModel @Inject constructor(
3434
invoice.data.let {
3535
Lce.Content(
3636
RequestPaymentUiState(
37-
invoice.data.encodedPaymentRequest,
37+
invoice.data.data.encodedPaymentRequest,
3838
invoiceAmount.value,
3939
),
4040
)

crypto/gradle.properties

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
GROUP=com.lightspark
22
POM_ARTIFACT_ID=lightspark-crypto
33
# Don't bump this manually. Run `scripts/versions.main.kt <new_version>` to bump the version instead.
4-
VERSION_NAME=0.1.2
4+
VERSION_NAME=0.2.0
55

66
POM_DESCRIPTION=The Lightspark shared crypto library.
77
POM_INCEPTION_YEAR=2023
Binary file not shown.
Binary file not shown.
Binary file not shown.

crypto/src/commonMain/kotlin/com/lightspark/sdk/crypto/Signing.kt

Lines changed: 35 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ package com.lightspark.sdk.crypto
44

55
import com.lightspark.sdk.crypto.internal.LightsparkSigner
66
import com.lightspark.sdk.crypto.internal.Mnemonic
7+
import com.lightspark.sdk.crypto.internal.Network
78
import com.lightspark.sdk.crypto.internal.Seed
89
import com.lightspark.sdk.crypto.internal.use
910

@@ -20,53 +21,60 @@ object Signing {
2021
}
2122
}
2223

23-
fun ecdh(seedBytes: ByteArray, derivationPath: String, otherPubKey: String): ByteArray {
24-
val signer = LightsparkSigner()
25-
var seed: Seed? = null
26-
try {
27-
seed = Seed(seedBytes.toUByteArray().toList())
28-
return signer.ecdh(seed, derivationPath, otherPubKey).toUByteArray().toByteArray()
29-
} finally {
30-
signer.close()
31-
seed?.close()
24+
fun ecdh(seedBytes: ByteArray, network: Network, otherPubKeyHex: String): ByteArray {
25+
return withSeedAndSigner(seedBytes, network) { _, signer ->
26+
val otherPubKeyBytes = otherPubKeyHex.hexToByteArray().toUByteArray().toList()
27+
signer.ecdh(otherPubKeyBytes).toUByteArray().toByteArray()
3228
}
3329
}
3430

35-
fun derivePublicKey(seedBytes: ByteArray, derivationPath: String): String {
36-
val signer = LightsparkSigner()
37-
var seed: Seed? = null
38-
try {
39-
seed = Seed(seedBytes.toUByteArray().toList())
40-
return signer.derivePublicKey(seed, derivationPath)
41-
} finally {
42-
signer.close()
43-
seed?.close()
31+
fun derivePublicKey(seedBytes: ByteArray, network: Network, derivationPath: String): String {
32+
return withSeedAndSigner(seedBytes, network) { _, signer ->
33+
signer.derivePublicKey(derivationPath)
4434
}
4535
}
4636

4737
@JvmOverloads
4838
fun signMessage(
4939
message: ByteArray,
5040
seedBytes: ByteArray,
41+
network: Network,
5142
derivationPath: String? = null,
43+
isRaw: Boolean = false,
5244
multTweak: ByteArray? = null,
5345
addTweak: ByteArray? = null,
5446
): ByteArray {
55-
val signer = LightsparkSigner()
56-
var seed: Seed? = null
57-
try {
58-
seed = Seed(seedBytes.toUByteArray().toList())
59-
val signature = signer.deriveKeyAndSign(
60-
seed,
47+
return withSeedAndSigner(seedBytes, network) { _, signer ->
48+
signer.deriveKeyAndSign(
6149
message = message.toUByteArray().toList(),
6250
derivationPath = derivationPath ?: "",
51+
isRaw = isRaw,
6352
mulTweak = multTweak?.toUByteArray()?.toList(),
6453
addTweak = addTweak?.toUByteArray()?.toList(),
65-
)
66-
return signature.toUByteArray().toByteArray()
54+
).toUByteArray().toByteArray()
55+
}
56+
}
57+
58+
private fun <T> withSeedAndSigner(seedBytes: ByteArray, network: Network, block: (Seed, LightsparkSigner) -> T): T {
59+
var seed: Seed? = null
60+
var signer: LightsparkSigner? = null
61+
try {
62+
seed = Seed(seedBytes.toUByteArray().toList())
63+
signer = LightsparkSigner(seed, network)
64+
return block(seed, signer)
6765
} finally {
68-
signer.close()
66+
signer?.close()
6967
seed?.close()
7068
}
7169
}
7270
}
71+
72+
private fun String.hexToByteArray(): ByteArray {
73+
check(length % 2 == 0) { "Must have an even length" }
74+
75+
val byteIterator = chunkedSequence(2)
76+
.map { it.toInt(16).toByte() }
77+
.iterator()
78+
79+
return ByteArray(length / 2) { byteIterator.next() }
80+
}

0 commit comments

Comments
 (0)