-
Notifications
You must be signed in to change notification settings - Fork 356
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
15 changed files
with
49 additions
and
2,781 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,45 @@ | ||
import keccak256 from 'keccak256'; | ||
import { MerkleTree } from 'merkletreejs'; | ||
|
||
class Airdrop { | ||
private tree: MerkleTree; | ||
|
||
constructor(accounts: Array<{ address: string; amount: string }>) { | ||
const leaves = accounts.map((a) => keccak256(a.address + a.amount)); | ||
this.tree = new MerkleTree(leaves, keccak256, { sort: true }); | ||
} | ||
|
||
public getMerkleRoot(): string { | ||
return this.tree.getHexRoot().replace('0x', ''); | ||
} | ||
|
||
public getMerkleProof(account: { | ||
address: string; | ||
amount: string; | ||
}): string[] { | ||
return this.tree | ||
.getHexProof(keccak256(account.address + account.amount)) | ||
.map((v) => v.replace('0x', '')); | ||
} | ||
|
||
public verify( | ||
proof: string[], | ||
account: { address: string; amount: string } | ||
): boolean { | ||
let hashBuf = keccak256(account.address + account.amount); | ||
|
||
proof.forEach((proofElem) => { | ||
const proofBuf = Buffer.from(proofElem, 'hex'); | ||
|
||
if (hashBuf < proofBuf) { | ||
hashBuf = keccak256(Buffer.concat([hashBuf, proofBuf])); | ||
} else { | ||
hashBuf = keccak256(Buffer.concat([proofBuf, hashBuf])); | ||
} | ||
}); | ||
|
||
return this.getMerkleRoot() === hashBuf.toString('hex'); | ||
} | ||
} | ||
|
||
export = Airdrop; |
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
42 changes: 0 additions & 42 deletions
42
contracts/cw20-merkle-airdrop/helpers/src/commands/generateRoot.ts
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.