Skip to content

Instantly share code, notes, and snippets.

@SuperstrongBE
Last active May 28, 2023 07:50
Show Gist options
  • Save SuperstrongBE/c1c88c64dce663772907aa1e70c56059 to your computer and use it in GitHub Desktop.
Save SuperstrongBE/c1c88c64dce663772907aa1e70c56059 to your computer and use it in GitHub Desktop.
import { Checksum256,U256,sha256 } from "proton-tsc";
export function u64ArrayToU256(assetIds:u64[]): U256 {
const hash: U256 = new U256()
const sortedAsset: u64[] = (assetIds).sort((a: u64, b: u64) => u32(a - b));
return sortedAsset.reduce((prev: U256, current: u64): U256 => {
return U256.add(prev, U256.fromU64(current));
},new U256())
}
export function u64ArrayToCheckSum256(assetIds: u64[]): Checksum256 {
const sortedAsset: u64[] = (assetIds).sort((a: u64, b: u64) => u32(a - b));
const u8array = u64ArrayToU8Array(sortedAsset);
const hash: U256 = new U256()
return sha256(u8array);
}
export function u64ArrayToU8Array(values: u64[]): u8[] {
return values.reduce((prev:u8[], val:u64):u8[] => {
return prev.concat(u64ToU8Array(val))
},new Array<u8>())
}
export function u64ToU8Array(value: u64): u8[] {
let y = u64(Math.floor(f64(value) / 2 ** 32));
return [y, (y << 8), (y << 16), (y << 24), value, (value << 8), (value << 16), (value << 24)].map<u8>((z) => u8(z >>> 24));
}
/*Need types to be fix*/
export function u8ArrayToU64(u8Array:u8[]):u64 {
return u64(u8Array.reduce((a,c,i)=> a+c*2**((56-i)*8),0))
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment