use crate::signature::*;
use sp_core::H160;

const MESSAGE: &[u8] = b"Link: did:ad3:hwtGPq42GojPtyx5ngtSRSpJfjN";

#[test]
fn should_generate_message() {
    pub const DID_ALICE: H160 = H160([
        0x32, 0xac, 0x79, 0x9d, //
        0x35, 0xde, 0x72, 0xa2, //
        0xae, 0x57, 0xa4, 0x6c, //
        0xa9, 0x75, 0x31, 0x9f, //
        0xbb, 0xb1, 0x25, 0xa9,
    ]);
    assert_eq!(generate_message(&DID_ALICE), MESSAGE.to_vec());
}

#[test]
fn should_recover_btc() {
    // PK: 5KYZdUEo39z3FPrtuX2QbbwGnNP5zTd7yyr2SC1j299sBCnWjss
    let address = b"1F3sAm6ZtwLAUnj7d38pGFxtP3RVEvtsbV".to_vec();

    // 1cb5b2e3b269cd9b78c2fec806cf667f35ccbf8934bdcb8b125ed70d64e48c8b9c81853b2c0e905184e57add15874b0cffcc5fe0cce33f3a31f508bb569b19b4a2
    let signature = [
        0x1c, 0xb5, 0xb2, 0xe3, 0xb2, 0x69, 0xcd, 0x9b, 0x78, 0xc2, 0xfe, 0xc8, 0x06, 0xcf, 0x66,
        0x7f, 0x35, 0xcc, 0xbf, 0x89, 0x34, 0xbd, 0xcb, 0x8b, 0x12, 0x5e, 0xd7, 0x0d, 0x64, 0xe4,
        0x8c, 0x8b, 0x9c, 0x81, 0x85, 0x3b, 0x2c, 0x0e, 0x90, 0x51, 0x84, 0xe5, 0x7a, 0xdd, 0x15,
        0x87, 0x4b, 0x0c, 0xff, 0xcc, 0x5f, 0xe0, 0xcc, 0xe3, 0x3f, 0x3a, 0x31, 0xf5, 0x08, 0xbb,
        0x56, 0x9b, 0x19, 0xb4, 0xa2,
    ];

    let mut sig = [0u8; 65];
    sig.copy_from_slice(&signature);

    assert_eq!(
        recover_address(Network::Bitcoin, address.clone(), sig, MESSAGE.to_vec()).unwrap(),
        address
    );
}

#[test]
fn should_recover_btc_segwit() {
    // PK: p2wpkh:Kzbv1fJbGs24LpWjdPNgvtBEdkVF9w1urLiqbfrvTt2YGqQS6SSC
    let address = b"bc1qug9quswyl8pxalrfudfr9p34mmjvj2f6tx6f0k".to_vec();

    // 203b166d7adfe349fdae5b36e1262a979c70b1e041228df149d8b7d0d5278b6aad4b027693ae2eda794f13da93b928505c4b1d23da572ebeba9696edc4af57cf58
    let signature = [
        0x20, 0x3b, 0x16, 0x6d, 0x7a, 0xdf, 0xe3, 0x49, 0xfd, 0xae, 0x5b, 0x36, 0xe1, 0x26, 0x2a,
        0x97, 0x9c, 0x70, 0xb1, 0xe0, 0x41, 0x22, 0x8d, 0xf1, 0x49, 0xd8, 0xb7, 0xd0, 0xd5, 0x27,
        0x8b, 0x6a, 0xad, 0x4b, 0x02, 0x76, 0x93, 0xae, 0x2e, 0xda, 0x79, 0x4f, 0x13, 0xda, 0x93,
        0xb9, 0x28, 0x50, 0x5c, 0x4b, 0x1d, 0x23, 0xda, 0x57, 0x2e, 0xbe, 0xba, 0x96, 0x96, 0xed,
        0xc4, 0xaf, 0x57, 0xcf, 0x58,
    ];

    let mut sig = [0u8; 65];
    sig.copy_from_slice(&signature);

    assert_eq!(
        recover_address(Network::Bitcoin, address.clone(), sig, MESSAGE.to_vec()).unwrap(),
        address
    );
}

#[test]
fn should_recover_dot() {
    // URI: //Alice
    let address = b"5GrwvaEF5zXb26Fz9rcQpDWS57CtERHpNehXCPcNoHGKutQY".to_vec();

    // 00b019009d196eb10f3d7f46309b591d21950fa617ced4f0b01b908b472bf0566610691636fde1088383b2b8134e5aee1bf48b2f4b46056709f8f0d81f79ebe58b
    let signature = [
        0x00, 0xb0, 0x19, 0x00, 0x9d, 0x19, 0x6e, 0xb1, 0x0f, 0x3d, 0x7f, 0x46, 0x30, 0x9b, 0x59,
        0x1d, 0x21, 0x95, 0x0f, 0xa6, 0x17, 0xce, 0xd4, 0xf0, 0xb0, 0x1b, 0x90, 0x8b, 0x47, 0x2b,
        0xf0, 0x56, 0x66, 0x10, 0x69, 0x16, 0x36, 0xfd, 0xe1, 0x08, 0x83, 0x83, 0xb2, 0xb8, 0x13,
        0x4e, 0x5a, 0xee, 0x1b, 0xf4, 0x8b, 0x2f, 0x4b, 0x46, 0x05, 0x67, 0x09, 0xf8, 0xf0, 0xd8,
        0x1f, 0x79, 0xeb, 0xe5, 0x8b,
    ];

    let mut sig = [0u8; 65];
    sig.copy_from_slice(&signature);

    assert_eq!(
        recover_address(Network::Polkadot, address.clone(), sig, MESSAGE.to_vec()).unwrap(),
        address
    );
}

#[test]
fn should_recover_eth() {
    // PK: be6383dad004f233317e46ddb46ad31b16064d14447a95cc1d8c8d4bc61c3728
    // eb014f8c8b418db6b45774c326a0e64c78914dc0
    let address = vec![
        0xeb, 0x01, 0x4f, 0x8c, 0x8b, 0x41, 0x8d, 0xb6, 0xb4, 0x57, 0x74, 0xc3, 0x26, 0xa0, 0xe6,
        0x4c, 0x78, 0x91, 0x4d, 0xc0,
    ];

    // 193883369b84888e1dded1e83a8fd92cdde41b9a9c977be5ddbbb259783a69d060d120704760eb82671889c664be25d6cf6f25b9efe781fb637bbd6097da0e621c
    let signature = [
        0x19, 0x38, 0x83, 0x36, 0x9b, 0x84, 0x88, 0x8e, 0x1d, 0xde, 0xd1, 0xe8, 0x3a, 0x8f, 0xd9,
        0x2c, 0xdd, 0xe4, 0x1b, 0x9a, 0x9c, 0x97, 0x7b, 0xe5, 0xdd, 0xbb, 0xb2, 0x59, 0x78, 0x3a,
        0x69, 0xd0, 0x60, 0xd1, 0x20, 0x70, 0x47, 0x60, 0xeb, 0x82, 0x67, 0x18, 0x89, 0xc6, 0x64,
        0xbe, 0x25, 0xd6, 0xcf, 0x6f, 0x25, 0xb9, 0xef, 0xe7, 0x81, 0xfb, 0x63, 0x7b, 0xbd, 0x60,
        0x97, 0xda, 0x0e, 0x62, 0x1c,
    ];

    let mut sig = [0u8; 65];
    sig.copy_from_slice(&signature);

    assert_eq!(
        recover_address(Network::Ethereum, address.clone(), sig, MESSAGE.to_vec()).unwrap(),
        address
    );
}

#[test]
fn should_recover_sol() {
    // PK: 4c696e6b3a206469643a6164333a6877744750713432476f6a50747978356e6774535253704a666a4e
    let address = b"2q7pyhPwAwZ3QMfZrnAbDhnh9mDUqycszcpf86VgQxhF".to_vec();

    // 00f94c93e56f6a07540ac21f95449eb308495048904ccfbdc9fe9a49b890da942ec3b1cd8cad30eef7e28437afa3463d389d75e0451d715997302cc2aaaa65630e
    let signature = [
        0x00, 0xf9, 0x4c, 0x93, 0xe5, 0x6f, 0x6a, 0x07, 0x54, 0x0a, 0xc2, 0x1f, 0x95, 0x44, 0x9e,
        0xb3, 0x08, 0x49, 0x50, 0x48, 0x90, 0x4c, 0xcf, 0xbd, 0xc9, 0xfe, 0x9a, 0x49, 0xb8, 0x90,
        0xda, 0x94, 0x2e, 0xc3, 0xb1, 0xcd, 0x8c, 0xad, 0x30, 0xee, 0xf7, 0xe2, 0x84, 0x37, 0xaf,
        0xa3, 0x46, 0x3d, 0x38, 0x9d, 0x75, 0xe0, 0x45, 0x1d, 0x71, 0x59, 0x97, 0x30, 0x2c, 0xc2,
        0xaa, 0xaa, 0x65, 0x63, 0x0e,
    ];

    let mut sig = [0u8; 65];
    sig.copy_from_slice(&signature);

    assert_eq!(
        recover_address(Network::Solana, address.clone(), sig, MESSAGE.to_vec()).unwrap(),
        address
    );
}

#[test]
fn should_recover_trx() {
    // PK: da146374a75310b9666e834ee4ad0866d6f4035967bfc76217c5a495fff9f0d0
    let address = b"TPL66VK2gCXNCD7EJg9pgJRfqcRazjhUZY".to_vec();

    // 4d423812706f526a546adc810968a87b361664097bfb9bf8c768089493eecb2d1cfdc4fcbc9705da2c3f0c81b6c52c3a3c334db6656e7671194647e0628f7deb1b
    let signature = [
        0x4d, 0x42, 0x38, 0x12, 0x70, 0x6f, 0x52, 0x6a, 0x54, 0x6a, 0xdc, 0x81, 0x09, 0x68, 0xa8,
        0x7b, 0x36, 0x16, 0x64, 0x09, 0x7b, 0xfb, 0x9b, 0xf8, 0xc7, 0x68, 0x08, 0x94, 0x93, 0xee,
        0xcb, 0x2d, 0x1c, 0xfd, 0xc4, 0xfc, 0xbc, 0x97, 0x05, 0xda, 0x2c, 0x3f, 0x0c, 0x81, 0xb6,
        0xc5, 0x2c, 0x3a, 0x3c, 0x33, 0x4d, 0xb6, 0x65, 0x6e, 0x76, 0x71, 0x19, 0x46, 0x47, 0xe0,
        0x62, 0x8f, 0x7d, 0xeb, 0x1b,
    ];

    let mut sig = [0u8; 65];
    sig.copy_from_slice(&signature);

    assert_eq!(
        recover_address(Network::Tron, address.clone(), sig, MESSAGE.to_vec()).unwrap(),
        address
    );
}
