Skip to content

Commit

Permalink
Test verify
Browse files Browse the repository at this point in the history
  • Loading branch information
isoosiss7 committed Apr 4, 2021
1 parent e5dd944 commit 7a01e0b
Show file tree
Hide file tree
Showing 6 changed files with 29 additions and 7 deletions.
8 changes: 1 addition & 7 deletions packages/native-utils/common/src/serde.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,12 +36,6 @@ impl<'de> Deserialize<'de> for RecoverableSignature {
D: Deserializer<'de>,
{
let bytes: Bytes = Deserialize::deserialize(deserializer)?;
assert_eq!(65,bytes.0.len());
let mut a: [u8; 64] = [0; 64];
a.copy_from_slice(&bytes.0[0..64]);
Ok(RecoverableSignature(
Signature::parse(&a),
RecoveryId::parse(bytes.0[64] - 27).map_err(D::Error::custom)?
))
Ok(RecoverableSignature::from_bytes(bytes).map_err(D::Error::custom)?)
}
}
10 changes: 10 additions & 0 deletions packages/native-utils/common/src/state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -263,6 +263,16 @@ impl RecoverableSignature {
v.push(self.1.serialize());
Bytes(v)
}

pub fn from_bytes(bytes: Bytes) -> Result<RecoverableSignature, &'static str> {
assert_eq!(65,bytes.0.len());
let mut a: [u8; 64] = [0; 64];
a.copy_from_slice(&bytes.0[0..64]);
Ok(RecoverableSignature(
Signature::parse(&a),
RecoveryId::parse(bytes.0[64] - 27).map_err(|_| "Invalid recovery ID")?
))
}
}

#[derive(Serialize)]
Expand Down
8 changes: 8 additions & 0 deletions packages/native-utils/lib/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -79,3 +79,11 @@ export function signState(state: State, privateKey: string): StateWithHashAndSig
* @param signature A signature resulting from a previous call to `signState`.
*/
export function recoverAddress(state: State, signature: string): string

/**
* Verifies a signature.
*
* @param state A Nitro state.
* @param signature A signature resulting from a previous call to `signState`.
*/
export function verifySignature(state: State, signature: string): boolean
3 changes: 3 additions & 0 deletions packages/native-utils/lib/index.native.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ const {

signState,
recoverAddress,
verifySignature,
} = require('../native/index.node')

function unwrapResult({ Ok, Err }) {
Expand Down Expand Up @@ -40,4 +41,6 @@ module.exports = {
},

recoverAddress: (state, signature) => unwrapResult(recoverAddress(state, signature)),

verifySignature: (state, signature) => unwrapResult(verifySignature(state, signature)),
}
4 changes: 4 additions & 0 deletions packages/native-utils/native/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,4 +37,8 @@ export! {
fn recoverAddress(state: State, signature: Bytes) -> Result<String, &'static str> {
state.recover_address(signature)
}

fn verifySignature(state: State, signature: Bytes) -> Result<bool, &'static str> {
state.verify(RecoverableSignature::from_bytes(signature)?)
}
}
3 changes: 3 additions & 0 deletions packages/native-utils/tests/sign.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,9 @@ describe('Sign state', () => {

expect(nativeSignature).toStrictEqual(oldSignature)
expect(wasmSignature).toStrictEqual(oldSignature)

expect(native.verifySignature(state, nativeSignature)).toBe(true)
expect(native.verifySignature(state, wasmSignature)).toBe(true)
})

test('Catches invalid private key', async () => {
Expand Down

0 comments on commit 7a01e0b

Please sign in to comment.