Skip to content

Commit

Permalink
more swift kotlin bindings
Browse files Browse the repository at this point in the history
  • Loading branch information
Evanfeenstra committed Oct 16, 2023
1 parent 38925c7 commit 347abcd
Show file tree
Hide file tree
Showing 5 changed files with 83 additions and 0 deletions.
52 changes: 52 additions & 0 deletions sphinx-ffi/onion.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
### onion messages

**`create_onion(seed: String, time: String, hops: String, payload: Bytes)`**

- seed: 32-byte hex
- time: 13-digit timestamp (milliseconds). MUST be unique each time!
- hops: JSON string of hops (object with `pubkey` hex string)
- payload: message Bytes to encrypto for the final hop
- return encrypted onion Bytes

**`peel_onion(seed: String, time: String, payload Bytes)`**

- seed: 32-byte hex
- time: 13-digit timestamp (milliseconds)
- payload: encrypted onion (1401 bytes)
- returns decrypted content Bytes

### keysends

**`sha_256(msg: Bytes)`**

- msg: Bytes to hash
- returns hex-encoded hash string

**`create_keysend(seed: String, time: String, hops: String, msat: u64, rhash: String, payload: Bytes, curr_height: u32, preimage: String)`**

- seed: 32-byte hex
- time: 13-digit timestamp (milliseconds). MUST be unique each time!
- hops: JSON string of hops (see below)
- msat: value to send (in millisatoshi)
- rhash: hash of preimage (hex string)
- payload: message Bytes to encrypto for the final hop
- curr_height: current block height
- preimage: random 32-byte hex string
- returns encrypted onion Bytes

```ts
interface Hop {
pubkey: string;
short_channel_id: number;
cltv_expiry_delta: number;
fee_msat?: number; // the last hop doesn't need this
}
```

**`peel_keysend(seed: String, time: String, rhash: String)`**

- seed: 32-byte hex
- time: 13-digit timestamp (milliseconds)
- rhash: hex-encoded payment hash

# MQTT setup
11 changes: 11 additions & 0 deletions sphinx-ffi/src/onion.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,17 @@ pub fn sha_256(msg: Vec<u8>) -> String {
hex::encode(sphinx::sha_256(&msg))
}

pub fn sign_ms(seed: String, time: String) -> Result<String> {
let km = make_keys_manager(&seed, &time)?;
let sig =
sphinx::sig::sign_message(time.as_bytes(), &km.get_node_secret_key()).map_err(|_| {
SphinxError::BadCiper {
r: "sign failed".to_string(),
}
})?;
Ok(hex::encode(sig))
}

pub fn create_onion(seed: String, time: String, hops: String, payload: Vec<u8>) -> Result<Vec<u8>> {
let km = make_keys_manager(&seed, &time)?;
let hops = parse_hops(&hops)?;
Expand Down
13 changes: 13 additions & 0 deletions sphinx-ffi/src/sphinxrs.swift
Original file line number Diff line number Diff line change
Expand Up @@ -932,6 +932,16 @@ public func `peelPayment`(`seed`: String, `time`: String, `payload`: Data, `prei
)
}

public func `signMs`(`seed`: String, `time`: String) throws -> String {
return try FfiConverterString.lift(
try rustCallWithError(FfiConverterTypeSphinxError.lift) {
uniffi_sphinxrs_fn_func_sign_ms(
FfiConverterString.lower(`seed`),
FfiConverterString.lower(`time`),$0)
}
)
}

private enum InitializationResult {
case ok
case contractVersionMismatch
Expand Down Expand Up @@ -1001,6 +1011,9 @@ private var initializationResult: InitializationResult {
if (uniffi_sphinxrs_checksum_func_peel_payment() != 5916) {
return InitializationResult.apiChecksumMismatch
}
if (uniffi_sphinxrs_checksum_func_sign_ms() != 65056) {
return InitializationResult.apiChecksumMismatch
}

return InitializationResult.ok
}
Expand Down
2 changes: 2 additions & 0 deletions sphinx-ffi/src/sphinxrs.udl
Original file line number Diff line number Diff line change
Expand Up @@ -69,4 +69,6 @@ namespace sphinxrs {
bytes peel_onion(string seed, string time, bytes payload);
[Throws=SphinxError]
bytes peel_payment(string seed, string time, bytes payload, string preimage);
[Throws=SphinxError]
string sign_ms(string seed, string time);
};
5 changes: 5 additions & 0 deletions sphinx-ffi/src/sphinxrsFFI.h
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,8 @@ RustBuffer uniffi_sphinxrs_fn_func_peel_onion(RustBuffer seed, RustBuffer time,
);
RustBuffer uniffi_sphinxrs_fn_func_peel_payment(RustBuffer seed, RustBuffer time, RustBuffer payload, RustBuffer preimage, RustCallStatus *_Nonnull out_status
);
RustBuffer uniffi_sphinxrs_fn_func_sign_ms(RustBuffer seed, RustBuffer time, RustCallStatus *_Nonnull out_status
);
RustBuffer ffi_sphinxrs_rustbuffer_alloc(int32_t size, RustCallStatus *_Nonnull out_status
);
RustBuffer ffi_sphinxrs_rustbuffer_from_bytes(ForeignBytes bytes, RustCallStatus *_Nonnull out_status
Expand Down Expand Up @@ -160,6 +162,9 @@ uint16_t uniffi_sphinxrs_checksum_func_peel_onion(void
);
uint16_t uniffi_sphinxrs_checksum_func_peel_payment(void

);
uint16_t uniffi_sphinxrs_checksum_func_sign_ms(void

);
uint32_t ffi_sphinxrs_uniffi_contract_version(void

Expand Down

0 comments on commit 347abcd

Please sign in to comment.