Skip to content

Instantly share code, notes, and snippets.

@WietseWind
Last active April 2, 2024 13:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save WietseWind/5c3dfe291dd23bf2ca65edc39216dd42 to your computer and use it in GitHub Desktop.
Save WietseWind/5c3dfe291dd23bf2ca65edc39216dd42 to your computer and use it in GitHub Desktop.
Voucher generation & claim
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,
})
@WietseWind
Copy link
Author

WietseWind commented Apr 2, 2024

Example output:

{
  TransactionType: 'Invoke',
  Account: 'rwietsevLFg8XSmG3bEZzFein1g8RBqWDZ',
  Blob: 'bbfe12add101913ae0e2fa3611984ce733ea79cbd5c117d9b60174c8dad8f7b0d98ff296b6969968df811a7c62fe1f39ce40c54e64615f5d06f671daf6537604eda18c3d29df5cf5fbb7227198632c91592f7f7661f4159191c538403b83760a886c1d22888cf2cc88579d71fe0be6890fe2268bf3'
}
{
  verifyData: 'eda18c3d29df5cf5fbb7227198632c91592f7f7661f4159191c538403b83760a886c1d22888cf2cc88579d71fe0be6890fe2268bf3',
  verifySignature: 'bbfe12add101913ae0e2fa3611984ce733ea79cbd5c117d9b60174c8dad8f7b0d98ff296b6969968df811a7c62fe1f39ce40c54e64615f5d06f671daf6537604',
  verifyVoucherPubkey: 'eda18c3d29df5cf5fbb7227198632c91592f7f7661f4159191c538403b83760a88',
  verificationVerifies: true,
  sendToAccount: 'rwietsevLFg8XSmG3bEZzFein1g8RBqWDZ'
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment