Skip to content

Commit 4c6b378

Browse files
authored
Feature/updates (#294)
* 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
1 parent 6d449cb commit 4c6b378

30 files changed

+806
-295
lines changed

.eslintrc.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ module.exports = {
1313
sourceType: 'module',
1414
},
1515
rules: {
16+
'no-continue': 'off',
1617
'no-bitwise': 'off',
1718
'import/extensions': 'off',
1819
'import/prefer-default-export': 'off',

example/nft.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ async function main() {
5656
const utxo = await readCollectibleOutput(tokenUuid, [user.user_id]);
5757
console.log(utxo);
5858

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

6262
// If a bot owns the nft, sign the transaction

example/safe.js

Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
const {
2+
MixinApi,
3+
getED25519KeyPair,
4+
getTipPinUpdateMsg,
5+
base64RawURLDecode,
6+
encodeSafeTransaction,
7+
buildSafeTransactionRecipient,
8+
buildSafeTransaction,
9+
signSafeTransaction,
10+
} = require('..');
11+
const { v4 } = require('uuid');
12+
const keystore = require('../keystore.json'); // keystore from your bot
13+
14+
const main = async () => {
15+
const client = MixinApi({ keystore });
16+
let bot = await client.user.profile();
17+
18+
// private key for safe registration
19+
let privateKey = '';
20+
// upgrade to tip pin if haven't
21+
if (!bot.tip_key_base64) {
22+
const keys = getED25519KeyPair();
23+
const pub = base64RawURLDecode(keys.publicKey);
24+
const priv = base64RawURLDecode(keys.privateKey);
25+
const tipPin = priv.toString('hex');
26+
privateKey = tipPin;
27+
28+
const b = getTipPinUpdateMsg(pub, bot.tip_counter + 1);
29+
await client.pin.update(keystore.pin, b);
30+
bot = await client.pin.verifyTipPin(tipPin);
31+
keystore.pin = tipPin; // should update pin in your keystore file too
32+
console.log('new tip pin', tipPin);
33+
}
34+
35+
// register to safe if haven't
36+
// it's convinient to use the same private key as above tipPin
37+
if (!bot.has_safe) {
38+
const resp = await client.safe.register(keystore.client_id, keystore.pin, privateKey);
39+
console.log(resp);
40+
}
41+
42+
// destination
43+
const members = ['7766b24c-1a03-4c3a-83a3-b4358266875d'];
44+
const threshold = 1;
45+
const recipients = [buildSafeTransactionRecipient(members, threshold, '1')];
46+
47+
// get ghost key to send tx to uuid multisigs
48+
// For Mixin Kernel Address start with 'XIN', get ghost key with getMainnetAddressGhostKey
49+
const ghosts = await client.utxo.ghostKey(
50+
recipients.map((r, i) => ({
51+
hint: v4(),
52+
receivers: r.members,
53+
index: i,
54+
})),
55+
);
56+
57+
// get unspent utxos
58+
const outputs = await client.utxo.safeOutputs({
59+
members: [keystore.client_id],
60+
threshold: 1,
61+
asset: 'edb4fe8b-8f05-32e3-a0e0-5d2096e7a7ac',
62+
state: 'unspent',
63+
});
64+
console.log(outputs);
65+
const balance = await client.utxo.safeAssetBalance({
66+
members: [keystore.client_id],
67+
threshold: 1,
68+
asset: 'edb4fe8b-8f05-32e3-a0e0-5d2096e7a7ac',
69+
state: 'unspent',
70+
});
71+
console.log(balance);
72+
73+
// build safe transaction raw
74+
const tx = buildSafeTransaction(outputs, recipients, ghosts, 'test-memo');
75+
console.log(tx);
76+
const raw = encodeSafeTransaction(tx);
77+
console.log(raw);
78+
79+
// verify safe transaction
80+
const request_id = v4();
81+
const verifiedTx = await client.utxo.verifyTransaction([
82+
{
83+
raw,
84+
request_id,
85+
},
86+
]);
87+
console.log(verifiedTx);
88+
89+
// sign safe transaction with the private key registerd to safe
90+
const signedRaw = await signSafeTransaction(tx, verifiedTx[0].views, privateKey);
91+
console.log(signedRaw);
92+
const sendedTx = await client.utxo.sendTransactions([
93+
{
94+
raw: signedRaw,
95+
request_id,
96+
},
97+
]);
98+
console.log(sendedTx);
99+
};
100+
101+
main();

package-lock.json

Lines changed: 37 additions & 115 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -83,19 +83,19 @@
8383
},
8484
"dependencies": {
8585
"@ethersproject/providers": "^5.6.8",
86+
"@noble/curves": "^1.2.0",
87+
"@noble/hashes": "^1.3.2",
8688
"axios": "1.6.0",
8789
"axios-retry": "3.4.0",
8890
"curve25519-js": "^0.0.4",
8991
"ethers": "^5.6.8",
9092
"int64-buffer": "^1.0.1",
9193
"is-retry-allowed": "2.2.0",
92-
"jssha": "^3.2.0",
9394
"lodash.merge": "^4.6.2",
9495
"nano-seconds": "^1.2.2",
9596
"node-forge": "^1.3.1",
9697
"pako": "^2.0.4",
9798
"serialize-javascript": "^6.0.0",
98-
"sha3": "^2.1.4",
9999
"uuid": "^9.0.0",
100100
"ws": "^8.7.0"
101101
},

0 commit comments

Comments
 (0)