Skip to content

Commit

Permalink
Added genDeposit function
Browse files Browse the repository at this point in the history
  • Loading branch information
Kazuaki Ishiguro committed Aug 31, 2020
1 parent 4614a63 commit f24de60
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 1 deletion.
14 changes: 13 additions & 1 deletion ts/__tests__/libcream.test.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import {
createDeposit,
generateDeposit,
pedersenHash,
rbigInt,
bigInt,
toHex
} from '../'

describe('creamlib utilities', () => {
Expand All @@ -17,5 +18,16 @@ describe('creamlib utilities', () => {
expect(deposit.commitment.toString()).toEqual(pedersenHash(preimage).babyJubX.toString())
expect(deposit.nullifierHash.toString()).toEqual(pedersenHash(nullifier_buf).babyJubX.toString())
})

it('should generate `Deposit` object from deposit note correctly', () => {
const nullifier = rbigInt(31)
const secret = rbigInt(31)
const deposit = createDeposit(nullifier, secret)

const note = toHex(deposit.preimage, 62)
const generatedDeposit = generateDeposit(note)

expect(deposit.commitment.toString()).toEqual(generatedDeposit.commitment.toString())
})
})
})
20 changes: 20 additions & 0 deletions ts/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,14 @@ interface Deposit {

const bigInt = snarkjs.bigInt

const toHex = (
n: SnarkBigInt,
length = 31
): string => {
const str = n instanceof Buffer ? n.toString('hex') : bigInt(n).toString(16)
return '0x' + str.padStart(length * 2, '0')
}

const pedersenHash = (
value: SnarkBigInt
): PedersenHash => {
Expand Down Expand Up @@ -54,10 +62,22 @@ const createDeposit = (
}
}

const generateDeposit = (
note: string
): Deposit => {
const buf: Buffer = Buffer.from(note.slice(2), 'hex')
return createDeposit(
bigInt.leBuff2int(buf.slice(0, 31)),
bigInt.leBuff2int(buf.slice(31, 62))
)
}

export {
SnarkBigInt,
bigInt,
toHex,
pedersenHash,
rbigInt,
createDeposit,
generateDeposit
}

0 comments on commit f24de60

Please sign in to comment.