-
Notifications
You must be signed in to change notification settings - Fork 898
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #2177 from looikaizhi/main
task7,8
- Loading branch information
Showing
7 changed files
with
150 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
[package] | ||
name = "checkin" | ||
edition = "2024.beta" # edition = "legacy" to use legacy (pre-2024) Move | ||
# license = "" # e.g., "MIT", "GPL", "Apache 2.0" | ||
# authors = ["..."] # e.g., ["Joe Smith ([email protected])", "John Snow ([email protected])"] | ||
|
||
[dependencies] | ||
Sui = { git = "https://github.com/MystenLabs/sui.git", subdir = "crates/sui-framework/packages/sui-framework", rev = "framework/testnet" } | ||
|
||
# For remote import, use the `{ git = "...", subdir = "...", rev = "..." }`. | ||
# Revision can be a branch, a tag, and a commit hash. | ||
# MyRemotePackage = { git = "https://some.remote/host.git", subdir = "remote/path", rev = "main" } | ||
|
||
# For local dependencies use `local = path`. Path is relative to the package root | ||
# Local = { local = "../path/to" } | ||
|
||
# To resolve a version conflict and force a specific version for dependency | ||
# override use `override = true` | ||
# Override = { local = "../conflicting/version", override = true } | ||
|
||
[addresses] | ||
checkin = "0x0" | ||
|
||
# Named addresses will be accessible in Move as `@name`. They're also exported: | ||
# for example, `std = "0x1"` is exported by the Standard Library. | ||
# alice = "0xA11CE" | ||
|
||
[dev-dependencies] | ||
# The dev-dependencies section allows overriding dependencies for `--test` and | ||
# `--dev` modes. You can introduce test-only dependencies here. | ||
# Local = { local = "../path/to/dev-build" } | ||
|
||
[dev-addresses] | ||
# The dev-addresses section allows overwriting named addresses for the `--test` | ||
# and `--dev` modes. | ||
# alice = "0xB0B" | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
module checkin::checkin{ | ||
use std::ascii::String; | ||
use std::hash; | ||
use sui::bcs::to_bytes; | ||
|
||
public struct VectorAns has key{ | ||
id: UID, | ||
vec: vector<u8>, | ||
} | ||
|
||
fun init(ctx: &mut TxContext){ | ||
let vectorAns = VectorAns{ | ||
id: object::new(ctx), | ||
vec: vector::empty<u8>(), | ||
}; | ||
transfer::share_object(vectorAns); | ||
} | ||
|
||
public entry fun getHash(github_id: String, flagStr: String, vectorAns: &mut VectorAns){ | ||
let mut v0 = to_bytes(&flagStr); | ||
vector::append<u8>(&mut v0, *github_id.as_bytes()); | ||
vectorAns.vec = hash::sha3_256(v0); | ||
} | ||
|
||
} | ||
|
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
import { getFullnodeUrl, SuiClient } from '@mysten/sui/client'; | ||
import { createHash } from 'crypto'; | ||
import { bcs } from '@mysten/sui/bcs'; | ||
|
||
const rpcUrl = getFullnodeUrl('testnet'); | ||
const client = new SuiClient({url: rpcUrl}); | ||
const flagStringId = '0xc8dcd54baa7724177593a9f70598a09ae6a4286f996542e058f248209db08147'; | ||
const githubID = 'looikaizhi'; | ||
|
||
async function getFlagStringData() { | ||
const flagObject = await client.getObject({id: flagStringId, options: {showContent: true}}); | ||
const content = flagObject.data?.content; | ||
console.log(content); | ||
return content.fields.str; | ||
} | ||
|
||
async function calVector() { | ||
const flagStr = await getFlagStringData(); | ||
// 使用BCS序列化 | ||
const flagStrBytes = bcs.String.serialize(flagStr).toBytes(); | ||
const githubBytes = Buffer.from(githubID,'utf-8'); | ||
// 字符串字节添加在BCS后面 | ||
const combinedBytes = new Uint8Array(flagStrBytes.length + githubBytes.length); | ||
combinedBytes.set(flagStrBytes, 0); | ||
combinedBytes.set(githubBytes, flagStrBytes.length); | ||
// 测试 | ||
console.log(flagStrBytes); | ||
console.log(githubBytes); | ||
console.log(combinedBytes); | ||
const hash1 = createHash('sha3-256').update(combinedBytes).digest(); | ||
console.log('最终哈希值:', JSON.stringify(Array.from(hash1))); | ||
console.log("hash: ", "0x" + hash1.toString('hex')); | ||
} | ||
|
||
calVector().catch(console.error); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
import {getFullnodeUrl, SuiClient} from '@mysten/sui/client'; | ||
import {fromBase64} from '@mysten/bcs'; | ||
import { bcs } from '@mysten/sui/bcs'; | ||
import {createHash} from 'crypto'; | ||
|
||
const rpcUrl = getFullnodeUrl('testnet'); | ||
const client = new SuiClient({url: rpcUrl}); | ||
const challengeID = '0x19e76ca504c5a5fa5e214a45fca6c058171ba333f6da897b82731094504d5ab9'; | ||
const address = '0x0234bddfc4deb68aaddd61b79a5857c1b0a29268a3b2e9d162bc13f45be60e1a'; | ||
// const githubID = 'looikaizhi'; | ||
|
||
async function findAnswer(){ | ||
const challenge = await client.getObject({id : challengeID, options:{showBcs:true, showContent: true}}); | ||
console.log(challenge.data); | ||
const data = challenge.data; | ||
const content = data?.content; | ||
console.log(content); | ||
|
||
const addressBytes = bcs.Address.serialize(address).toBytes(); | ||
const challengeBytes1 = fromBase64((data!.bcs as any)['bcsBytes']); | ||
let nonce = 0; | ||
do { | ||
const proofBytes = bcs.U64.serialize(nonce).toBytes(); | ||
const combinedBytes = new Uint8Array(proofBytes.length + addressBytes.length + challengeBytes1.length); | ||
combinedBytes.set(proofBytes, 0); | ||
combinedBytes.set(addressBytes, proofBytes.length); | ||
combinedBytes.set(challengeBytes1, proofBytes.length + addressBytes.length); | ||
|
||
const hash = Array.from(createHash('sha3-256').update(combinedBytes).digest()); | ||
let prefix_sum = 0; | ||
let i = 0; | ||
while(i < content?.fields.difficulity){ | ||
prefix_sum = prefix_sum + hash[i]; | ||
i += 1; | ||
} | ||
if(prefix_sum == 0){ | ||
console.log(`第${nonce}次成功:`, JSON.stringify(Array.from(proofBytes))); | ||
console.log("proof: ", "0x" + Buffer.from(proofBytes).toString('hex')) | ||
break; | ||
}else{ | ||
nonce++; | ||
//console.log(`第${nonce}次失败:`, proofBytes); | ||
} | ||
|
||
}while(true) | ||
} | ||
|
||
findAnswer().catch(console.error); |
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters