Skip to content

Commit

Permalink
Feature/updates (#294)
Browse files Browse the repository at this point in the history
* Add apis for tip pin

* Prepare data to register safe

* Format

* Fix encoding

* Reorganize safe funcs

* Format

* Add safe transaction encode

* Add unfinished safe tx example

* Format

* Fix type

* Sign safe transaction with views in resp

* Slight fixes

* Slight fix

* Fix blake3 lib

* Add safeAssetBalance

* Improve params

* Add safe transaction signature test

* Add getMainnetAddressGhostKey

* Improve safe example

* Format

* Ignore noble curves type error

* Improve hash funcs

* Slight improves

* Improve func name
  • Loading branch information
hundredark authored Nov 23, 2023
1 parent 6d449cb commit 4c6b378
Show file tree
Hide file tree
Showing 30 changed files with 806 additions and 295 deletions.
1 change: 1 addition & 0 deletions .eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ module.exports = {
sourceType: 'module',
},
rules: {
'no-continue': 'off',
'no-bitwise': 'off',
'import/extensions': 'off',
'import/prefer-default-export': 'off',
Expand Down
2 changes: 1 addition & 1 deletion example/nft.js
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ async function main() {
const utxo = await readCollectibleOutput(tokenUuid, [user.user_id]);
console.log(utxo);

const multisig = await buildNfoTransferRequest(client, utxo.transaction_hash, receivers, threshold, Buffer.from('test').toString('hex'));
const multisig = await buildNfoTransferRequest(client, utxo, receivers, threshold, Buffer.from('test').toString('hex'));
console.log(multisig);

// If a bot owns the nft, sign the transaction
Expand Down
101 changes: 101 additions & 0 deletions example/safe.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
const {
MixinApi,
getED25519KeyPair,
getTipPinUpdateMsg,
base64RawURLDecode,
encodeSafeTransaction,
buildSafeTransactionRecipient,
buildSafeTransaction,
signSafeTransaction,
} = require('..');
const { v4 } = require('uuid');
const keystore = require('../keystore.json'); // keystore from your bot

const main = async () => {
const client = MixinApi({ keystore });
let bot = await client.user.profile();

// private key for safe registration
let privateKey = '';
// upgrade to tip pin if haven't
if (!bot.tip_key_base64) {
const keys = getED25519KeyPair();
const pub = base64RawURLDecode(keys.publicKey);
const priv = base64RawURLDecode(keys.privateKey);
const tipPin = priv.toString('hex');
privateKey = tipPin;

const b = getTipPinUpdateMsg(pub, bot.tip_counter + 1);
await client.pin.update(keystore.pin, b);
bot = await client.pin.verifyTipPin(tipPin);
keystore.pin = tipPin; // should update pin in your keystore file too
console.log('new tip pin', tipPin);
}

// register to safe if haven't
// it's convinient to use the same private key as above tipPin
if (!bot.has_safe) {
const resp = await client.safe.register(keystore.client_id, keystore.pin, privateKey);
console.log(resp);
}

// destination
const members = ['7766b24c-1a03-4c3a-83a3-b4358266875d'];
const threshold = 1;
const recipients = [buildSafeTransactionRecipient(members, threshold, '1')];

// get ghost key to send tx to uuid multisigs
// For Mixin Kernel Address start with 'XIN', get ghost key with getMainnetAddressGhostKey
const ghosts = await client.utxo.ghostKey(
recipients.map((r, i) => ({
hint: v4(),
receivers: r.members,
index: i,
})),
);

// get unspent utxos
const outputs = await client.utxo.safeOutputs({
members: [keystore.client_id],
threshold: 1,
asset: 'edb4fe8b-8f05-32e3-a0e0-5d2096e7a7ac',
state: 'unspent',
});
console.log(outputs);
const balance = await client.utxo.safeAssetBalance({
members: [keystore.client_id],
threshold: 1,
asset: 'edb4fe8b-8f05-32e3-a0e0-5d2096e7a7ac',
state: 'unspent',
});
console.log(balance);

// build safe transaction raw
const tx = buildSafeTransaction(outputs, recipients, ghosts, 'test-memo');
console.log(tx);
const raw = encodeSafeTransaction(tx);
console.log(raw);

// verify safe transaction
const request_id = v4();
const verifiedTx = await client.utxo.verifyTransaction([
{
raw,
request_id,
},
]);
console.log(verifiedTx);

// sign safe transaction with the private key registerd to safe
const signedRaw = await signSafeTransaction(tx, verifiedTx[0].views, privateKey);
console.log(signedRaw);
const sendedTx = await client.utxo.sendTransactions([
{
raw: signedRaw,
request_id,
},
]);
console.log(sendedTx);
};

main();
152 changes: 37 additions & 115 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -83,19 +83,19 @@
},
"dependencies": {
"@ethersproject/providers": "^5.6.8",
"@noble/curves": "^1.2.0",
"@noble/hashes": "^1.3.2",
"axios": "1.6.0",
"axios-retry": "3.4.0",
"curve25519-js": "^0.0.4",
"ethers": "^5.6.8",
"int64-buffer": "^1.0.1",
"is-retry-allowed": "2.2.0",
"jssha": "^3.2.0",
"lodash.merge": "^4.6.2",
"nano-seconds": "^1.2.2",
"node-forge": "^1.3.1",
"pako": "^2.0.4",
"serialize-javascript": "^6.0.0",
"sha3": "^2.1.4",
"uuid": "^9.0.0",
"ws": "^8.7.0"
},
Expand Down
Loading

0 comments on commit 4c6b378

Please sign in to comment.