Skip to content

Commit

Permalink
feat(su): hash chain working
Browse files Browse the repository at this point in the history
  • Loading branch information
VinceJuliano committed Dec 15, 2023
1 parent c6450b9 commit dedeebd
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 6 deletions.
22 changes: 16 additions & 6 deletions servers/su/src/domain/scheduler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -86,15 +86,25 @@ impl ProcessScheduler {
}
}

fn gen_hash_chain(previous_or_seed: &str, message_id: Option<&str>) -> String {
fn gen_hash_chain(previous_or_seed: &str, message_id: Option<&str>) -> Result<String, String> {
let mut hasher = Sha256::new();
hasher.update(previous_or_seed);
let mut all = previous_or_seed.to_string();
match message_id {
Some(id) => hasher.update(id),
Some(id) => {
all = id.to_string() + previous_or_seed;
},
None => ()
}

let bytes = match base64_url::decode(&all) {
Ok(p) => p,
Err(e) => return Err(e.to_string())
};

hasher.update(bytes);
let result = hasher.finalize();
base64_url::encode(&result)

Ok(base64_url::encode(&result))
}

/*
Expand Down Expand Up @@ -124,11 +134,11 @@ async fn fetch_values(deps: Arc<SchedulerDeps>, process_id: &String) -> Result<(
let hash_chain = gen_hash_chain(
&previous_message.hash_chain,
Some(&previous_message.message.id)
);
)?;
Ok((epoch, nonce, hash_chain, millis))
},
None => {
let hash_chain = gen_hash_chain(&process_id, None);
let hash_chain = gen_hash_chain(&process_id, None)?;
Ok((0, 0, hash_chain, millis))
}
}
Expand Down
13 changes: 13 additions & 0 deletions servers/testscripts/src/local/hashes.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import Arweave from 'arweave';

(async function () {
const pid = Arweave.utils.b64UrlToBuffer('kRIwKM5cLvEDWmgbZ8OSDT1SFdJaWQJgtpCJayDd7_4')
const hash = await Arweave.crypto.hash(pid)
const h = Arweave.utils.bufferTob64Url(hash)
console.log(h)

const mid = Arweave.utils.b64UrlToBuffer('6V-WTZpUEtzyeFur03Hgfl3GkikG5kVS44nJ366GKQg' + 'NCd1EuUNlH0O2O8eaJgFxrGNja0QhHpOWCwLDzFnFcA')
const mhash = await Arweave.crypto.hash(mid)
const h2 = Arweave.utils.bufferTob64Url(mhash)
console.log(h2)
})()
27 changes: 27 additions & 0 deletions servers/testscripts/src/local/su.js
Original file line number Diff line number Diff line change
Expand Up @@ -62,4 +62,31 @@ const { createData, ArweaveSigner } = WarpArBundles;
console.log(await dataItem.id)
console.log('Su MessageResponse 1 for Contract 1')
console.log(responseText)

const data3 = Math.random().toString().slice(-4)
const tags3 = [
{ name: 'Data-Protocol', value: 'ao' },
{ name: 'Type', value: 'Message' },
{ name: 'Test', value: 'test' }
]

const dataItem3 = createData(data3, signer, { tags: tags3, target: processId })
await dataItem3.sign(signer)

const response3 = await fetch(
'http://localhost:9000/',
{
method: 'POST',
headers: {
'Content-Type': 'application/octet-stream',
Accept: 'application/json'
},
body: dataItem3.getRaw()
}
)

const responseText3 = await response3.text()
console.log(await dataItem3.id)
console.log('Su MessageResponse 2 for Contract 1')
console.log(responseText3)
})()

0 comments on commit dedeebd

Please sign in to comment.