Skip to content

Commit

Permalink
Merge pull request #2 from joshhchun/ci
Browse files Browse the repository at this point in the history
Added CI workflow to build, format, lint, and test the code
  • Loading branch information
r-n-o authored May 24, 2024
2 parents f52294a + 0a72e3a commit 9b24ee2
Show file tree
Hide file tree
Showing 3 changed files with 85 additions and 16 deletions.
20 changes: 20 additions & 0 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
name: Continuous Integration

on:
push:
branches:
- main
pull_request:
branches:
- main

jobs:
ubuntu:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- uses: actions-rs/toolchain@v1
with:
toolchain: stable
components: clippy,rustfmt
- run: ./ci.sh
17 changes: 17 additions & 0 deletions ci.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
#!/bin/bash

set -e

# Build
cargo build

# Check formatting
cargo fmt -- --check

# Run Clippy
cargo clippy -- -D warnings

# Run tests
cargo test

echo "CI checks passed successfully."
64 changes: 48 additions & 16 deletions src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use base64::prelude::BASE64_URL_SAFE_NO_PAD;
use base64::Engine;
use p256::ecdsa::{Signature, SigningKey};
use p256::ecdsa::signature::Signer;
use p256::ecdsa::{Signature, SigningKey};
use p256::FieldBytes;
use serde::Serialize;
use thiserror::Error;
Expand All @@ -28,7 +28,9 @@ pub struct TurnkeyApiKey {

pub fn stamp(request_body: String, api_key: &TurnkeyApiKey) -> Result<String, StampError> {
let private_key_bytes = hex::decode(&api_key.private_key_hex)?;
let signing_key: SigningKey = SigningKey::from_bytes(FieldBytes::from_slice(&private_key_bytes)).map_err(|_| StampError::InvalidPrivateKeyBytes)?;
let signing_key: SigningKey =
SigningKey::from_bytes(FieldBytes::from_slice(&private_key_bytes))
.map_err(|_| StampError::InvalidPrivateKeyBytes)?;
let sig: Signature = signing_key.sign(request_body.as_bytes());

let stamp = ApiStamp {
Expand All @@ -49,42 +51,72 @@ mod tests {

#[test]
fn test_stamps() {
let stamp = stamp(
"hello from TKHQ".to_string(),
&TurnkeyApiKey { private_key_hex: "9720de87f61537e481f95f4433bed97b9d60719457c4dd20dac4bbf377f59c69".to_string(), public_key_hex: "02a1d9ee281053cf73c07678d6c1231216e8434f87662b75f08c66882c2f95ee45".to_string()},
).unwrap();
let stamp =
stamp(
"hello from TKHQ".to_string(),
&TurnkeyApiKey {
private_key_hex:
"9720de87f61537e481f95f4433bed97b9d60719457c4dd20dac4bbf377f59c69"
.to_string(),
public_key_hex:
"02a1d9ee281053cf73c07678d6c1231216e8434f87662b75f08c66882c2f95ee45"
.to_string(),
},
)
.unwrap();

// The stamp should be valid base64
let decoded_stamp_bytes = BASE64_URL_SAFE_NO_PAD.decode(stamp).unwrap();

// These bytes should be valid UTF8 characters
let decoded_stamp_string = String::from_utf8(decoded_stamp_bytes).unwrap();

// The resulting string should be valid JSON
let json_stamp: Value = serde_json::from_str(&decoded_stamp_string).unwrap();

// And finally: the signature scheme and public key should be correct
assert_eq!(json_stamp["scheme"], "SIGNATURE_SCHEME_TK_API_P256");
assert_eq!(json_stamp["publicKey"], "02a1d9ee281053cf73c07678d6c1231216e8434f87662b75f08c66882c2f95ee45");
assert_eq!(
json_stamp["publicKey"],
"02a1d9ee281053cf73c07678d6c1231216e8434f87662b75f08c66882c2f95ee45"
);
}

#[test]
fn test_bad_hex() {
let err = stamp(
"body".to_string(),
&TurnkeyApiKey { private_key_hex: "bad-private-key".to_string(), public_key_hex: "".to_string()},
).unwrap_err();
assert_eq!(format!("{:?}", err), "InvalidPrivateKeyString(OddLength)".to_string());
assert_eq!(err.to_string(), "cannot decode private key: invalid hex".to_string());
&TurnkeyApiKey {
private_key_hex: "bad-private-key".to_string(),
public_key_hex: "".to_string(),
},
)
.unwrap_err();
assert_eq!(
format!("{:?}", err),
"InvalidPrivateKeyString(OddLength)".to_string()
);
assert_eq!(
err.to_string(),
"cannot decode private key: invalid hex".to_string()
);
}

#[test]
fn test_bad_bytes() {
let err = stamp(
"body".to_string(),
&TurnkeyApiKey { private_key_hex: "fffffffff61537e481f95f4433bed97b9d60719457c4dd20dac4bbf377f59c70".to_string(), public_key_hex: "".to_string()},
).unwrap_err();
&TurnkeyApiKey {
private_key_hex: "fffffffff61537e481f95f4433bed97b9d60719457c4dd20dac4bbf377f59c70"
.to_string(),
public_key_hex: "".to_string(),
},
)
.unwrap_err();
assert_eq!(format!("{:?}", err), "InvalidPrivateKeyBytes".to_string());
assert_eq!(err.to_string(), "cannot load private key: invalid bytes".to_string());
assert_eq!(
err.to_string(),
"cannot load private key: invalid bytes".to_string()
);
}
}

0 comments on commit 9b24ee2

Please sign in to comment.