const tg = require("./target/timegraph_nodejs");
const { cryptoWaitReady } = require("@polkadot/util-crypto");
const { Keyring } = require("@polkadot/keyring");
const {u8aWrapBytes} = require("@polkadot/util")
const hexToBytes = (hex) => {
var bytes = [];
for (var c = 0; c < hex.length; c += 2) {
bytes.push(parseInt(hex.substr(c, 2), 16));
}
return bytes;
};
test("Create API key and then SSK with polkadot Keyring ", async () => {
await cryptoWaitReady();
const keyring = new Keyring({ type: "sr25519" });
const keypair = keyring.addFromSeed(hexToBytes("aa250e48af7472b8cd4d7809a169fe884d4271522cf99e876cef1c8e95b32ba4"));
expect(keypair.address).toStrictEqual("5CJJ7L3sXxUAPEcsEUNRayf1yhG6RQihqbKN6TB7mvLB4KZX");
const [cert_data, secret] = tg.new_cert(keypair.address, "developer");
const signature = keypair.sign(cert_data);
const key = tg.build_apikey(secret, cert_data, signature);
cert = tg.verify_cert(key.cert);
expect(cert.owner).toStrictEqual(tg.address_as_pubkey(keypair.address));
const ssk_data = tg.encode_ssk({
ns: 0,
key: keypair.address,
secret: "",
expiration: 0,
});
const ssk_signature = keypair.sign(ssk_data);
const ssk = tg.build_ssk(ssk_data, ssk_signature);
const ssk_info = tg.verify_ssk(ssk);
console.log(ssk_info);
expect(ssk_info).toStrictEqual({
ns: 0,
key: tg.address_as_pubkey(keypair.address),
expiration: 0,
user_id: undefined,
});
});
test("Create wallet SSK with polkadot Keyring ", async () => {
await cryptoWaitReady();
const keyring = new Keyring({ type: "sr25519" });
const keypair = keyring.addFromSeed(hexToBytes("aa250e48af7472b8cd4d7809a169fe884d4271522cf99e876cef1c8e95b32ba4"));
expect(keypair.address).toStrictEqual("5CJJ7L3sXxUAPEcsEUNRayf1yhG6RQihqbKN6TB7mvLB4KZX");
const ssk_data = tg.encode_ssk({
ns: 0,
key: keypair.address,
user_id: 1,
expiration: 0,
});
const ssk_signature = keypair.sign(ssk_data);
const ssk = tg.build_ssk(ssk_data, ssk_signature);
const ssk_info = tg.verify_ssk(ssk);
console.log(ssk_info);
expect(ssk_info).toStrictEqual({
ns: 0,
key: tg.address_as_pubkey(keypair.address),
expiration: 0,
user_id: 1,
});
});
test("Create API key and then SSK with polkadot Keyring wrapped bytes ", async () => {
await cryptoWaitReady();
const keyring = new Keyring({ type: "sr25519" });
const keypair = keyring.addFromSeed(hexToBytes("aa250e48af7472b8cd4d7809a169fe884d4271522cf99e876cef1c8e95b32ba4"));
expect(keypair.address).toStrictEqual("5CJJ7L3sXxUAPEcsEUNRayf1yhG6RQihqbKN6TB7mvLB4KZX");
const [cert_data, secret] = tg.new_cert(keypair.address, "developer");
const signature = keypair.sign( u8aWrapBytes(cert_data));
const key = tg.build_apikey(secret, cert_data, signature);
cert = tg.verify_cert(key.cert);
expect(cert.owner).toStrictEqual(tg.address_as_pubkey(keypair.address));
const ssk_data = tg.encode_ssk({
ns: 0,
key: keypair.address,
secret: "",
expiration: 0,
});
const ssk_signature = keypair.sign(u8aWrapBytes(ssk_data));
const ssk = tg.build_ssk(ssk_data, ssk_signature);
const ssk_info = tg.verify_ssk(ssk);
console.log(ssk_info);
expect(ssk_info).toStrictEqual({
ns: 0,
key: tg.address_as_pubkey(keypair.address),
expiration: 0,
user_id: undefined,
});
});
test("Verify externally signed wallet SSK", () => {
const owner = "5CJJ7L3sXxUAPEcsEUNRayf1yhG6RQihqbKN6TB7mvLB4KZX";
const ssk_data = tg.encode_ssk({
ns: 0,
key: owner,
secret: "",
expiration: 0,
});
const signature =
"bcf8be42cf4b479648b2eae7a19ec2ebf2c49be4e761def16f3e2a3d9cf2eb5e38b312e4e2be8819e6b089d30ab21817a536da5e2e4cafc3e2abfa4d0044e38c";
const ssk = tg.build_ssk(ssk_data, hexToBytes(signature));
console.log("ssk", tg.verify_ssk(ssk));
expect(tg.verify_ssk(ssk)).toStrictEqual({
ns: 0,
key: tg.address_as_pubkey(owner),
expiration: 0,
user_id: undefined,
});
});
test("", async () => {
await cryptoWaitReady();
const keyring = new Keyring({ type: "sr25519" });
const keypair = keyring.addFromUri("//Alice");
console.log(keypair.address,keypair.toJson())
})