Last active
April 2, 2024 13:38
-
-
Save WietseWind/5c3dfe291dd23bf2ca65edc39216dd42 to your computer and use it in GitHub Desktop.
Voucher generation & claim
This file contains hidden or 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
import { | |
// generate, | |
derive | |
} from 'xrpl-accountlib' | |
import rippleAddressCodec from 'ripple-address-codec' | |
import { | |
sign, | |
verify | |
} from 'ripple-keypairs' | |
// const { address, secret: { familySeed }, keypair: { publicKey, privateKey }, } = generate.familySeed({ algorithm: 'ed25519' }) | |
/** | |
* This is th evoucher | |
*/ | |
const Voucher = { | |
privateKey: 'EDE43F4051BA986482CB9236D00C323C4E9BB92BF83750B4F93B77D5F90CDDE193', // The secret key (family seed) is the actual voucher | |
publicKey: null | |
} | |
const derived = derive.privatekey(Voucher.privateKey) | |
Voucher.publicKey = Buffer.from(derived.keypair.publicKey, 'hex') | |
// ^^ Voucher pubkey / or hash thereof is the KEY for the Hook Store that tracks the balance that can be claimed | |
// Voucher claim balance is stored as Hook State under publicKey / pubkey hash | |
/** | |
* This is the Invoke as generated | |
*/ | |
const InvokeTx = { | |
TransactionType: 'Invoke', | |
Account: 'rwietsevLFg8XSmG3bEZzFein1g8RBqWDZ', | |
Blob: null, | |
} | |
const accountId = rippleAddressCodec.decodeAccountID(InvokeTx.Account) | |
const payload = Buffer.concat([ Voucher.publicKey, accountId ]) | |
const signature = Buffer.from(sign(payload.toString('hex'), Voucher.privateKey), 'hex') | |
const Blob = Buffer.concat([ signature, payload ]).toString('hex') | |
// | |
// [ - 64 signature - ] [ - 33 voucher pubkey - ] [ - 20 destination account id - ] | |
// | |
Object.assign(InvokeTx, { Blob, }) | |
console.log(InvokeTx) | |
/** | |
* Now verify the signature | |
* ---> THIS IS WHAT NEEDS TO HAPPEN IN THE HOOK! | |
*/ | |
const verifyBlob = Buffer.from(InvokeTx.Blob, 'hex') | |
const verifyData = verifyBlob.slice(64).toString('hex') | |
const verifySignature = verifyBlob.slice(0, 64).toString('hex') | |
const verifyVoucherPubkey = verifyBlob.slice(64, -20).toString('hex') | |
const verificationVerifies = verify(verifyData, verifySignature, verifyVoucherPubkey) | |
const sendToAccount = rippleAddressCodec.encodeAccountID(verifyBlob.slice(-20)) | |
console.log({ | |
verifyData, | |
verifySignature, | |
verifyVoucherPubkey, | |
verificationVerifies, | |
sendToAccount, | |
}) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Example output: